Skip to content

Commit 86af116

Browse files
author
Daniel Herzog
committed
Added date.FromLocaleISOString, fixed setting value attributes to input type=datetime-local that it doesn't interpret.
1 parent 14dfb97 commit 86af116

File tree

4 files changed

+86
-11
lines changed

4 files changed

+86
-11
lines changed

src/cookie-manager/cookie_manager_templates.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ templates.cookie_manager = {
4343
var datetime_local_val;
4444
if (value_in_seconds)
4545
{
46-
datetime_local_val= new Date(value_in_seconds * 1000).toISOString();
46+
datetime_local_val= new Date(value_in_seconds * 1000).toLocaleISOString();
4747
}
4848
return [
4949
"input",

src/cookie-manager/cookie_manager_views.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -430,17 +430,10 @@ cls.CookieManager.CookieManagerViewBase = function()
430430
// "domain" is val of [input] (with add_cookie service present), or runtimes .hostname
431431
var domain = domain_input && domain_input.value.trim() || runtime && this.data._rts[runtime].hostname;
432432

433-
// Fix expires value, work around CORE-47780: .value property of <input type=datetime-local>
434-
// element has two digits representing milliseconds, instead of three.
435-
if (/.\.\d{2}$/.test(expires))
436-
expires += "0";
437-
438-
expires = new Date(expires || 0).getTime();
439-
440-
// An expires value of 0 represents a session cookie.
441-
// Other values represent local time values. Add timezone offset.
442433
if (expires)
443-
expires += new Date().getTimezoneOffset() * 60 * 1000;
434+
expires = Date.fromLocaleISOString(expires).getTime();
435+
else
436+
expires = 0; // 0 means the cookie expires after the session.
444437

445438
var object_id = edit_tr.getAttribute("data-object-id");
446439
var old_cookie;

src/scripts/dom.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,40 @@ Date.prototype.toLocaleISOString = function()
777777
return new Date(this.getTime() - this.getTimezoneOffset() * 1000 * 60).toISOString().replace('Z','');
778778
};
779779

780+
Date.fromLocaleISOString = function(localeISOString)
781+
{
782+
// A localeISOString looks like one of these:
783+
// 2012-08-03T16:11
784+
// 2012-08-03T16:11:52.61 (the it's CORE-47780)
785+
// 2012-08-03T16:11:52.611
786+
787+
var parts = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::(\d{2})\.(\d{2,3}))?$/.exec(localeISOString);
788+
if (parts)
789+
{
790+
parts.shift();
791+
var date = new Date(0);
792+
[
793+
// setXyz methods are used to set local timezone, as opposed to setXyzUTC
794+
date.setFullYear,
795+
function(v){
796+
this.setMonth(v - 1)
797+
},
798+
date.setDate,
799+
date.setHours,
800+
date.setMinutes,
801+
date.setSeconds,
802+
date.setMilliseconds
803+
].forEach(function(func){
804+
var val = parts.shift();
805+
if (val)
806+
func.call(date, Number(val));
807+
808+
});
809+
return date;
810+
}
811+
return null;
812+
};
813+
780814
/**
781815
* Convenience function for loading a resource with XHR using the get method.
782816
* Will automatically append a "time" guery argument to avoid caching.

tests/locale_iso_strings.html

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Dragonfly - Locale ISO strings</title>
5+
<link rel="stylesheet" href="qunit/qunit.css" media="screen">
6+
<script src="qunit/qunit.js"></script>
7+
<script src="../src/scripts/dom.js"></script>
8+
9+
10+
<script>
11+
12+
13+
test("locale_iso_strings", function()
14+
{
15+
var TIMEZONE_OFFSET = new Date().getTimezoneOffset() * 1000 * 60;
16+
17+
// The outcome of toLocaleISOString depends on the timezone.
18+
// Using getTimezoneOffset to hit the moving target at beer o'clock.
19+
// The moment we create a Date object on, for comparison, is variable.
20+
var d = new Date(1344013200000 + TIMEZONE_OFFSET);
21+
equal(d.toLocaleISOString(),"2012-08-03T17:00:00.000");
22+
23+
// The outcome of toLocaleISOString depends on the timezone.
24+
// The expected moment needs to be adjusted.
25+
var t = Date.fromLocaleISOString("2012-08-03T17:00").getTime();
26+
equal(t, 1344013200000 + TIMEZONE_OFFSET);
27+
28+
var t = Date.fromLocaleISOString("2012-08-03T17:00:00.000").getTime();
29+
equal(t, 1344013200000 + TIMEZONE_OFFSET);
30+
31+
var t = Date.fromLocaleISOString("2012-08-03T17:00:00.00").getTime();
32+
equal(t, 1344013200000 + TIMEZONE_OFFSET);
33+
34+
});
35+
36+
37+
38+
</script>
39+
40+
</head>
41+
<body>
42+
<h1 id="qunit-header">Dragonfly - Locale ISO strings</h1>
43+
<h2 id="qunit-banner"></h2>
44+
<div id="qunit-testrunner-toolbar"></div>
45+
<h2 id="qunit-userAgent"></h2>
46+
<ol id="qunit-tests"></ol>
47+
</body>
48+
</html>

0 commit comments

Comments
 (0)