PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

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