From 44fbf8afe276a1cb8e1cec0959ab85d3e9d9c3e0 Mon Sep 17 00:00:00 2001 From: Ross Boucher Date: Tue, 24 Nov 2009 23:02:38 -0500 Subject: [PATCH] Add support for encoding dates, courtesy of luddep. --- Foundation/CPDate.j | 21 +++++++++++++++++++++ Tests/Foundation/CPDateTest.j | 9 +++++++++ 2 files changed, 30 insertions(+) diff --git a/Foundation/CPDate.j b/Foundation/CPDate.j index 3dd164157..2f171c4b1 100644 --- a/Foundation/CPDate.j +++ b/Foundation/CPDate.j @@ -185,4 +185,25 @@ var CPDateReferenceDate = new Date(Date.UTC(2001,1,1,0,0,0,0)); @end +var CPDateTimeKey = @"CPDateTimeKey"; + +@implementation CPDate (CPCoding) + +- (id)initWithCoder:(CPCoder)aCoder +{ + if (self) + { + self.setTime([aCoder decodeIntForKey:CPDateTimeKey]); + } + + return self; +} + +- (void)encodeWithCoder:(CPCoder)aCoder +{ + [aCoder encodeInt:self.getTime() forKey:CPDateTimeKey]; +} + +@end + Date.prototype.isa = CPDate; diff --git a/Tests/Foundation/CPDateTest.j b/Tests/Foundation/CPDateTest.j index fe2de759b..4df915457 100644 --- a/Tests/Foundation/CPDateTest.j +++ b/Tests/Foundation/CPDateTest.j @@ -43,6 +43,15 @@ } } +- (void)testEncoding +{ + var date = new Date(), + encodedDate = [CPKeyedArchiver archivedDataWithRootObject:date], + decodedDate = [CPKeyedUnarchiver unarchiveObjectWithData:encodedDate]; + + [self assert:date equals:decodedDate]; +} + - (void)testDescription { // Unfortunately the result will be different depending on the testing machine's timezone.