]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojo/date/stamp.js
Comment class stub
[eow] / static / dojo-release-1.1.1 / dojo / date / stamp.js
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");
4
5 // Methods to convert dates to or from a wire (string) format using well-known conventions
6
7 dojo.date.stamp.fromISOString = function(/*String*/formattedString, /*Number?*/defaultTime){
8         //      summary:
9         //              Returns a Date object given a string formatted according to a subset of the ISO-8601 standard.
10         //
11         //      description:
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:
16         //
17         //                      * dates only
18         //                      |       * yyyy
19         //                      |       * yyyy-MM
20         //                      |       * yyyy-MM-dd
21         //                      * times only, with an optional time zone appended
22         //                      |       * THH:mm
23         //                      |       * THH:mm:ss
24         //                      |       * THH:mm:ss.SSS
25         //                      * and "datetimes" which could be any combination of the above
26         //
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.
32         //
33         //      formattedString:
34         //              A string such as 2005-06-30T08:05:00-07:00 or 2005-06-30 or T08:05:00
35         //
36         //      defaultTime:
37         //              Used for defaults for fields omitted in the formattedString.
38         //              Uses 1970-01-01T00:00:00.0Z by default.
39
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)?)?$/;
44         }
45
46         var match = dojo.date.stamp._isoRegExp.exec(formattedString);
47         var result = null;
48
49         if(match){
50                 match.shift();
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
53
54                 if(defaultTime){
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){
61                                         match[index] = value;
62                                 }
63                         });
64                 }
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
67
68                 var offset = 0;
69                 var zoneSign = match[7] && match[7].charAt(0);
70                 if(zoneSign != 'Z'){
71                         offset = ((match[8] || 0) * 60) + (Number(match[9]) || 0);
72                         if(zoneSign != '-'){ offset *= -1; }
73                 }
74                 if(zoneSign){
75                         offset -= result.getTimezoneOffset();
76                 }
77                 if(offset){
78                         result.setTime(result.getTime() + offset * 60000);
79                 }
80         }
81
82         return result; // Date or null
83 }
84
85 /*=====
86         dojo.date.stamp.__Options = function(){
87                 //      selector: String
88                 //              "date" or "time" for partial formatting of the Date object.
89                 //              Both date and time will be formatted by default.
90                 //      zulu: Boolean
91                 //              if true, UTC/GMT is used for a timezone
92                 //      milliseconds: Boolean
93                 //              if true, output milliseconds
94                 this.selector = selector;
95                 this.zulu = zulu;
96                 this.milliseconds = milliseconds;
97         }
98 =====*/
99
100 dojo.date.stamp.toISOString = function(/*Date*/dateObject, /*dojo.date.stamp.__Options?*/options){
101         //      summary:
102         //              Format a Date object as a string according a subset of the ISO-8601 standard
103         //
104         //      description:
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.
108         //
109         //      dateObject:
110         //              A Date object
111
112         var _ = function(n){ return (n < 10) ? "0" + n : n; };
113         options = options || {};
114         var formattedDate = [];
115         var getter = options.zulu ? "getUTC" : "get";
116         var date = "";
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('-');
120         }
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);
127                 }
128                 if(options.zulu){
129                         time += "Z";
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);
135                 }
136                 formattedDate.push(time);
137         }
138         return formattedDate.join('T'); // String
139 }
140
141 }