/src/name/carter/mark/flex/util/DateUtils.as

http://transcriptstudio4isha.googlecode.com/ · ActionScript · 118 lines · 74 code · 9 blank · 35 comment · 16 complexity · 88a8583b6f4a84710ee740c0decada21 MD5 · raw file

  1. package name.carter.mark.flex.util
  2. {
  3. import mx.formatters.DateBase;
  4. import mx.formatters.DateFormatter;
  5. public class DateUtils
  6. {
  7. public function DateUtils()
  8. {
  9. }
  10. /**
  11. * See: http://safari.oreilly.com/0596004907/actscptckbk-CHP-10-SECT-7
  12. */
  13. public static function parseNonStandardDateString(dateStr:String):Date {
  14. // Create local variables to hold the year, month, date of month, hour, minute, and
  15. // second. Assume that there are no milliseconds in the date string.
  16. var year:int, month:int, dayOfMonth:int, hour:int, minute:int, second:int;
  17. var re:RegExp;
  18. // includes either dd-MM-yy(yy) or dd/MM/yy(yy).
  19. re = /([0-9]{1,2})(?:\/|-)([0-9]{1,2}|[a-zA-Z]{3,})(?:\/|-)([0-9]{2,})/g;
  20. var match:Array = re.exec(dateStr);
  21. if (match != null) {
  22. // Extract the month number and day-of-month values from the date string.
  23. dayOfMonth = new int(match[1]);
  24. if (match[2].length <= 2) {
  25. month = new int(match[2]) - 1;
  26. }
  27. else {
  28. // must be written out - e.g Apr or April
  29. var shortMonths:Array = Utils.arrayToLowerCase(DateBase.monthNamesShort);
  30. var monthStr:String = (match[2] as String).toLowerCase();
  31. var shortIndex:int = shortMonths.indexOf(monthStr);
  32. if (shortIndex >= 0) {
  33. month = shortIndex;
  34. }
  35. else {
  36. var longMonths:Array = Utils.arrayToLowerCase(DateBase.monthNamesLong);
  37. var longIndex:int = longMonths.indexOf(monthStr);
  38. if (longIndex >= 0) {
  39. month = longIndex;
  40. }
  41. else {
  42. // we cannot identify the month so return null
  43. return null;
  44. }
  45. }
  46. }
  47. // If the year value is two characters, then we must add the century to it.
  48. if (match[3].length == 2) {
  49. var twoDigitYear:int = new int(match[3]);
  50. // Assumes that years less than 50 are in the 21st century
  51. year = (twoDigitYear < 50) ? twoDigitYear + 2000 : twoDigitYear + 1900;
  52. } else {
  53. // Extract the four-digit year
  54. year = new int(match[3]);
  55. }
  56. /*
  57. // Check whether the string includes a time value of the form of h(h):mm(:ss).
  58. re = new RegExp("[0-9]{1,2}:[0-9]{2}(:[0-9]{2,})?", "g");
  59. match = re.exec(dateStr);
  60. if (match != null) {
  61. // If the length is 4, the time is given as h:mm. If so, then the length of the
  62. // first part of the time (hours) is only one character. Otherwise, it is two
  63. // characters in length.
  64. var firstLength = 2;
  65. if (match[0].length == 4) {
  66. firstLength = 1;
  67. }
  68. // Extract the hour and minute parts from the date string. If the length of the
  69. // match is greater than five, assume that it includes seconds.
  70. hour = Number(dateStr.substr(match.index, firstLength));
  71. minute = Number(dateStr.substr(match.index + firstLength + 1, 2));
  72. if (match[0].length > 5) {
  73. second = Number(dateStr.substr(match.index + firstLength + 4, 2));
  74. }
  75. }
  76. */
  77. // Return the new date.
  78. return new Date(year, month, dayOfMonth, hour, minute, second);
  79. }
  80. else {
  81. return null;
  82. }
  83. }
  84. private static const START_OF_DAY_DATE_FORMATTER:DateFormatter = createDateFormatter("YYYY-MM-DD");
  85. public static const MILLIS_IN_DAY:Number = 24 * 60 * 60 * 1000;
  86. public static function createDateFormatter(formatString:String):DateFormatter {
  87. var result:DateFormatter = new DateFormatter();
  88. result.formatString = formatString;
  89. return result;
  90. }
  91. public static function parseStandardDateString(dateStr:String):Date {
  92. if (dateStr == null) {
  93. return null;
  94. }
  95. dateStr = dateStr.replace(/-/g, "/");
  96. dateStr = dateStr.replace("T", " ");
  97. dateStr = dateStr.replace("Z", " GMT-0000");
  98. return new Date(Date.parse(dateStr));
  99. }
  100. public static function getStartOfDay(date:Date):Date {
  101. if (date == null) {
  102. return null;
  103. }
  104. var result:Date = DateUtils.parseStandardDateString(START_OF_DAY_DATE_FORMATTER.format(date));
  105. return result;
  106. }
  107. }
  108. }