1 | /*
|
---|
2 | Adobe Systems Incorporated(r) Source Code License Agreement
|
---|
3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
|
---|
4 |
|
---|
5 | Please read this Source Code License Agreement carefully before using
|
---|
6 | the source code.
|
---|
7 |
|
---|
8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive,
|
---|
9 | no-charge, royalty-free, irrevocable copyright license, to reproduce,
|
---|
10 | prepare derivative works of, publicly display, publicly perform, and
|
---|
11 | distribute this source code and such derivative works in source or
|
---|
12 | object code form without any attribution requirements.
|
---|
13 |
|
---|
14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products
|
---|
15 | derived from the source code without prior written permission.
|
---|
16 |
|
---|
17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
|
---|
18 | against any loss, damage, claims or lawsuits, including attorney's
|
---|
19 | fees that arise or result from your use or distribution of the source
|
---|
20 | code.
|
---|
21 |
|
---|
22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
|
---|
23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
|
---|
24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF
|
---|
26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA
|
---|
27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
---|
30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
---|
31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
---|
32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
|
---|
33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
34 | */
|
---|
35 |
|
---|
36 | package com.adobe.utils
|
---|
37 | {
|
---|
38 | import com.adobe.utils.ArrayUtil;
|
---|
39 | import mx.formatters.DateBase;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Class that contains static utility methods for manipulating and working
|
---|
43 | * with Dates.
|
---|
44 | *
|
---|
45 | * @langversion ActionScript 3.0
|
---|
46 | * @playerversion Flash 9.0
|
---|
47 | * @tiptext
|
---|
48 | */
|
---|
49 | public class DateUtil
|
---|
50 | {
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Returns the English Short Month name (3 letters) for the Month that
|
---|
54 | * the Date represents.
|
---|
55 | *
|
---|
56 | * @param d The Date instance whose month will be used to retrieve the
|
---|
57 | * short month name.
|
---|
58 | *
|
---|
59 | * @return An English 3 Letter Month abbreviation.
|
---|
60 | *
|
---|
61 | * @langversion ActionScript 3.0
|
---|
62 | * @playerversion Flash 9.0
|
---|
63 | * @tiptext
|
---|
64 | *
|
---|
65 | * @see SHORT_MONTH
|
---|
66 | */
|
---|
67 | public static function getShortMonthName(d:Date):String
|
---|
68 | {
|
---|
69 | return DateBase.monthNamesShort[d.getMonth()];
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Returns the index of the month that the short month name string
|
---|
74 | * represents.
|
---|
75 | *
|
---|
76 | * @param m The 3 letter abbreviation representing a short month name.
|
---|
77 | *
|
---|
78 | * @param Optional parameter indicating whether the search should be case
|
---|
79 | * sensitive
|
---|
80 | *
|
---|
81 | * @return A int that represents that month represented by the specifed
|
---|
82 | * short name.
|
---|
83 | *
|
---|
84 | * @langversion ActionScript 3.0
|
---|
85 | * @playerversion Flash 9.0
|
---|
86 | * @tiptext
|
---|
87 | *
|
---|
88 | * @see SHORT_MONTH
|
---|
89 | */
|
---|
90 | public static function getShortMonthIndex(m:String):int
|
---|
91 | {
|
---|
92 | return DateBase.monthNamesShort.indexOf(m);
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Returns the English full Month name for the Month that
|
---|
97 | * the Date represents.
|
---|
98 | *
|
---|
99 | * @param d The Date instance whose month will be used to retrieve the
|
---|
100 | * full month name.
|
---|
101 | *
|
---|
102 | * @return An English full month name.
|
---|
103 | *
|
---|
104 | * @langversion ActionScript 3.0
|
---|
105 | * @playerversion Flash 9.0
|
---|
106 | * @tiptext
|
---|
107 | *
|
---|
108 | * @see FULL_MONTH
|
---|
109 | */
|
---|
110 | public static function getFullMonthName(d:Date):String
|
---|
111 | {
|
---|
112 | return DateBase.monthNamesLong[d.getMonth()];
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Returns the index of the month that the full month name string
|
---|
117 | * represents.
|
---|
118 | *
|
---|
119 | * @param m A full month name.
|
---|
120 | *
|
---|
121 | * @return A int that represents that month represented by the specifed
|
---|
122 | * full month name.
|
---|
123 | *
|
---|
124 | * @langversion ActionScript 3.0
|
---|
125 | * @playerversion Flash 9.0
|
---|
126 | * @tiptext
|
---|
127 | *
|
---|
128 | * @see FULL_MONTH
|
---|
129 | */
|
---|
130 | public static function getFullMonthIndex(m:String):int
|
---|
131 | {
|
---|
132 | return DateBase.monthNamesLong.indexOf(m);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Returns the English Short Day name (3 letters) for the day that
|
---|
137 | * the Date represents.
|
---|
138 | *
|
---|
139 | * @param d The Date instance whose day will be used to retrieve the
|
---|
140 | * short day name.
|
---|
141 | *
|
---|
142 | * @return An English 3 Letter day abbreviation.
|
---|
143 | *
|
---|
144 | * @langversion ActionScript 3.0
|
---|
145 | * @playerversion Flash 9.0
|
---|
146 | * @tiptext
|
---|
147 | *
|
---|
148 | * @see SHORT_DAY
|
---|
149 | */
|
---|
150 | public static function getShortDayName(d:Date):String
|
---|
151 | {
|
---|
152 | return DateBase.dayNamesShort[d.getDay()];
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Returns the index of the day that the short day name string
|
---|
157 | * represents.
|
---|
158 | *
|
---|
159 | * @param m A short day name.
|
---|
160 | *
|
---|
161 | * @return A int that represents that short day represented by the specifed
|
---|
162 | * full month name.
|
---|
163 | *
|
---|
164 | * @langversion ActionScript 3.0
|
---|
165 | * @playerversion Flash 9.0
|
---|
166 | * @tiptext
|
---|
167 | *
|
---|
168 | * @see SHORT_DAY
|
---|
169 | */
|
---|
170 | public static function getShortDayIndex(d:String):int
|
---|
171 | {
|
---|
172 | return DateBase.dayNamesShort.indexOf(d);
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Returns the English full day name for the day that
|
---|
177 | * the Date represents.
|
---|
178 | *
|
---|
179 | * @param d The Date instance whose day will be used to retrieve the
|
---|
180 | * full day name.
|
---|
181 | *
|
---|
182 | * @return An English full day name.
|
---|
183 | *
|
---|
184 | * @langversion ActionScript 3.0
|
---|
185 | * @playerversion Flash 9.0
|
---|
186 | * @tiptext
|
---|
187 | *
|
---|
188 | * @see FULL_DAY
|
---|
189 | */
|
---|
190 | public static function getFullDayName(d:Date):String
|
---|
191 | {
|
---|
192 | return DateBase.dayNamesLong[d.getDay()];
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Returns the index of the day that the full day name string
|
---|
197 | * represents.
|
---|
198 | *
|
---|
199 | * @param m A full day name.
|
---|
200 | *
|
---|
201 | * @return A int that represents that full day represented by the specifed
|
---|
202 | * full month name.
|
---|
203 | *
|
---|
204 | * @langversion ActionScript 3.0
|
---|
205 | * @playerversion Flash 9.0
|
---|
206 | * @tiptext
|
---|
207 | *
|
---|
208 | * @see FULL_DAY
|
---|
209 | */
|
---|
210 | public static function getFullDayIndex(d:String):int
|
---|
211 | {
|
---|
212 | return DateBase.dayNamesLong.indexOf(d);
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Returns a two digit representation of the year represented by the
|
---|
217 | * specified date.
|
---|
218 | *
|
---|
219 | * @param d The Date instance whose year will be used to generate a two
|
---|
220 | * digit string representation of the year.
|
---|
221 | *
|
---|
222 | * @return A string that contains a 2 digit representation of the year.
|
---|
223 | * Single digits will be padded with 0.
|
---|
224 | *
|
---|
225 | * @langversion ActionScript 3.0
|
---|
226 | * @playerversion Flash 9.0
|
---|
227 | * @tiptext
|
---|
228 | */
|
---|
229 | public static function getShortYear(d:Date):String
|
---|
230 | {
|
---|
231 | var dStr:String = String(d.getFullYear());
|
---|
232 |
|
---|
233 | if(dStr.length < 3)
|
---|
234 | {
|
---|
235 | return dStr;
|
---|
236 | }
|
---|
237 |
|
---|
238 | return (dStr.substr(dStr.length - 2));
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Compares two dates and returns an integer depending on their relationship.
|
---|
243 | *
|
---|
244 | * Returns -1 if d1 is greater than d2.
|
---|
245 | * Returns 1 if d2 is greater than d1.
|
---|
246 | * Returns 0 if both dates are equal.
|
---|
247 | *
|
---|
248 | * @param d1 The date that will be compared to the second date.
|
---|
249 | * @param d2 The date that will be compared to the first date.
|
---|
250 | *
|
---|
251 | * @return An int indicating how the two dates compare.
|
---|
252 | *
|
---|
253 | * @langversion ActionScript 3.0
|
---|
254 | * @playerversion Flash 9.0
|
---|
255 | * @tiptext
|
---|
256 | */
|
---|
257 | public static function compareDates(d1:Date, d2:Date):int
|
---|
258 | {
|
---|
259 | var d1ms:Number = d1.getTime();
|
---|
260 | var d2ms:Number = d2.getTime();
|
---|
261 |
|
---|
262 | if(d1ms > d2ms)
|
---|
263 | {
|
---|
264 | return -1;
|
---|
265 | }
|
---|
266 | else if(d1ms < d2ms)
|
---|
267 | {
|
---|
268 | return 1;
|
---|
269 | }
|
---|
270 | else
|
---|
271 | {
|
---|
272 | return 0;
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Returns a short hour (0 - 12) represented by the specified date.
|
---|
278 | *
|
---|
279 | * If the hour is less than 12 (0 - 11 AM) then the hour will be returned.
|
---|
280 | *
|
---|
281 | * If the hour is greater than 12 (12 - 23 PM) then the hour minus 12
|
---|
282 | * will be returned.
|
---|
283 | *
|
---|
284 | * @param d1 The Date from which to generate the short hour
|
---|
285 | *
|
---|
286 | * @return An int between 0 and 13 ( 1 - 12 ) representing the short hour.
|
---|
287 | *
|
---|
288 | * @langversion ActionScript 3.0
|
---|
289 | * @playerversion Flash 9.0
|
---|
290 | * @tiptext
|
---|
291 | */
|
---|
292 | public static function getShortHour(d:Date):int
|
---|
293 | {
|
---|
294 | var h:int = d.hours;
|
---|
295 |
|
---|
296 | if(h == 0 || h == 12)
|
---|
297 | {
|
---|
298 | return 12;
|
---|
299 | }
|
---|
300 | else if(h > 12)
|
---|
301 | {
|
---|
302 | return h - 12;
|
---|
303 | }
|
---|
304 | else
|
---|
305 | {
|
---|
306 | return h;
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Returns a string indicating whether the date represents a time in the
|
---|
312 | * ante meridiem (AM) or post meridiem (PM).
|
---|
313 | *
|
---|
314 | * If the hour is less than 12 then "AM" will be returned.
|
---|
315 | *
|
---|
316 | * If the hour is greater than 12 then "PM" will be returned.
|
---|
317 | *
|
---|
318 | * @param d1 The Date from which to generate the 12 hour clock indicator.
|
---|
319 | *
|
---|
320 | * @return A String ("AM" or "PM") indicating which half of the day the
|
---|
321 | * hour represents.
|
---|
322 | *
|
---|
323 | * @langversion ActionScript 3.0
|
---|
324 | * @playerversion Flash 9.0
|
---|
325 | * @tiptext
|
---|
326 | */
|
---|
327 | public static function getAMPM(d:Date):String
|
---|
328 | {
|
---|
329 | return (d.hours > 11)? "PM" : "AM";
|
---|
330 | }
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * Parses dates that conform to RFC822 into Date objects. This method also
|
---|
334 | * supports four-digit years (not supported in RFC822), but two-digit years
|
---|
335 | * (referring to the 20th century) are fine, too.
|
---|
336 | *
|
---|
337 | * This function is useful for parsing RSS .91, .92, and 2.0 dates.
|
---|
338 | *
|
---|
339 | * @param str
|
---|
340 | *
|
---|
341 | * @returns
|
---|
342 | *
|
---|
343 | * @langversion ActionScript 3.0
|
---|
344 | * @playerversion Flash 9.0
|
---|
345 | * @tiptext
|
---|
346 | *
|
---|
347 | * @see http://asg.web.cmu.edu/rfc/rfc822.html
|
---|
348 | */
|
---|
349 | public static function parseRFC822(str:String):Date
|
---|
350 | {
|
---|
351 | var finalDate:Date;
|
---|
352 | try
|
---|
353 | {
|
---|
354 | var dateParts:Array = str.split(" ");
|
---|
355 | var day:String = null;
|
---|
356 |
|
---|
357 | if (dateParts[0].search(/\d/) == -1)
|
---|
358 | {
|
---|
359 | day = dateParts.shift().replace(/\W/, "");
|
---|
360 | }
|
---|
361 |
|
---|
362 | var date:Number = Number(dateParts.shift());
|
---|
363 | var month:Number = Number(DateUtil.getShortMonthIndex(dateParts.shift()));
|
---|
364 | var year:Number = Number(dateParts.shift());
|
---|
365 | var timeParts:Array = dateParts.shift().split(":");
|
---|
366 | var hour:Number = int(timeParts.shift());
|
---|
367 | var minute:Number = int(timeParts.shift());
|
---|
368 | var second:Number = (timeParts.length > 0) ? int(timeParts.shift()): 0;
|
---|
369 |
|
---|
370 | var milliseconds:Number = Date.UTC(year, month, date, hour, minute, second, 0);
|
---|
371 |
|
---|
372 | var timezone:String = dateParts.shift();
|
---|
373 | var offset:Number = 0;
|
---|
374 |
|
---|
375 | if (timezone.search(/\d/) == -1)
|
---|
376 | {
|
---|
377 | switch(timezone)
|
---|
378 | {
|
---|
379 | case "UT":
|
---|
380 | offset = 0;
|
---|
381 | break;
|
---|
382 | case "UTC":
|
---|
383 | offset = 0;
|
---|
384 | break;
|
---|
385 | case "GMT":
|
---|
386 | offset = 0;
|
---|
387 | break;
|
---|
388 | case "EST":
|
---|
389 | offset = (-5 * 3600000);
|
---|
390 | break;
|
---|
391 | case "EDT":
|
---|
392 | offset = (-4 * 3600000);
|
---|
393 | break;
|
---|
394 | case "CST":
|
---|
395 | offset = (-6 * 3600000);
|
---|
396 | break;
|
---|
397 | case "CDT":
|
---|
398 | offset = (-5 * 3600000);
|
---|
399 | break;
|
---|
400 | case "MST":
|
---|
401 | offset = (-7 * 3600000);
|
---|
402 | break;
|
---|
403 | case "MDT":
|
---|
404 | offset = (-6 * 3600000);
|
---|
405 | break;
|
---|
406 | case "PST":
|
---|
407 | offset = (-8 * 3600000);
|
---|
408 | break;
|
---|
409 | case "PDT":
|
---|
410 | offset = (-7 * 3600000);
|
---|
411 | break;
|
---|
412 | case "Z":
|
---|
413 | offset = 0;
|
---|
414 | break;
|
---|
415 | case "A":
|
---|
416 | offset = (-1 * 3600000);
|
---|
417 | break;
|
---|
418 | case "M":
|
---|
419 | offset = (-12 * 3600000);
|
---|
420 | break;
|
---|
421 | case "N":
|
---|
422 | offset = (1 * 3600000);
|
---|
423 | break;
|
---|
424 | case "Y":
|
---|
425 | offset = (12 * 3600000);
|
---|
426 | break;
|
---|
427 | default:
|
---|
428 | offset = 0;
|
---|
429 | }
|
---|
430 | }
|
---|
431 | else
|
---|
432 | {
|
---|
433 | var multiplier:Number = 1;
|
---|
434 | var oHours:Number = 0;
|
---|
435 | var oMinutes:Number = 0;
|
---|
436 | if (timezone.length != 4)
|
---|
437 | {
|
---|
438 | if (timezone.charAt(0) == "-")
|
---|
439 | {
|
---|
440 | multiplier = -1;
|
---|
441 | }
|
---|
442 | timezone = timezone.substr(1, 4);
|
---|
443 | }
|
---|
444 | oHours = Number(timezone.substr(0, 2));
|
---|
445 | oMinutes = Number(timezone.substr(2, 2));
|
---|
446 | offset = (((oHours * 3600000) + (oMinutes * 60000)) * multiplier);
|
---|
447 | }
|
---|
448 |
|
---|
449 | finalDate = new Date(milliseconds - offset);
|
---|
450 |
|
---|
451 | if (finalDate.toString() == "Invalid Date")
|
---|
452 | {
|
---|
453 | throw new Error("This date does not conform to RFC822.");
|
---|
454 | }
|
---|
455 | }
|
---|
456 | catch (e:Error)
|
---|
457 | {
|
---|
458 | var eStr:String = "Unable to parse the string [" +str+ "] into a date. ";
|
---|
459 | eStr += "The internal error was: " + e.toString();
|
---|
460 | throw new Error(eStr);
|
---|
461 | }
|
---|
462 | return finalDate;
|
---|
463 | }
|
---|
464 |
|
---|
465 | /**
|
---|
466 | * Returns a date string formatted according to RFC822.
|
---|
467 | *
|
---|
468 | * @param d
|
---|
469 | *
|
---|
470 | * @returns
|
---|
471 | *
|
---|
472 | * @langversion ActionScript 3.0
|
---|
473 | * @playerversion Flash 9.0
|
---|
474 | * @tiptext
|
---|
475 | *
|
---|
476 | * @see http://asg.web.cmu.edu/rfc/rfc822.html
|
---|
477 | */
|
---|
478 | public static function toRFC822(d:Date):String
|
---|
479 | {
|
---|
480 | var date:Number = d.getUTCDate();
|
---|
481 | var hours:Number = d.getUTCHours();
|
---|
482 | var minutes:Number = d.getUTCMinutes();
|
---|
483 | var seconds:Number = d.getUTCSeconds();
|
---|
484 | var sb:String = new String();
|
---|
485 | sb += DateBase.dayNamesShort[d.getUTCDay()];
|
---|
486 | sb += ", ";
|
---|
487 |
|
---|
488 | if (date < 10)
|
---|
489 | {
|
---|
490 | sb += "0";
|
---|
491 | }
|
---|
492 | sb += date;
|
---|
493 | sb += " ";
|
---|
494 | //sb += DateUtil.SHORT_MONTH[d.getUTCMonth()];
|
---|
495 | sb += DateBase.monthNamesShort[d.getUTCMonth()];
|
---|
496 | sb += " ";
|
---|
497 | sb += d.getUTCFullYear();
|
---|
498 | sb += " ";
|
---|
499 | if (hours < 10)
|
---|
500 | {
|
---|
501 | sb += "0";
|
---|
502 | }
|
---|
503 | sb += hours;
|
---|
504 | sb += ":";
|
---|
505 | if (minutes < 10)
|
---|
506 | {
|
---|
507 | sb += "0";
|
---|
508 | }
|
---|
509 | sb += minutes;
|
---|
510 | sb += ":";
|
---|
511 | if (seconds < 10)
|
---|
512 | {
|
---|
513 | sb += "0";
|
---|
514 | }
|
---|
515 | sb += seconds;
|
---|
516 | sb += " GMT";
|
---|
517 | return sb;
|
---|
518 | }
|
---|
519 |
|
---|
520 | /**
|
---|
521 | * Parses dates that conform to the W3C Date-time Format into Date objects.
|
---|
522 | *
|
---|
523 | * This function is useful for parsing RSS 1.0 and Atom 1.0 dates.
|
---|
524 | *
|
---|
525 | * @param str
|
---|
526 | *
|
---|
527 | * @returns
|
---|
528 | *
|
---|
529 | * @langversion ActionScript 3.0
|
---|
530 | * @playerversion Flash 9.0
|
---|
531 | * @tiptext
|
---|
532 | *
|
---|
533 | * @see http://www.w3.org/TR/NOTE-datetime
|
---|
534 | */
|
---|
535 | public static function parseW3CDTF(str:String):Date
|
---|
536 | {
|
---|
537 | var finalDate:Date;
|
---|
538 | try
|
---|
539 | {
|
---|
540 | var dateStr:String = str.substring(0, str.indexOf("T"));
|
---|
541 | var timeStr:String = str.substring(str.indexOf("T")+1, str.length);
|
---|
542 | var dateArr:Array = dateStr.split("-");
|
---|
543 | var year:Number = Number(dateArr.shift());
|
---|
544 | var month:Number = Number(dateArr.shift());
|
---|
545 | var date:Number = Number(dateArr.shift());
|
---|
546 |
|
---|
547 | var multiplier:Number;
|
---|
548 | var offsetHours:Number;
|
---|
549 | var offsetMinutes:Number;
|
---|
550 | var offsetStr:String;
|
---|
551 |
|
---|
552 | if (timeStr.indexOf("Z") != -1)
|
---|
553 | {
|
---|
554 | multiplier = 1;
|
---|
555 | offsetHours = 0;
|
---|
556 | offsetMinutes = 0;
|
---|
557 | timeStr = timeStr.replace("Z", "");
|
---|
558 | }
|
---|
559 | else if (timeStr.indexOf("+") != -1)
|
---|
560 | {
|
---|
561 | multiplier = 1;
|
---|
562 | offsetStr = timeStr.substring(timeStr.indexOf("+")+1, timeStr.length);
|
---|
563 | offsetHours = Number(offsetStr.substring(0, offsetStr.indexOf(":")));
|
---|
564 | offsetMinutes = Number(offsetStr.substring(offsetStr.indexOf(":")+1, offsetStr.length));
|
---|
565 | timeStr = timeStr.substring(0, timeStr.indexOf("+"));
|
---|
566 | }
|
---|
567 | else // offset is -
|
---|
568 | {
|
---|
569 | multiplier = -1;
|
---|
570 | offsetStr = timeStr.substring(timeStr.indexOf("-")+1, timeStr.length);
|
---|
571 | offsetHours = Number(offsetStr.substring(0, offsetStr.indexOf(":")));
|
---|
572 | offsetMinutes = Number(offsetStr.substring(offsetStr.indexOf(":")+1, offsetStr.length));
|
---|
573 | timeStr = timeStr.substring(0, timeStr.indexOf("-"));
|
---|
574 | }
|
---|
575 | var timeArr:Array = timeStr.split(":");
|
---|
576 | var hour:Number = Number(timeArr.shift());
|
---|
577 | var minutes:Number = Number(timeArr.shift());
|
---|
578 | var secondsArr:Array = (timeArr.length > 0) ? String(timeArr.shift()).split(".") : null;
|
---|
579 | var seconds:Number = (secondsArr != null && secondsArr.length > 0) ? Number(secondsArr.shift()) : 0;
|
---|
580 | var milliseconds:Number = (secondsArr != null && secondsArr.length > 0) ? Number(secondsArr.shift()) : 0;
|
---|
581 | var utc:Number = Date.UTC(year, month-1, date, hour, minutes, seconds, milliseconds);
|
---|
582 | var offset:Number = (((offsetHours * 3600000) + (offsetMinutes * 60000)) * multiplier);
|
---|
583 | finalDate = new Date(utc - offset);
|
---|
584 |
|
---|
585 | if (finalDate.toString() == "Invalid Date")
|
---|
586 | {
|
---|
587 | throw new Error("This date does not conform to W3CDTF.");
|
---|
588 | }
|
---|
589 | }
|
---|
590 | catch (e:Error)
|
---|
591 | {
|
---|
592 | var eStr:String = "Unable to parse the string [" +str+ "] into a date. ";
|
---|
593 | eStr += "The internal error was: " + e.toString();
|
---|
594 | throw new Error(eStr);
|
---|
595 | }
|
---|
596 | return finalDate;
|
---|
597 | }
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * Returns a date string formatted according to W3CDTF.
|
---|
601 | *
|
---|
602 | * @param d
|
---|
603 | * @param includeMilliseconds Determines whether to include the
|
---|
604 | * milliseconds value (if any) in the formatted string.
|
---|
605 | *
|
---|
606 | * @returns
|
---|
607 | *
|
---|
608 | * @langversion ActionScript 3.0
|
---|
609 | * @playerversion Flash 9.0
|
---|
610 | * @tiptext
|
---|
611 | *
|
---|
612 | * @see http://www.w3.org/TR/NOTE-datetime
|
---|
613 | */
|
---|
614 | public static function toW3CDTF(d:Date,includeMilliseconds:Boolean=false):String
|
---|
615 | {
|
---|
616 | var date:Number = d.getUTCDate();
|
---|
617 | var month:Number = d.getUTCMonth();
|
---|
618 | var hours:Number = d.getUTCHours();
|
---|
619 | var minutes:Number = d.getUTCMinutes();
|
---|
620 | var seconds:Number = d.getUTCSeconds();
|
---|
621 | var milliseconds:Number = d.getUTCMilliseconds();
|
---|
622 | var sb:String = new String();
|
---|
623 |
|
---|
624 | sb += d.getUTCFullYear();
|
---|
625 | sb += "-";
|
---|
626 |
|
---|
627 | //thanks to "dom" who sent in a fix for the line below
|
---|
628 | if (month + 1 < 10)
|
---|
629 | {
|
---|
630 | sb += "0";
|
---|
631 | }
|
---|
632 | sb += month + 1;
|
---|
633 | sb += "-";
|
---|
634 | if (date < 10)
|
---|
635 | {
|
---|
636 | sb += "0";
|
---|
637 | }
|
---|
638 | sb += date;
|
---|
639 | sb += "T";
|
---|
640 | if (hours < 10)
|
---|
641 | {
|
---|
642 | sb += "0";
|
---|
643 | }
|
---|
644 | sb += hours;
|
---|
645 | sb += ":";
|
---|
646 | if (minutes < 10)
|
---|
647 | {
|
---|
648 | sb += "0";
|
---|
649 | }
|
---|
650 | sb += minutes;
|
---|
651 | sb += ":";
|
---|
652 | if (seconds < 10)
|
---|
653 | {
|
---|
654 | sb += "0";
|
---|
655 | }
|
---|
656 | sb += seconds;
|
---|
657 | if (includeMilliseconds && milliseconds > 0)
|
---|
658 | {
|
---|
659 | sb += ".";
|
---|
660 | sb += milliseconds;
|
---|
661 | }
|
---|
662 | sb += "-00:00";
|
---|
663 | return sb;
|
---|
664 | }
|
---|
665 | }
|
---|
666 | }
|
---|