1 if(!dojo._hasResource["dojo.date.stamp"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo.date.stamp"] = true;
3 dojo.provide("dojo.date.stamp");
5 // Methods to convert dates to or from a wire (string) format using well-known conventions
7 dojo.date.stamp.fromISOString = function(/*String*/formattedString, /*Number?*/defaultTime){
9 // Returns a Date object given a string formatted according to a subset of the ISO-8601 standard.
12 // Accepts a string formatted according to a profile of ISO8601 as defined by
13 // [RFC3339](http://www.ietf.org/rfc/rfc3339.txt), except that partial input is allowed.
14 // Can also process dates as specified [by the W3C](http://www.w3.org/TR/NOTE-datetime)
15 // The following combinations are valid:
21 // * times only, with an optional time zone appended
25 // * and "datetimes" which could be any combination of the above
27 // timezones may be specified as Z (for UTC) or +/- followed by a time expression HH:mm
28 // Assumes the local time zone if not specified. Does not validate. Improperly formatted
29 // input may return null. Arguments which are out of bounds will be handled
30 // by the Date constructor (e.g. January 32nd typically gets resolved to February 1st)
31 // Only years between 100 and 9999 are supported.
34 // A string such as 2005-06-30T08:05:00-07:00 or 2005-06-30 or T08:05:00
37 // Used for defaults for fields omitted in the formattedString.
38 // Uses 1970-01-01T00:00:00.0Z by default.
40 if(!dojo.date.stamp._isoRegExp){
41 dojo.date.stamp._isoRegExp =
42 //TODO: could be more restrictive and check for 00-59, etc.
43 /^(?:(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(.\d+)?)?((?:[+-](\d{2}):(\d{2}))|Z)?)?$/;
46 var match = dojo.date.stamp._isoRegExp.exec(formattedString);
51 if(match[1]){match[1]--;} // Javascript Date months are 0-based
52 if(match[6]){match[6] *= 1000;} // Javascript Date expects fractional seconds as milliseconds
55 // mix in defaultTime. Relatively expensive, so use || operators for the fast path of defaultTime === 0
56 defaultTime = new Date(defaultTime);
57 dojo.map(["FullYear", "Month", "Date", "Hours", "Minutes", "Seconds", "Milliseconds"], function(prop){
58 return defaultTime["get" + prop]();
59 }).forEach(function(value, index){
60 if(match[index] === undefined){
65 result = new Date(match[0]||1970, match[1]||0, match[2]||1, match[3]||0, match[4]||0, match[5]||0, match[6]||0);
66 // result.setFullYear(match[0]||1970); // for year < 100
69 var zoneSign = match[7] && match[7].charAt(0);
71 offset = ((match[8] || 0) * 60) + (Number(match[9]) || 0);
72 if(zoneSign != '-'){ offset *= -1; }
75 offset -= result.getTimezoneOffset();
78 result.setTime(result.getTime() + offset * 60000);
82 return result; // Date or null
86 dojo.date.stamp.__Options = function(){
88 // "date" or "time" for partial formatting of the Date object.
89 // Both date and time will be formatted by default.
91 // if true, UTC/GMT is used for a timezone
92 // milliseconds: Boolean
93 // if true, output milliseconds
94 this.selector = selector;
96 this.milliseconds = milliseconds;
100 dojo.date.stamp.toISOString = function(/*Date*/dateObject, /*dojo.date.stamp.__Options?*/options){
102 // Format a Date object as a string according a subset of the ISO-8601 standard
105 // When options.selector is omitted, output follows [RFC3339](http://www.ietf.org/rfc/rfc3339.txt)
106 // The local time zone is included as an offset from GMT, except when selector=='time' (time without a date)
107 // Does not check bounds. Only years between 100 and 9999 are supported.
112 var _ = function(n){ return (n < 10) ? "0" + n : n; };
113 options = options || {};
114 var formattedDate = [];
115 var getter = options.zulu ? "getUTC" : "get";
117 if(options.selector != "time"){
118 var year = dateObject[getter+"FullYear"]();
119 date = ["0000".substr((year+"").length)+year, _(dateObject[getter+"Month"]()+1), _(dateObject[getter+"Date"]())].join('-');
121 formattedDate.push(date);
122 if(options.selector != "date"){
123 var time = [_(dateObject[getter+"Hours"]()), _(dateObject[getter+"Minutes"]()), _(dateObject[getter+"Seconds"]())].join(':');
124 var millis = dateObject[getter+"Milliseconds"]();
125 if(options.milliseconds){
126 time += "."+ (millis < 100 ? "0" : "") + _(millis);
130 }else if(options.selector != "time"){
131 var timezoneOffset = dateObject.getTimezoneOffset();
132 var absOffset = Math.abs(timezoneOffset);
133 time += (timezoneOffset > 0 ? "-" : "+") +
134 _(Math.floor(absOffset/60)) + ":" + _(absOffset%60);
136 formattedDate.push(time);
138 return formattedDate.join('T'); // String