source: Dev/branches/rest-dojo-ui/client/dojo/date/stamp.js @ 263

Last change on this file since 263 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 5.1 KB
Line 
1define(["../_base/kernel", "../_base/lang", "../_base/array"], function(dojo, lang, array) {
2        // module:
3        //              dojo/date/stamp
4        // summary:
5        //              TODOC
6
7lang.getObject("date.stamp", true, dojo);
8
9// Methods to convert dates to or from a wire (string) format using well-known conventions
10
11dojo.date.stamp.fromISOString = function(/*String*/formattedString, /*Number?*/defaultTime){
12        //      summary:
13        //              Returns a Date object given a string formatted according to a subset of the ISO-8601 standard.
14        //
15        //      description:
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:
20        //
21        //                      * dates only
22        //                      |       * yyyy
23        //                      |       * yyyy-MM
24        //                      |       * yyyy-MM-dd
25        //                      * times only, with an optional time zone appended
26        //                      |       * THH:mm
27        //                      |       * THH:mm:ss
28        //                      |       * THH:mm:ss.SSS
29        //                      * and "datetimes" which could be any combination of the above
30        //
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.
36        //
37        //      formattedString:
38        //              A string such as 2005-06-30T08:05:00-07:00 or 2005-06-30 or T08:05:00
39        //
40        //      defaultTime:
41        //              Used for defaults for fields omitted in the formattedString.
42        //              Uses 1970-01-01T00:00:00.0Z by default.
43
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)?)?$/;
48        }
49
50        var match = dojo.date.stamp._isoRegExp.exec(formattedString),
51                result = null;
52
53        if(match){
54                match.shift();
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
57
58                if(defaultTime){
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;
65                        });
66                }
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
68                if(match[0] < 100){
69                        result.setFullYear(match[0] || 1970);
70                }
71
72                var offset = 0,
73                        zoneSign = match[7] && match[7].charAt(0);
74                if(zoneSign != 'Z'){
75                        offset = ((match[8] || 0) * 60) + (Number(match[9]) || 0);
76                        if(zoneSign != '-'){ offset *= -1; }
77                }
78                if(zoneSign){
79                        offset -= result.getTimezoneOffset();
80                }
81                if(offset){
82                        result.setTime(result.getTime() + offset * 60000);
83                }
84        }
85
86        return result; // Date or null
87};
88
89/*=====
90        dojo.date.stamp.__Options = function(){
91                //      selector: String
92                //              "date" or "time" for partial formatting of the Date object.
93                //              Both date and time will be formatted by default.
94                //      zulu: Boolean
95                //              if true, UTC/GMT is used for a timezone
96                //      milliseconds: Boolean
97                //              if true, output milliseconds
98                this.selector = selector;
99                this.zulu = zulu;
100                this.milliseconds = milliseconds;
101        }
102=====*/
103
104dojo.date.stamp.toISOString = function(/*Date*/dateObject, /*dojo.date.stamp.__Options?*/options){
105        //      summary:
106        //              Format a Date object as a string according a subset of the ISO-8601 standard
107        //
108        //      description:
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.
112        //
113        //      dateObject:
114        //              A Date object
115
116        var _ = function(n){ return (n < 10) ? "0" + n : n; };
117        options = options || {};
118        var formattedDate = [],
119                getter = options.zulu ? "getUTC" : "get",
120                date = "";
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('-');
124        }
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);
131                }
132                if(options.zulu){
133                        time += "Z";
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);
139                }
140                formattedDate.push(time);
141        }
142        return formattedDate.join('T'); // String
143};
144
145return dojo.date.stamp;
146});
Note: See TracBrowser for help on using the repository browser.