!import
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is Robert Sayre.
18 * Portions created by the Initial Developer are Copyright (C) 2006
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Flock Inc. <erwan@flock.com>
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38 const HOURS_TO_MINUTES = 60;
39 const MINUTES_TO_SECONDS = 60;
40 const SECONDS_TO_MILLISECONDS = 1000;
41 const MINUTES_TO_MILLISECONDS = MINUTES_TO_SECONDS * SECONDS_TO_MILLISECONDS;
42 const HOURS_TO_MILLISECONDS = HOURS_TO_MINUTES * MINUTES_TO_MILLISECONDS;
43
44 var EXPORTED_SYMBOLS = ["ISO8601DateUtils"];
45
46 debug("*** loading ISO8601DateUtils\n");
47
48 var ISO8601DateUtils = {
49
50 /**
51 * XXX Thunderbird's W3C-DTF function
52 *
53 * Converts a W3C-DTF (subset of ISO 8601) date string to a Javascript
54 * date object. W3C-DTF is described in this note:
55 * http://www.w3.org/TR/NOTE-datetime IETF is obtained via the Date
56 * object's toUTCString() method. The object's toString() method is
57 * insufficient because it spells out timezones on Win32
58 * (f.e. "Pacific Standard Time" instead of "PST"), which Mail doesn't
59 * grok. For info, see
60 * http://lxr.mozilla.org/mozilla/source/js/src/jsdate.c#1526.
61 */
ISO8601_parse
62 parse: function ISO8601_parse(aDateString) {
63 var dateString = aDateString;
64 if (!dateString.match('-')) {
65 // Workaround for server sending
66 // dates such as: 20030530T11:18:50-08:00
67 // instead of: 2003-05-30T11:18:50-08:00
68 var year = dateString.slice(0, 4);
69 var month = dateString.slice(4, 6);
70 var rest = dateString.slice(6, dateString.length);
71 dateString = year + "-" + month + "-" + rest;
72 }
73
74 var parts = dateString.match(/(\d{4})(-(\d{2,3}))?(-(\d{2}))?(T(\d{2}):(\d{2})(:(\d{2})(\.(\d+))?)?(Z|([+-])(\d{2}):(\d{2}))?)?/);
75
76 // Here's an example of a W3C-DTF date string and what .match returns for it.
77 //
78 // date: 2003-05-30T11:18:50.345-08:00
79 // date.match returns array values:
80 //
81 // 0: 2003-05-30T11:18:50-08:00,
82 // 1: 2003,
83 // 2: -05,
84 // 3: 05,
85 // 4: -30,
86 // 5: 30,
87 // 6: T11:18:50-08:00,
88 // 7: 11,
89 // 8: 18,
90 // 9: :50,
91 // 10: 50,
92 // 11: .345,
93 // 12: 345,
94 // 13: -08:00,
95 // 14: -,
96 // 15: 08,
97 // 16: 00
98
99 // Create a Date object from the date parts. Note that the Date
100 // object apparently can't deal with empty string parameters in lieu
101 // of numbers, so optional values (like hours, minutes, seconds, and
102 // milliseconds) must be forced to be numbers.
103 var date = new Date(parts[1], parts[3] - 1, parts[5], parts[7] || 0,
104 parts[8] || 0, parts[10] || 0, parts[12] || 0);
105
106 // We now have a value that the Date object thinks is in the local
107 // timezone but which actually represents the date/time in the
108 // remote timezone (f.e. the value was "10:00 EST", and we have
109 // converted it to "10:00 PST" instead of "07:00 PST"). We need to
110 // correct that. To do so, we're going to add the offset between
111 // the remote timezone and UTC (to convert the value to UTC), then
112 // add the offset between UTC and the local timezone //(to convert
113 // the value to the local timezone).
114
115 // Ironically, W3C-DTF gives us the offset between UTC and the
116 // remote timezone rather than the other way around, while the
117 // getTimezoneOffset() method of a Date object gives us the offset
118 // between the local timezone and UTC rather than the other way
119 // around. Both of these are the additive inverse (i.e. -x for x)
120 // of what we want, so we have to invert them to use them by
121 // multipying by -1 (f.e. if "the offset between UTC and the remote
122 // timezone" is -5 hours, then "the offset between the remote
123 // timezone and UTC" is -5*-1 = 5 hours).
124
125 // Note that if the timezone portion of the date/time string is
126 // absent (which violates W3C-DTF, although ISO 8601 allows it), we
127 // assume the value to be in UTC.
128
129 // The offset between the remote timezone and UTC in milliseconds.
130 var remoteToUTCOffset = 0;
131 if (parts[13] && parts[13] != "Z") {
132 var direction = (parts[14] == "+" ? 1 : -1);
133 if (parts[15])
134 remoteToUTCOffset += direction * parts[15] * HOURS_TO_MILLISECONDS;
135 if (parts[16])
136 remoteToUTCOffset += direction * parts[16] * MINUTES_TO_MILLISECONDS;
137 }
138 remoteToUTCOffset = remoteToUTCOffset * -1; // invert it
139
140 // The offset between UTC and the local timezone in milliseconds.
141 var UTCToLocalOffset = date.getTimezoneOffset() * MINUTES_TO_MILLISECONDS;
142 UTCToLocalOffset = UTCToLocalOffset * -1; // invert it
143 date.setTime(date.getTime() + remoteToUTCOffset + UTCToLocalOffset);
144
145 return date;
146 },
147
ISO8601_create
148 create: function ISO8601_create(aDate) {
zeropad
149 function zeropad (s, l) {
150 s = s.toString(); // force it to a string
151 while (s.length < l) {
152 s = '0' + s;
153 }
154 return s;
155 }
156
157 var myDate;
158 // if d is a number, turn it into a date
159 if (typeof aDate == 'number') {
160 myDate = new Date()
161 myDate.setTime(aDate);
162 } else {
163 myDate = aDate;
164 }
165
166 // YYYY-MM-DDThh:mm:ssZ
167 var result = zeropad(myDate.getUTCFullYear (), 4) +
168 zeropad(myDate.getUTCMonth () + 1, 2) +
169 zeropad(myDate.getUTCDate (), 2) + 'T' +
170 zeropad(myDate.getUTCHours (), 2) + ':' +
171 zeropad(myDate.getUTCMinutes (), 2) + ':' +
172 zeropad(myDate.getUTCSeconds (), 2) + 'Z';
173
174 return result;
175 }
176 }