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