diff --git a/ChangeLog b/ChangeLog index 8f9536716..29bde4089 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2011-01-01 Stefan Bidigaray + + * Headers/Foundation/NSCalendar.h: + * Source/NSCalendar.m: Added NSCoding and NSCopying. + * Source/NSLocale.m: Implement -isEqual: and do a better job of copying. + 2011-01-01 Stefan Bidigaray * Headers/Foundation/NSCalendar.h: diff --git a/Headers/Foundation/NSCalendar.h b/Headers/Foundation/NSCalendar.h index 7232a1d6b..32ec4770b 100644 --- a/Headers/Foundation/NSCalendar.h +++ b/Headers/Foundation/NSCalendar.h @@ -132,7 +132,7 @@ enum -@interface NSCalendar : NSObject +@interface NSCalendar : NSObject { NSString *_identifier; NSString *_localeId; diff --git a/Source/NSCalendar.m b/Source/NSCalendar.m index c9b461788..c0df8b48b 100644 --- a/Source/NSCalendar.m +++ b/Source/NSCalendar.m @@ -24,6 +24,7 @@ #import "common.h" #import "Foundation/NSCalendar.h" +#import "Foundation/NSCoder.h" #import "Foundation/NSDate.h" #import "Foundation/NSDictionary.h" #import "Foundation/NSLocale.h" @@ -71,6 +72,8 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit) - (void) _openCalendar; - (void) _closeCalendar; - (NSString *) _localeIdWithLocale: (NSLocale *) locale; +- (NSString *)_localeIdentifier; +- (void) _setLocaleIdentifier: (NSString *) identifier; @end #define TZ_NAME_LENGTH 1024 @@ -130,6 +133,21 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit) return result; } + +- (NSString *)_localeIdentifier +{ + return _localeId; +} + +- (void) _setLocaleIdentifier: (NSString *) identifier +{ + if ([identifier isEqualToString: _localeId]) + return; + + [self _closeCalendar]; + RELEASE(_localeId); + _localeId = RETAIN(identifier); +} @end @implementation NSCalendar @@ -340,12 +358,7 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit) - (void)setLocale: (NSLocale *) locale { - if ([[locale localeIdentifier] isEqual: _localeId]) - return; - - [self _closeCalendar]; - RELEASE(_localeId); - _localeId = RETAIN([self _localeIdWithLocale: locale]); + [self _setLocaleIdentifier: [self _localeIdWithLocale: locale]]; } - (NSUInteger) firstWeekday @@ -481,6 +494,58 @@ static UCalendarDateFields _NSCalendarUnitToDateField (NSCalendarUnit unit) { return NO; } + +- (BOOL) isEqual: (id) obj +{ + if ([obj isKindOfClass: [self class]]) + { + if (![_identifier isEqual: [obj calendarIdentifier]]) + return NO; + if (![_localeId isEqual: [obj _localeIdentifier]]) + return NO; + if (![_tz isEqual: [obj timeZone]]) + return NO; + return YES; + } + + return NO; +} + +- (void) encodeWithCoder: (NSCoder*)encoder +{ + [encoder encodeObject: _identifier]; + [encoder encodeObject: _localeId]; + [encoder encodeObject: _tz]; +} + +- (id) initWithCoder: (NSCoder*)decoder +{ + NSString *s = [decoder decodeObject]; + + [self initWithCalendarIdentifier: s]; + [self _setLocaleIdentifier: [decoder decodeObject]]; + [self setTimeZone: [decoder decodeObject]]; + + return self; +} + +- (id) copyWithZone: (NSZone*)zone +{ + NSCalendar *result; + + if (NSShouldRetainWithZone(self, zone)) + return RETAIN(self); + else + { + result = (NSCalendar *)NSCopyObject(self, 0, zone); + result->_identifier = [_identifier copyWithZone: zone]; + result->_localeId = [_localeId copyWithZone: zone]; + result->_tz = RETAIN(_tz); + } + + return result; +} + @end diff --git a/Source/NSLocale.m b/Source/NSLocale.m index cc1109e55..0e94f69da 100644 --- a/Source/NSLocale.m +++ b/Source/NSLocale.m @@ -912,6 +912,14 @@ static NSRecursiveLock *classLock = nil; return _localeId; } +- (BOOL) isEqual: (id) obj +{ + if ([obj isKindOfClass: [self class]]) + return [_localeId isEqual: [obj localeIdentifier]]; + + return NO; +} + - (void) dealloc { RELEASE(_localeId); @@ -933,10 +941,17 @@ static NSRecursiveLock *classLock = nil; - (id) copyWithZone: (NSZone *) zone { + NSLocale *result; + if (NSShouldRetainWithZone(self, zone)) - return RETAIN(self); + result = RETAIN(self); else - return NSCopyObject(self, 0, zone); + { + result = (NSLocale *)NSCopyObject(self, 0, zone); + result->_localeId = [_localeId copyWithZone: zone]; + } + + return result; } @end