DateUtil.as 17 KB

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