Add support for encoding dates, courtesy of luddep.

This commit is contained in:
Ross Boucher
2009-11-24 23:02:38 -05:00
parent 1eb9202233
commit 44fbf8afe2
2 changed files with 30 additions and 0 deletions
+21
View File
@@ -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;
+9
View File
@@ -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.