]> git.wh0rd.org - tt-rss.git/blame - lib/dojo/date/stamp.js
remove call-by-reference to comply with php 5.4
[tt-rss.git] / lib / dojo / date / stamp.js
CommitLineData
2f01fe57 1/*
81bea17a 2 Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
2f01fe57
AD
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
5*/
6
7
a089699c
AD
8if(!dojo._hasResource["dojo.date.stamp"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9dojo._hasResource["dojo.date.stamp"] = true;
2f01fe57 10dojo.provide("dojo.date.stamp");
a089699c 11
81bea17a
AD
12dojo.getObject("date.stamp", true, dojo);
13
a089699c
AD
14// Methods to convert dates to or from a wire (string) format using well-known conventions
15
16dojo.date.stamp.fromISOString = function(/*String*/formattedString, /*Number?*/defaultTime){
17 // summary:
18 // Returns a Date object given a string formatted according to a subset of the ISO-8601 standard.
19 //
20 // description:
21 // Accepts a string formatted according to a profile of ISO8601 as defined by
22 // [RFC3339](http://www.ietf.org/rfc/rfc3339.txt), except that partial input is allowed.
23 // Can also process dates as specified [by the W3C](http://www.w3.org/TR/NOTE-datetime)
24 // The following combinations are valid:
25 //
26 // * dates only
27 // | * yyyy
28 // | * yyyy-MM
29 // | * yyyy-MM-dd
30 // * times only, with an optional time zone appended
31 // | * THH:mm
32 // | * THH:mm:ss
33 // | * THH:mm:ss.SSS
34 // * and "datetimes" which could be any combination of the above
35 //
36 // timezones may be specified as Z (for UTC) or +/- followed by a time expression HH:mm
37 // Assumes the local time zone if not specified. Does not validate. Improperly formatted
38 // input may return null. Arguments which are out of bounds will be handled
39 // by the Date constructor (e.g. January 32nd typically gets resolved to February 1st)
40 // Only years between 100 and 9999 are supported.
41 //
42 // formattedString:
43 // A string such as 2005-06-30T08:05:00-07:00 or 2005-06-30 or T08:05:00
44 //
45 // defaultTime:
46 // Used for defaults for fields omitted in the formattedString.
47 // Uses 1970-01-01T00:00:00.0Z by default.
48
49 if(!dojo.date.stamp._isoRegExp){
50 dojo.date.stamp._isoRegExp =
51//TODO: could be more restrictive and check for 00-59, etc.
52 /^(?:(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(.\d+)?)?((?:[+-](\d{2}):(\d{2}))|Z)?)?$/;
53 }
54
55 var match = dojo.date.stamp._isoRegExp.exec(formattedString),
56 result = null;
57
58 if(match){
59 match.shift();
60 if(match[1]){match[1]--;} // Javascript Date months are 0-based
61 if(match[6]){match[6] *= 1000;} // Javascript Date expects fractional seconds as milliseconds
62
63 if(defaultTime){
64 // mix in defaultTime. Relatively expensive, so use || operators for the fast path of defaultTime === 0
65 defaultTime = new Date(defaultTime);
66 dojo.forEach(dojo.map(["FullYear", "Month", "Date", "Hours", "Minutes", "Seconds", "Milliseconds"], function(prop){
67 return defaultTime["get" + prop]();
68 }), function(value, index){
69 match[index] = match[index] || value;
70 });
71 }
72 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
73 if(match[0] < 100){
74 result.setFullYear(match[0] || 1970);
75 }
76
77 var offset = 0,
78 zoneSign = match[7] && match[7].charAt(0);
79 if(zoneSign != 'Z'){
80 offset = ((match[8] || 0) * 60) + (Number(match[9]) || 0);
81 if(zoneSign != '-'){ offset *= -1; }
82 }
83 if(zoneSign){
84 offset -= result.getTimezoneOffset();
85 }
86 if(offset){
87 result.setTime(result.getTime() + offset * 60000);
88 }
89 }
90
91 return result; // Date or null
81bea17a 92};
a089699c
AD
93
94/*=====
95 dojo.date.stamp.__Options = function(){
96 // selector: String
97 // "date" or "time" for partial formatting of the Date object.
98 // Both date and time will be formatted by default.
99 // zulu: Boolean
100 // if true, UTC/GMT is used for a timezone
101 // milliseconds: Boolean
102 // if true, output milliseconds
103 this.selector = selector;
104 this.zulu = zulu;
105 this.milliseconds = milliseconds;
106 }
107=====*/
108
109dojo.date.stamp.toISOString = function(/*Date*/dateObject, /*dojo.date.stamp.__Options?*/options){
110 // summary:
111 // Format a Date object as a string according a subset of the ISO-8601 standard
112 //
113 // description:
114 // When options.selector is omitted, output follows [RFC3339](http://www.ietf.org/rfc/rfc3339.txt)
115 // The local time zone is included as an offset from GMT, except when selector=='time' (time without a date)
116 // Does not check bounds. Only years between 100 and 9999 are supported.
117 //
118 // dateObject:
119 // A Date object
120
121 var _ = function(n){ return (n < 10) ? "0" + n : n; };
122 options = options || {};
123 var formattedDate = [],
124 getter = options.zulu ? "getUTC" : "get",
125 date = "";
126 if(options.selector != "time"){
127 var year = dateObject[getter+"FullYear"]();
128 date = ["0000".substr((year+"").length)+year, _(dateObject[getter+"Month"]()+1), _(dateObject[getter+"Date"]())].join('-');
129 }
130 formattedDate.push(date);
131 if(options.selector != "date"){
132 var time = [_(dateObject[getter+"Hours"]()), _(dateObject[getter+"Minutes"]()), _(dateObject[getter+"Seconds"]())].join(':');
133 var millis = dateObject[getter+"Milliseconds"]();
134 if(options.milliseconds){
135 time += "."+ (millis < 100 ? "0" : "") + _(millis);
136 }
137 if(options.zulu){
138 time += "Z";
139 }else if(options.selector != "time"){
140 var timezoneOffset = dateObject.getTimezoneOffset();
141 var absOffset = Math.abs(timezoneOffset);
81bea17a 142 time += (timezoneOffset > 0 ? "-" : "+") +
a089699c
AD
143 _(Math.floor(absOffset/60)) + ":" + _(absOffset%60);
144 }
145 formattedDate.push(time);
146 }
147 return formattedDate.join('T'); // String
81bea17a 148};
a089699c 149
2f01fe57 150}