Implement support for UNIX epoch initialization.

Add support for the +[CPDate dateWithTimeIntervalSince1970:]
and -[CPDate initWithTimeIntervalSince1970].
This commit is contained in:
Landon Fuller
2009-02-26 17:13:37 -08:00
committed by Ross Boucher
parent ec3dcaa287
commit 1da9e93241
2 changed files with 18 additions and 2 deletions
+9 -1
View File
@@ -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;
Date.prototype.isa = CPDate;
+9 -1
View File
@@ -2,6 +2,14 @@ import <Foundation/CPDate.j>
@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 <Foundation/CPDate.j>
[self assert:middle equals:[middle laterDate:past] message:"laterDate incorrect"];
}
@end
@end