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