From 1da9e93241740c1b4cfe3c0bfb30648db07cfa4f Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Thu, 19 Feb 2009 22:59:37 -0800 Subject: [PATCH] Implement support for UNIX epoch initialization. Add support for the +[CPDate dateWithTimeIntervalSince1970:] and -[CPDate initWithTimeIntervalSince1970]. --- Foundation/CPDate.j | 10 +++++++++- Tests/Foundation/CPDateTest.j | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Foundation/CPDate.j b/Foundation/CPDate.j index db83c63c4..d30084ce6 100644 --- a/Foundation/CPDate.j +++ b/Foundation/CPDate.j @@ -43,6 +43,9 @@ var CPDateReferenceDate = new Date(Date.UTC(2001,1,1,0,0,0,0)); { return [[CPDate alloc] initWithTimeIntervalSinceNow:seconds]; } ++ (id)dateWithTimeIntervalSince1970:(CPTimeInterval)seconds { + return [[CPDate alloc] initWithTimeIntervalSince1970:seconds]; +} + (id)dateWithTimeIntervalSinceReferenceDate:(CPTimeInterval)seconds { return [[CPDate alloc] initWithTimeIntervalSinceReferenceDate:seconds]; @@ -63,6 +66,11 @@ var CPDateReferenceDate = new Date(Date.UTC(2001,1,1,0,0,0,0)); self = new Date((new Date()).getTime() + seconds * 1000); return self; } +- (id)initWithTimeIntervalSince1970:(CPTimeInterval)seconds +{ + self = new Date(seconds * 1000); + return self; +} - (id)initWithTimeIntervalSinceReferenceDate:(CPTimeInterval)seconds { self = [self initWithTimeInterval:seconds sinceDate:CPDateReferenceDate]; @@ -129,4 +137,4 @@ var CPDateReferenceDate = new Date(Date.UTC(2001,1,1,0,0,0,0)); @end -Date.prototype.isa = CPDate; \ No newline at end of file +Date.prototype.isa = CPDate; diff --git a/Tests/Foundation/CPDateTest.j b/Tests/Foundation/CPDateTest.j index a94ec00c5..075f93ce7 100644 --- a/Tests/Foundation/CPDateTest.j +++ b/Tests/Foundation/CPDateTest.j @@ -2,6 +2,14 @@ import @implementation CPDateTest : OJTestCase +- (void)testSince1970 +{ + /* These two dates should be equal to Fri Feb 13 2009 15:31:30 GMT-0800 */ + unixDate = [CPDate dateWithTimeIntervalSince1970: 1234567890]; + cocoaDate = [CPDate dateWithTimeIntervalSinceReferenceDate: 253582290]; + [self assertTrue:[unixDate isEqualToDate: cocoaDate]]; +} + - (void)testDate { var before = new Date(); @@ -17,4 +25,4 @@ import [self assert:middle equals:[middle laterDate:past] message:"laterDate incorrect"]; } -@end \ No newline at end of file +@end