diff --git a/src/mesh/core/date/DateTime.as b/src/mesh/core/date/DateTime.as index a8a1c9c..782299d 100644 --- a/src/mesh/core/date/DateTime.as +++ b/src/mesh/core/date/DateTime.as @@ -7,11 +7,20 @@ package mesh.core.date */ public class DateTime extends MDate { - private var _date:Date; - /** * Constructor. * + * @param epochTime The number of milliseconds since midnight on January 1, 1970 UTC. + */ + public function DateTime(epochTime:Number = 0, offset:Number = 0) + { + super(epochTime); + _offset = offset; + } + + /** + * Creates a new date time. + * * @param year The year. * @param month The month of the year, between 1 and 12. * @param day The day of the month. @@ -20,25 +29,17 @@ package mesh.core.date * @param second The second of the minute * @param millisecond The millisecond in the second. * @param offset The timezone offset in minutes. + * @return A new date time. */ - public function DateTime(year:int, month:int = 1, day:int = 1, hour:int = 0, minute:int = 0, second:int = 0, millisecond:int = 0, offset:Number = 0) + public static function create(year:int, month:int = 1, day:int = 1, hour:int = 0, minute:int = 0, second:int = 0, millisecond:int = 0, offset:Number = 0):DateTime { - _date = new Date(Date.UTC(year, month-1, day-1, hour, minute, second, millisecond) + (offset * 60000)); - super(_date.fullYear, _date.month+1, _date.date); + var d:Date = new Date(year, month-1, day, hour, minute, second, millisecond); } public static function now():DateTime { var date:Date = new Date(); - return new DateTime(date.fullYearUTC, date.monthUTC+1, date.dateUTC+1, date.hoursUTC, date.minutesUTC, date.secondsUTC, date.millisecondsUTC, date.timezoneOffset); - } - - /** - * @inheritDoc - */ - override public function toDate():Date - { - return new Date(_date.time); + return new DateTime(date.fullYear, date.month-1, date.date, date.hours, date.minutes, date.seconds, date.milliseconds, -date.timezoneOffset); } /** @@ -46,31 +47,48 @@ package mesh.core.date */ public function get hour():int { - return _date.hours; + return date.hours; } /** * The minutes in the hour, between 0 and 59. */ - public function get minutes():int + public function get minute():int { - return _date.minutes; + return date.minutes; } + private var _offset:int; /** * The time zone offset in minutes. */ public function get offset():int { - return _date.timezoneOffset; + return _offset; + } + + /** + * The seconds in the minute, between 0 and 59. + */ + public function get second():int + { + return date.seconds; + } + + /** + * The millisecond in the second. + */ + public function get millisecond():int + { + return date.milliseconds; } /** - * The seconds in the hour, between 0 and 59. + * true if this date time has an offset of 0. */ - public function get seconds():int + public function get isUTC():Boolean { - return _date.seconds; + return offset == 0; } } } \ No newline at end of file diff --git a/src/mesh/core/date/MDate.as b/src/mesh/core/date/MDate.as index 8024828..7257a20 100644 --- a/src/mesh/core/date/MDate.as +++ b/src/mesh/core/date/MDate.as @@ -26,7 +26,15 @@ package mesh.core.date */ public class MDate { - private var _date:Date; + /** + * Constructor. + * + * @param epochTime The number of milliseconds since midnight on January 1, 1970 UTC. + */ + public function MDate(epochTime:Number = 0, offset:Number = 0) + { + _date = new Date(epochTime); + } /** * Constructor. @@ -35,9 +43,10 @@ package mesh.core.date * @param month The month in the year. * @param day The day in the month. */ - public function MDate(year:int, month:int = 1, day:int = 1) + public static function create(year:int, month:int = 1, day:int = 1):MDate { - _date = new Date(year, month-1, day); + var d:Date = new Date(year, month-1, day); + return new MDate(d.time); } /** @@ -47,8 +56,7 @@ package mesh.core.date */ public static function today():MDate { - var d:Date = new Date(); - return new MDate(d.fullYear, d.month+1, d.date); + return new MDate(new Date().time); } /** @@ -58,8 +66,7 @@ package mesh.core.date */ public static function yesterday():MDate { - var d:Date = new Date(); - return new MDate(d.fullYear, d.month+1, d.date-1); + return today().prevDay(); } /** @@ -69,8 +76,7 @@ package mesh.core.date */ public static function tomorrow():MDate { - var d:Date = new Date(); - return new MDate(d.fullYear, d.month+1, d.date+1); + return today().nextDay(); } /** @@ -89,14 +95,14 @@ package mesh.core.date { if (obj is String) { try { - return newInstance.apply(null, [MDate].concat(obj.split("-"))); + return MDate.create.apply(null, obj.split("-")); } catch (e:Error) { } } if (obj is Date) { - return new MDate(obj.fullYear, obj.month+1, obj.date); + return new MDate(obj.time); } throw new ArgumentError(clazz(MDate) + ".parse() cannot parse " + obj); @@ -115,11 +121,11 @@ package mesh.core.date * @param options The options hash. * @return A new date. */ - public function change(options:Object):MDate + public function change(options:Object):* { - return new MDate(options.hasOwnProperty("year") ? options.year : year, - options.hasOwnProperty("month") ? options.month : month, - options.hasOwnProperty("day") ? options.day : day); + return MDate.create(options.hasOwnProperty("year") ? options.year : year, + options.hasOwnProperty("month") ? options.month : month, + options.hasOwnProperty("day") ? options.day : day); } /** @@ -200,7 +206,7 @@ package mesh.core.date * @param n The number of days to subtract. * @return The previous n days as a new date. */ - public function daysAgo(n:int = 1):MDate + public function daysAgo(n:int = 1):* { return daysSince(-n); } @@ -212,9 +218,9 @@ package mesh.core.date * @param n The number of days to add. * @return The next n days as a new date. */ - public function daysSince(n:int = 1):MDate + public function daysSince(n:int = 1):* { - return new MDate(year, month, day+n); + return newInstance(clazz(this), valueOf() + (n * 86400000)); } /** @@ -222,7 +228,7 @@ package mesh.core.date * * @return The day before this date. */ - public function prevDay():MDate + public function prevDay():* { return daysAgo(1); } @@ -232,7 +238,7 @@ package mesh.core.date * * @return The day after this date. */ - public function nextDay():MDate + public function nextDay():* { return daysSince(1); } @@ -244,7 +250,7 @@ package mesh.core.date * @param n The number of months to subtract. * @return The previous n months as a new date. */ - public function monthsAgo(n:int = 1):MDate + public function monthsAgo(n:int = 1):* { return monthsSince(-n); } @@ -256,14 +262,14 @@ package mesh.core.date * @param n The number of months to subtract. * @return The next n months as a new date. */ - public function monthsSince(n:int = 1):MDate + public function monthsSince(n:int = 1):* { var m:int = month-1+n; var tempDate:Date = new Date(year, m, day); while (tempDate.month != m) { tempDate.date--; } - return new MDate(tempDate.fullYear, tempDate.month+1, tempDate.date); + return newInstance(clazz(this), tempDate.time); } /** @@ -271,7 +277,7 @@ package mesh.core.date * * @return The month before this date. */ - public function prevMonth():MDate + public function prevMonth():* { return monthsAgo(1); } @@ -281,7 +287,7 @@ package mesh.core.date * * @return The month after this date. */ - public function nextMonth():MDate + public function nextMonth():* { return monthsSince(1); } @@ -293,7 +299,7 @@ package mesh.core.date * @param n The number of years to subtract. * @return The previous n years as a new date. */ - public function yearsAgo(n:int = 1):MDate + public function yearsAgo(n:int = 1):* { return yearsSince(-n); } @@ -305,14 +311,14 @@ package mesh.core.date * @param n The number of years to add. * @return The next n years as a new date. */ - public function yearsSince(n:int = 1):MDate + public function yearsSince(n:int = 1):* { var m:int = month-1; var tempDate:Date = new Date(year+n, m, day); while (tempDate.month != m) { tempDate.date--; } - return new MDate(tempDate.fullYear, tempDate.month+1, tempDate.date); + return newInstance(clazz(this), tempDate.time); } /** @@ -320,7 +326,7 @@ package mesh.core.date * * @return The year before this date. */ - public function prevYear():MDate + public function prevYear():* { return yearsAgo(1); } @@ -330,7 +336,7 @@ package mesh.core.date * * @return The year after this date. */ - public function nextYear():MDate + public function nextYear():* { return yearsSince(1); } @@ -386,6 +392,15 @@ package mesh.core.date return _date.time; } + private var _date:Date; + /** + * The internal Date used to store this date's values. + */ + protected function get date():Date + { + return _date; + } + /** * The day of the month. */ diff --git a/tests/mesh/core/date/DateTimeTests.as b/tests/mesh/core/date/DateTimeTests.as new file mode 100644 index 0000000..59a5eda --- /dev/null +++ b/tests/mesh/core/date/DateTimeTests.as @@ -0,0 +1,20 @@ +package mesh.core.date +{ + import org.flexunit.assertThat; + import org.hamcrest.object.equalTo; + + public class DateTimeTests + { + public function setup():void + { + + } + + [Test] + public function testDateUTC():void + { + var now:Date = new Date(Date.UTC(2011, 1, 28) + (480*60000)); + trace(now); + } + } +} \ No newline at end of file diff --git a/tests/mesh/core/date/MDateTests.as b/tests/mesh/core/date/MDateTests.as index 52b2445..1c1cdaa 100644 --- a/tests/mesh/core/date/MDateTests.as +++ b/tests/mesh/core/date/MDateTests.as @@ -14,10 +14,10 @@ package mesh.core.date [Test] public function testChange():void { - var today:MDate = new MDate(2011, 1, 1); + var today:MDate = MDate.create(2011, 1, 1); - assertThat(today.change({month:2, day:5}).equals(new MDate(2011, 2, 5)), equalTo(true)); - assertThat(today.change({year:2012, month:3, day:10}).equals(new MDate(2012, 3, 10)), equalTo(true)); + assertThat(today.change({month:2, day:5}).equals(MDate.create(2011, 2, 5)), equalTo(true)); + assertThat(today.change({year:2012, month:3, day:10}).equals(MDate.create(2012, 3, 10)), equalTo(true)); } [Test] @@ -49,91 +49,91 @@ package mesh.core.date [Test] public function testDaysAgo():void { - assertThat(new MDate(2011, 1, 1).daysAgo(1).equals(new MDate(2010, 12, 31)), equalTo(true)); - assertThat(new MDate(2011, 1, 1).daysAgo(2).equals(new MDate(2010, 12, 30)), equalTo(true)); + assertThat(MDate.create(2011, 1, 1).daysAgo(1).equals(MDate.create(2010, 12, 31)), equalTo(true)); + assertThat(MDate.create(2011, 1, 1).daysAgo(2).equals(MDate.create(2010, 12, 30)), equalTo(true)); } [Test] public function testDaysSince():void { - assertThat(new MDate(2010, 12, 31).daysSince(1).equals(new MDate(2011, 1, 1)), equalTo(true)); - assertThat(new MDate(2010, 12, 31).daysSince(2).equals(new MDate(2011, 1, 2)), equalTo(true)); + assertThat(MDate.create(2010, 12, 31).daysSince(1).equals(MDate.create(2011, 1, 1)), equalTo(true)); + assertThat(MDate.create(2010, 12, 31).daysSince(2).equals(MDate.create(2011, 1, 2)), equalTo(true)); } [Test] public function testPrevDay():void { - assertThat(new MDate(2011, 1, 1).prevDay().equals(new MDate(2010, 12, 31)), equalTo(true)); + assertThat(MDate.create(2011, 1, 1).prevDay().equals(MDate.create(2010, 12, 31)), equalTo(true)); } [Test] public function testNextDay():void { - assertThat(new MDate(2010, 12, 31).nextDay().equals(new MDate(2011, 1, 1)), equalTo(true)); + assertThat(MDate.create(2010, 12, 31).nextDay().equals(MDate.create(2011, 1, 1)), equalTo(true)); } [Test] public function testMonthsAgo():void { - assertThat(new MDate(2011, 3, 31).monthsAgo(1).equals(new MDate(2011, 2, 28)), equalTo(true)); - assertThat(new MDate(2011, 3, 31).monthsAgo(2).equals(new MDate(2011, 1, 31)), equalTo(true)); + assertThat(MDate.create(2011, 3, 31).monthsAgo(1).equals(MDate.create(2011, 2, 28)), equalTo(true)); + assertThat(MDate.create(2011, 3, 31).monthsAgo(2).equals(MDate.create(2011, 1, 31)), equalTo(true)); // test leap year is Feb 29th - assertThat(new MDate(2004, 3, 31).monthsAgo(1).equals(new MDate(2004, 2, 29)), equalTo(true)); + assertThat(MDate.create(2004, 3, 31).monthsAgo(1).equals(MDate.create(2004, 2, 29)), equalTo(true)); } [Test] public function testMonthSince():void { - assertThat(new MDate(2011, 1, 31).monthsSince(1).equals(new MDate(2011, 2, 28)), equalTo(true)); - assertThat(new MDate(2011, 1, 31).monthsSince(2).equals(new MDate(2011, 3, 31)), equalTo(true)); + assertThat(MDate.create(2011, 1, 31).monthsSince(1).equals(MDate.create(2011, 2, 28)), equalTo(true)); + assertThat(MDate.create(2011, 1, 31).monthsSince(2).equals(MDate.create(2011, 3, 31)), equalTo(true)); // test leap year is Feb 29th - assertThat(new MDate(2004, 1, 31).monthsSince(1).equals(new MDate(2004, 2, 29)), equalTo(true)); + assertThat(MDate.create(2004, 1, 31).monthsSince(1).equals(MDate.create(2004, 2, 29)), equalTo(true)); } [Test] public function testPrevMonth():void { - assertThat(new MDate(2011, 3, 31).prevMonth().equals(new MDate(2011, 2, 28)), equalTo(true)); + assertThat(MDate.create(2011, 3, 31).prevMonth().equals(MDate.create(2011, 2, 28)), equalTo(true)); } [Test] public function testNextMonth():void { - assertThat(new MDate(2011, 1, 31).nextMonth().equals(new MDate(2011, 2, 28)), equalTo(true)); + assertThat(MDate.create(2011, 1, 31).nextMonth().equals(MDate.create(2011, 2, 28)), equalTo(true)); } [Test] public function testYearsAgo():void { - assertThat(new MDate(2012, 2, 29).yearsAgo(1).equals(new MDate(2011, 2, 28)), equalTo(true)); + assertThat(MDate.create(2012, 2, 29).yearsAgo(1).equals(MDate.create(2011, 2, 28)), equalTo(true)); } [Test] public function testYearsSince():void { - assertThat(new MDate(2012, 2, 29).yearsSince(1).equals(new MDate(2013, 2, 28)), equalTo(true)); + assertThat(MDate.create(2012, 2, 29).yearsSince(1).equals(MDate.create(2013, 2, 28)), equalTo(true)); } [Test] public function testPrevYear():void { - assertThat(new MDate(2012, 2, 29).prevYear().equals(new MDate(2011, 2, 28)), equalTo(true)); + assertThat(MDate.create(2012, 2, 29).prevYear().equals(MDate.create(2011, 2, 28)), equalTo(true)); } [Test] public function testNextYear():void { - assertThat(new MDate(2012, 2, 29).yearsSince(1).equals(new MDate(2013, 2, 28)), equalTo(true)); + assertThat(MDate.create(2012, 2, 29).yearsSince(1).equals(MDate.create(2013, 2, 28)), equalTo(true)); } [Test] public function testIsLeapYear():void { - assertThat(new MDate(1800, 1, 1).isLeapYear, equalTo(false)); - assertThat(new MDate(2000, 1, 1).isLeapYear, equalTo(true)); - assertThat(new MDate(2004, 1, 1).isLeapYear, equalTo(true)); + assertThat(MDate.create(1800, 1, 1).isLeapYear, equalTo(false)); + assertThat(MDate.create(2000, 1, 1).isLeapYear, equalTo(true)); + assertThat(MDate.create(2004, 1, 1).isLeapYear, equalTo(true)); } } } \ No newline at end of file