]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/date/stamp.js.uncompressed.js
1 define("dojo/date/stamp", ["../_base/lang", "../_base/array"], function(lang
, array
){
10 lang
.setObject("dojo.date.stamp", stamp
);
12 // Methods to convert dates to or from a wire (string) format using well-known conventions
14 stamp
.fromISOString = function(/*String*/ formattedString
, /*Number?*/ defaultTime
){
16 // Returns a Date object given a string formatted according to a subset of the ISO-8601 standard.
19 // Accepts a string formatted according to a profile of ISO8601 as defined by
20 // [RFC3339](http://www.ietf.org/rfc/rfc3339.txt), except that partial input is allowed.
21 // Can also process dates as specified [by the W3C](http://www.w3.org/TR/NOTE-datetime)
22 // The following combinations are valid:
28 // - times only, with an optional time zone appended
32 // - and "datetimes" which could be any combination of the above
34 // timezones may be specified as Z (for UTC) or +/- followed by a time expression HH:mm
35 // Assumes the local time zone if not specified. Does not validate. Improperly formatted
36 // input may return null. Arguments which are out of bounds will be handled
37 // by the Date constructor (e.g. January 32nd typically gets resolved to February 1st)
38 // Only years between 100 and 9999 are supported.
40 // A string such as 2005-06-30T08:05:00-07:00 or 2005-06-30 or T08:05:00
42 // Used for defaults for fields omitted in the formattedString.
43 // Uses 1970-01-01T00:00:00.0Z by default.
45 if(!stamp
._isoRegExp
){
47 //TODO: could be more restrictive and check for 00-59, etc.
48 /^(?:(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(.\d+)?)?((?:[+-](\d{2}):(\d{2}))|Z)?)?$/;
51 var match
= stamp
._isoRegExp
.exec(formattedString
),
56 if(match
[1]){match
[1]--;} // Javascript Date months are 0-based
57 if(match
[6]){match
[6] *= 1000;} // Javascript Date expects fractional seconds as milliseconds
60 // mix in defaultTime. Relatively expensive, so use || operators for the fast path of defaultTime === 0
61 defaultTime
= new Date(defaultTime
);
62 array
.forEach(array
.map(["FullYear", "Month", "Date", "Hours", "Minutes", "Seconds", "Milliseconds"], function(prop
){
63 return defaultTime
["get" + prop
]();
64 }), function(value
, index
){
65 match
[index
] = match
[index
] || value
;
68 result
= new Date(match
[0]||1970, match
[1]||0, match
[2]||1, match
[3]||0, match
[4]||0, match
[5]||0, match
[6]||0); //TODO: UTC defaults
70 result
.setFullYear(match
[0] || 1970);
74 zoneSign
= match
[7] && match
[7].charAt(0);
76 offset
= ((match
[8] || 0) * 60) + (Number(match
[9]) || 0);
77 if(zoneSign
!= '-'){ offset
*= -1; }
80 offset
-= result
.getTimezoneOffset();
83 result
.setTime(result
.getTime() + offset
* 60000);
87 return result
; // Date or null
93 // "date" or "time" for partial formatting of the Date object.
94 // Both date and time will be formatted by default.
96 // if true, UTC/GMT is used for a timezone
97 // milliseconds: Boolean
98 // if true, output milliseconds
102 stamp
.toISOString = function(/*Date*/ dateObject
, /*__Options?*/ options
){
104 // Format a Date object as a string according a subset of the ISO-8601 standard
107 // When options.selector is omitted, output follows [RFC3339](http://www.ietf.org/rfc/rfc3339.txt)
108 // The local time zone is included as an offset from GMT, except when selector=='time' (time without a date)
109 // Does not check bounds. Only years between 100 and 9999 are supported.
114 var _ = function(n
){ return (n
< 10) ? "0" + n
: n
; };
115 options
= options
|| {};
116 var formattedDate
= [],
117 getter
= options
.zulu
? "getUTC" : "get",
119 if(options
.selector
!= "time"){
120 var year
= dateObject
[getter
+"FullYear"]();
121 date
= ["0000".substr((year
+"").length
)+year
, _(dateObject
[getter
+"Month"]()+1), _(dateObject
[getter
+"Date"]())].join('-');
123 formattedDate
.push(date
);
124 if(options
.selector
!= "date"){
125 var time
= [_(dateObject
[getter
+"Hours"]()), _(dateObject
[getter
+"Minutes"]()), _(dateObject
[getter
+"Seconds"]())].join(':');
126 var millis
= dateObject
[getter
+"Milliseconds"]();
127 if(options
.milliseconds
){
128 time
+= "."+ (millis
< 100 ? "0" : "") + _(millis
);
132 }else if(options
.selector
!= "time"){
133 var timezoneOffset
= dateObject
.getTimezoneOffset();
134 var absOffset
= Math
.abs(timezoneOffset
);
135 time
+= (timezoneOffset
> 0 ? "-" : "+") +
136 _(Math
.floor(absOffset
/60)) + ":" + _(absOffset
%60);
138 formattedDate
.push(time
);
140 return formattedDate
.join('T'); // String