Skip to content

Commit

Permalink
changing dates to be instantiated by an epoch time
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultz committed Apr 5, 2011
1 parent 4113359 commit d0f412e
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 75 deletions.
60 changes: 39 additions & 21 deletions src/mesh/core/date/DateTime.as
Expand Up @@ -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.
Expand All @@ -20,57 +29,66 @@ 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);
}

/**
* The hour in the day using a 24-hour clock, between 0 and 23.
*/
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.
* <code>true</code> 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;
}
}
}
75 changes: 45 additions & 30 deletions src/mesh/core/date/MDate.as
Expand Up @@ -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.
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -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);
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -200,7 +206,7 @@ package mesh.core.date
* @param n The number of days to subtract.
* @return The previous <code>n</code> days as a new date.
*/
public function daysAgo(n:int = 1):MDate
public function daysAgo(n:int = 1):*
{
return daysSince(-n);
}
Expand All @@ -212,17 +218,17 @@ package mesh.core.date
* @param n The number of days to add.
* @return The next <code>n</code> 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));
}

/**
* Returns a new date where the date is a day before this date.
*
* @return The day before this date.
*/
public function prevDay():MDate
public function prevDay():*
{
return daysAgo(1);
}
Expand All @@ -232,7 +238,7 @@ package mesh.core.date
*
* @return The day after this date.
*/
public function nextDay():MDate
public function nextDay():*
{
return daysSince(1);
}
Expand All @@ -244,7 +250,7 @@ package mesh.core.date
* @param n The number of months to subtract.
* @return The previous <code>n</code> months as a new date.
*/
public function monthsAgo(n:int = 1):MDate
public function monthsAgo(n:int = 1):*
{
return monthsSince(-n);
}
Expand All @@ -256,22 +262,22 @@ package mesh.core.date
* @param n The number of months to subtract.
* @return The next <code>n</code> 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);
}

/**
* Returns a new date where the date is a month before this date.
*
* @return The month before this date.
*/
public function prevMonth():MDate
public function prevMonth():*
{
return monthsAgo(1);
}
Expand All @@ -281,7 +287,7 @@ package mesh.core.date
*
* @return The month after this date.
*/
public function nextMonth():MDate
public function nextMonth():*
{
return monthsSince(1);
}
Expand All @@ -293,7 +299,7 @@ package mesh.core.date
* @param n The number of years to subtract.
* @return The previous <code>n</code> years as a new date.
*/
public function yearsAgo(n:int = 1):MDate
public function yearsAgo(n:int = 1):*
{
return yearsSince(-n);
}
Expand All @@ -305,22 +311,22 @@ package mesh.core.date
* @param n The number of years to add.
* @return The next <code>n</code> 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);
}

/**
* Returns a new date where the date is a year before this date.
*
* @return The year before this date.
*/
public function prevYear():MDate
public function prevYear():*
{
return yearsAgo(1);
}
Expand All @@ -330,7 +336,7 @@ package mesh.core.date
*
* @return The year after this date.
*/
public function nextYear():MDate
public function nextYear():*
{
return yearsSince(1);
}
Expand Down Expand Up @@ -386,6 +392,15 @@ package mesh.core.date
return _date.time;
}

private var _date:Date;
/**
* The internal <code>Date</code> used to store this date's values.
*/
protected function get date():Date
{
return _date;
}

/**
* The day of the month.
*/
Expand Down
20 changes: 20 additions & 0 deletions 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);
}
}
}

0 comments on commit d0f412e

Please sign in to comment.