DateUtils.as 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package string
  2. {
  3. public class DateUtils
  4. {
  5. protected static var dateConsts:Object = {
  6. shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  7. longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
  8. shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
  9. longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
  10. }
  11. public static function replace_magic_values( tip:String, xVal:Number):String {
  12. // convert from a unix timestamp to an AS3 date
  13. var as3Date:Date = new Date(xVal * 1000);
  14. tip = tip.replace('#date#', DateUtils.formatDate(as3Date, "Y-m-d"));
  15. // check for user formatted dates
  16. var begPtr:int = tip.indexOf("#date:");
  17. while (begPtr >= 0)
  18. {
  19. var endPtr:int = tip.indexOf("#", begPtr + 1) + 1;
  20. var replaceStr:String = tip.substr(begPtr, endPtr-begPtr);
  21. var timeFmt:String = replaceStr.substr(6, replaceStr.length - 7);
  22. var dateStr:String = DateUtils.formatDate(as3Date, timeFmt);
  23. tip = tip.replace(replaceStr, dateStr);
  24. begPtr = tip.indexOf("#date:");
  25. }
  26. begPtr = tip.indexOf("#gmdate:");
  27. while (begPtr >= 0)
  28. {
  29. endPtr = tip.indexOf("#", begPtr + 1) + 1;
  30. replaceStr = tip.substr(begPtr, endPtr-begPtr);
  31. timeFmt = replaceStr.substr(8, replaceStr.length - 9);
  32. dateStr= DateUtils.formatUTCDate(as3Date, timeFmt);
  33. tip = tip.replace(replaceStr, dateStr);
  34. begPtr = tip.indexOf("#gmdate:");
  35. }
  36. return tip;
  37. }
  38. // Simulates PHP's date function
  39. public static function formatDate( aDate:Date, fmt:String ): String
  40. {
  41. var returnStr:String = '';
  42. for (var i:int = 0; i < fmt.length; i++) {
  43. var curChar:String = fmt.charAt(i);
  44. switch (curChar)
  45. {
  46. // day
  47. case 'd':
  48. returnStr += (aDate.getDate() < 10 ? '0' : '') + aDate.getDate();
  49. break;
  50. case 'D':
  51. returnStr += DateUtils.dateConsts.shortDays[aDate.getDate()];
  52. break;
  53. case 'j':
  54. returnStr += aDate.getDate();
  55. break;
  56. case 'l':
  57. returnStr += DateUtils.dateConsts.longDays[aDate.getDay()];
  58. break;
  59. case 'N':
  60. returnStr += aDate.getDay() + 1;
  61. break;
  62. case 'S':
  63. returnStr += (aDate.getDate() % 10 == 1 && aDate.getDate() != 11 ? 'st' : (aDate.getDate() % 10 == 2 && aDate.getDate() != 12 ? 'nd' : (aDate.getDate() % 10 == 3 && aDate.getDate() != 13 ? 'rd' : 'th')));
  64. break;
  65. case 'w':
  66. returnStr += aDate.getDay();
  67. break;
  68. //z: function() { return "Not Yet Supported"; },
  69. // Week
  70. //W: function() { return "Not Yet Supported"; },
  71. // Month
  72. case 'F':
  73. returnStr += DateUtils.dateConsts.longMonths[aDate.getMonth()];
  74. break;
  75. case 'm':
  76. returnStr += (aDate.getMonth() < 9 ? '0' : '') + (aDate.getMonth() + 1);
  77. break;
  78. case 'M':
  79. returnStr += DateUtils.dateConsts.shortMonths[aDate.getMonth()];
  80. break;
  81. case 'n':
  82. returnStr += aDate.getMonth() + 1;
  83. break;
  84. //t: function() { return "Not Yet Supported"; },
  85. // Year
  86. //L: function() { return "Not Yet Supported"; },
  87. //o: function() { return "Not Supported"; },
  88. case 'Y':
  89. returnStr += aDate.getFullYear();
  90. break;
  91. case 'y':
  92. returnStr += ('' + aDate.getFullYear()).substr(2);
  93. break;
  94. // Time
  95. case 'a':
  96. returnStr += aDate.getHours() < 12 ? 'am' : 'pm';
  97. break;
  98. case 'A':
  99. returnStr += aDate.getHours() < 12 ? 'AM' : 'PM';
  100. break;
  101. //B: function() { return "Not Yet Supported"; },
  102. case 'g':
  103. returnStr += aDate.getHours() == 0 ? 12 : (aDate.getHours() > 12 ? aDate.getHours() - 12 : aDate.getHours());
  104. break;
  105. case 'G':
  106. returnStr += aDate.getHours();
  107. break;
  108. case 'h':
  109. returnStr += (aDate.getHours() < 10 || (12 < aDate.getHours() < 22) ? '0' : '') + (aDate.getHours() < 10 ? aDate.getHours() + 1 : aDate.getHours() - 12);
  110. break;
  111. case 'H':
  112. returnStr += (aDate.getHours() < 10 ? '0' : '') + aDate.getHours();
  113. break;
  114. case 'i':
  115. returnStr += (aDate.getMinutes() < 10 ? '0' : '') + aDate.getMinutes();
  116. break;
  117. case 's':
  118. returnStr += (aDate.getSeconds() < 10 ? '0' : '') + aDate.getSeconds();
  119. break;
  120. // Timezone
  121. //e: function() { return "Not Yet Supported"; },
  122. //I: function() { return "Not Supported"; },
  123. case 'O':
  124. returnStr += (aDate.getTimezoneOffset() < 0 ? '-' : '+') + (aDate.getTimezoneOffset() / 60 < 10 ? '0' : '') + (aDate.getTimezoneOffset() / 60) + '00';
  125. break;
  126. //T: function() { return "Not Yet Supported"; },
  127. case 'Z':
  128. returnStr += aDate.getTimezoneOffset() * 60;
  129. break;
  130. // Full Date/Time
  131. //c: function() { return "Not Yet Supported"; },
  132. case 'r':
  133. returnStr += aDate.toString();
  134. break;
  135. case 'U':
  136. returnStr += aDate.getTime() / 1000;
  137. break;
  138. default:
  139. returnStr += curChar;
  140. }
  141. }
  142. return returnStr;
  143. };
  144. // Simulates PHP's date function
  145. public static function formatUTCDate( aDate:Date, fmt:String ): String
  146. {
  147. var returnStr:String = '';
  148. for (var i:int = 0; i < fmt.length; i++) {
  149. var curChar:String = fmt.charAt(i);
  150. switch (curChar)
  151. {
  152. // day
  153. case 'd':
  154. returnStr += (aDate.getUTCDate() < 10 ? '0' : '') + aDate.getUTCDate();
  155. break;
  156. case 'D':
  157. returnStr += DateUtils.dateConsts.shortDays[aDate.getUTCDate()];
  158. break;
  159. case 'j':
  160. returnStr += aDate.getUTCDate();
  161. break;
  162. case 'l':
  163. returnStr += DateUtils.dateConsts.longDays[aDate.getUTCDay()];
  164. break;
  165. case 'N':
  166. returnStr += aDate.getUTCDay() + 1;
  167. break;
  168. case 'S':
  169. returnStr += (aDate.getUTCDate() % 10 == 1 && aDate.getUTCDate() != 11 ? 'st' : (aDate.getUTCDate() % 10 == 2 && aDate.getUTCDate() != 12 ? 'nd' : (aDate.getUTCDate() % 10 == 3 && aDate.getUTCDate() != 13 ? 'rd' : 'th')));
  170. break;
  171. case 'w':
  172. returnStr += aDate.getUTCDay();
  173. break;
  174. //z: function() { return "Not Yet Supported"; },
  175. // Week
  176. //W: function() { return "Not Yet Supported"; },
  177. // Month
  178. case 'F':
  179. returnStr += DateUtils.dateConsts.longMonths[aDate.getUTCMonth()];
  180. break;
  181. case 'm':
  182. returnStr += (aDate.getUTCMonth() < 9 ? '0' : '') + (aDate.getUTCMonth() + 1);
  183. break;
  184. case 'M':
  185. returnStr += DateUtils.dateConsts.shortMonths[aDate.getUTCMonth()];
  186. break;
  187. case 'n':
  188. returnStr += aDate.getUTCMonth() + 1;
  189. break;
  190. //t: function() { return "Not Yet Supported"; },
  191. // Year
  192. //L: function() { return "Not Yet Supported"; },
  193. //o: function() { return "Not Supported"; },
  194. case 'Y':
  195. returnStr += aDate.getUTCFullYear();
  196. break;
  197. case 'y':
  198. returnStr += ('' + aDate.getUTCFullYear()).substr(2);
  199. break;
  200. // Time
  201. case 'a':
  202. returnStr += aDate.getUTCHours() < 12 ? 'am' : 'pm';
  203. break;
  204. case 'A':
  205. returnStr += aDate.getUTCHours() < 12 ? 'AM' : 'PM';
  206. break;
  207. //B: function() { return "Not Yet Supported"; },
  208. case 'g':
  209. returnStr += aDate.getUTCHours() == 0 ? 12 : (aDate.getUTCHours() > 12 ? aDate.getUTCHours() - 12 : aDate.getHours());
  210. break;
  211. case 'G':
  212. returnStr += aDate.getUTCHours();
  213. break;
  214. case 'h':
  215. returnStr += (aDate.getUTCHours() < 10 || (12 < aDate.getUTCHours() < 22) ? '0' : '') + (aDate.getUTCHours() < 10 ? aDate.getUTCHours() + 1 : aDate.getUTCHours() - 12);
  216. break;
  217. case 'H':
  218. returnStr += (aDate.getUTCHours() < 10 ? '0' : '') + aDate.getUTCHours();
  219. break;
  220. case 'i':
  221. returnStr += (aDate.getUTCMinutes() < 10 ? '0' : '') + aDate.getUTCMinutes();
  222. break;
  223. case 's':
  224. returnStr += (aDate.getUTCSeconds() < 10 ? '0' : '') + aDate.getUTCSeconds();
  225. break;
  226. // Timezone
  227. //e: function() { return "Not Yet Supported"; },
  228. //I: function() { return "Not Supported"; },
  229. case 'O':
  230. returnStr += '+0000';
  231. break;
  232. //T: function() { return "Not Yet Supported"; },
  233. case 'Z':
  234. returnStr += 0;
  235. break;
  236. // Full Date/Time
  237. //c: function() { return "Not Yet Supported"; },
  238. case 'r':
  239. returnStr += aDate.toUTCString();
  240. break;
  241. case 'U':
  242. returnStr += aDate.getTime() / 1000;
  243. break;
  244. default:
  245. returnStr += curChar;
  246. }
  247. }
  248. return returnStr;
  249. };
  250. }
  251. }