diff --git a/ChangeLog b/ChangeLog index 8cc942cc99..9bdcffe33b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2023-10-26 Richard Frith-Macdonald + + * Source/NSISO8601DateFormatter.m: + * Tests/base/NSDateFormatter/NSISO8601DateFormatter.m: + Add a couple of testcases, tidy up source code formatting, and make + fix for github issue #339 as suggested by kevinpeizner + 2023-10-10 Richard Frith-Macdonald * Source/GSPrivate.h: New method to set up system error information. diff --git a/Source/NSISO8601DateFormatter.m b/Source/NSISO8601DateFormatter.m index 6d51afec6f..c093c4d94d 100644 --- a/Source/NSISO8601DateFormatter.m +++ b/Source/NSISO8601DateFormatter.m @@ -30,40 +30,6 @@ @implementation NSISO8601DateFormatter -- (instancetype) init -{ - self = [super init]; - if (self != nil) - { - _formatter = [[NSDateFormatter alloc] init]; - _timeZone = RETAIN([NSTimeZone localTimeZone]); - _formatOptions = NSISO8601DateFormatWithInternetDateTime; - } - return self; -} - -- (oneway void) dealloc -{ - RELEASE(_formatter); - RELEASE(_timeZone); - [super dealloc]; -} - -- (NSTimeZone *) timeZone -{ - return _timeZone; -} - -- (void) setTimeZone: (NSTimeZone *)tz -{ - _timeZone = tz; -} - -- (NSISO8601DateFormatOptions) formatOptions -{ - return _formatOptions; -} - - (NSString *) _buildFormatWithOptions { NSString *result = @""; @@ -73,8 +39,8 @@ - (NSString *) _buildFormatWithOptions { result = [result stringByAppendingString: @"yyyy"]; } - if (_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate && - _formatOptions & NSISO8601DateFormatWithMonth) + if ((_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate) + && (_formatOptions & NSISO8601DateFormatWithMonth)) { result = [result stringByAppendingString: @"-"]; } @@ -82,8 +48,8 @@ - (NSString *) _buildFormatWithOptions { result = [result stringByAppendingString: @"MM"]; } - if (_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate && - _formatOptions & NSISO8601DateFormatWithDay) + if ((_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate) + && (_formatOptions & NSISO8601DateFormatWithDay)) { result = [result stringByAppendingString: @"-"]; } @@ -93,8 +59,8 @@ - (NSString *) _buildFormatWithOptions } // Build time... - if (_formatOptions & NSISO8601DateFormatWithSpaceBetweenDateAndTime && - _formatOptions & NSISO8601DateFormatWithTime) + if ((_formatOptions & NSISO8601DateFormatWithSpaceBetweenDateAndTime) + && (_formatOptions & NSISO8601DateFormatWithTime)) { result = [result stringByAppendingString: @" "]; } @@ -122,7 +88,7 @@ - (NSString *) _buildFormatWithOptions { if (_formatOptions & NSISO8601DateFormatWithColonSeparatorInTimeZone) { - result = [result stringByAppendingString: @"ZZ:ZZ"]; + result = [result stringByAppendingString: @"ZZZZZ"]; } else { @@ -133,38 +99,22 @@ - (NSString *) _buildFormatWithOptions return result; } -- (void) setFormatOptions: (NSISO8601DateFormatOptions)options -{ - _formatOptions = options; -} - -- (NSString *) stringFromDate: (NSDate *)date -{ - NSString *formatString = [self _buildFormatWithOptions]; - [_formatter setTimeZone: _timeZone]; - [_formatter setDateFormat: formatString]; - return [_formatter stringFromDate: date]; -} - - (NSDate *) dateFromString: (NSString *)string { NSString *formatString = [self _buildFormatWithOptions]; + [_formatter setTimeZone: _timeZone]; [_formatter setDateFormat: formatString]; return [_formatter dateFromString: string]; } -+ (NSString *) stringFromDate: (NSDate *)date - timeZone: (NSTimeZone *)timeZone - formatOptions: (NSISO8601DateFormatOptions)formatOptions +- (oneway void) dealloc { - NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init]; - AUTORELEASE(formatter); - [formatter setTimeZone: timeZone]; - [formatter setFormatOptions: formatOptions]; - return [formatter stringFromDate: date]; + RELEASE(_formatter); + RELEASE(_timeZone); + [super dealloc]; } - + - (void) encodeWithCoder: (NSCoder *)coder { if ([coder allowsKeyedCoding]) @@ -179,6 +129,23 @@ - (void) encodeWithCoder: (NSCoder *)coder } } +- (NSISO8601DateFormatOptions) formatOptions +{ + return _formatOptions; +} + +- (instancetype) init +{ + self = [super init]; + if (self != nil) + { + _formatter = [[NSDateFormatter alloc] init]; + _timeZone = RETAIN([NSTimeZone localTimeZone]); + _formatOptions = NSISO8601DateFormatWithInternetDateTime; + } + return self; +} + - (id) initWithCoder: (NSCoder *)decoder { if ((self = [super init]) != nil) @@ -191,10 +158,47 @@ - (id) initWithCoder: (NSCoder *)decoder else { ASSIGN(_timeZone, [decoder decodeObject]); - [decoder decodeValueOfObjCType: @encode(NSUInteger) at: &_formatOptions]; + [decoder decodeValueOfObjCType: @encode(NSUInteger) + at: &_formatOptions]; } } return self; } +- (void) setFormatOptions: (NSISO8601DateFormatOptions)options +{ + _formatOptions = options; +} + +- (void) setTimeZone: (NSTimeZone *)tz +{ + _timeZone = tz; +} + +- (NSString *) stringFromDate: (NSDate *)date +{ + NSString *formatString = [self _buildFormatWithOptions]; + + [_formatter setTimeZone: _timeZone]; + [_formatter setDateFormat: formatString]; + return [_formatter stringFromDate: date]; +} + ++ (NSString *) stringFromDate: (NSDate *)date + timeZone: (NSTimeZone *)timeZone + formatOptions: (NSISO8601DateFormatOptions)formatOptions +{ + NSISO8601DateFormatter *formatter; + + formatter = AUTORELEASE([[NSISO8601DateFormatter alloc] init]); + [formatter setTimeZone: timeZone]; + [formatter setFormatOptions: formatOptions]; + return [formatter stringFromDate: date]; +} + +- (NSTimeZone *) timeZone +{ + return _timeZone; +} + @end diff --git a/Tests/base/NSDateFormatter/NSISO8601DateFormatter.m b/Tests/base/NSDateFormatter/NSISO8601DateFormatter.m new file mode 100644 index 0000000000..24ca512b03 --- /dev/null +++ b/Tests/base/NSDateFormatter/NSISO8601DateFormatter.m @@ -0,0 +1,62 @@ +#import +#import +#import +#import +#import +#import +#import "Testing.h" + +#if defined(GS_USE_ICU) +#define IS_SUPPORTED GS_USE_ICU +#else +#define IS_SUPPORTED 0 +#endif + +int main(void) +{ + NSTimeZone *tz; + NSISO8601DateFormatter *fmt; + NSDate *date; + NSString *estr; + NSString *istr; + NSString *ostr; + + START_SET("NSISO8601DateFormatter") + if (!IS_SUPPORTED) + SKIP("NSISO8601DateFormatter not supported\nThe ICU library was not available when GNUstep-base was built") + + tz = [NSTimeZone timeZoneWithName: @"GMT"]; + [NSTimeZone setDefaultTimeZone: tz]; + + fmt = [[NSISO8601DateFormatter alloc] init]; + estr = @"2011-01-27T17:36:00Z"; + istr = @"2011-01-27T17:36:00+00:00"; + date = [fmt dateFromString: istr]; + RELEASE(fmt); + + fmt = [NSISO8601DateFormatter new]; + ostr = [fmt stringFromDate: date]; + RELEASE(fmt); + + PASS_EQUAL(ostr, estr, "date format matches for GMT") + + + fmt = [[NSISO8601DateFormatter alloc] init]; + estr = @"2011-08-27T16:36:00Z"; + istr = @"2011-08-27T17:36:00+01:00"; + date = [fmt dateFromString: istr]; + RELEASE(fmt); + + tz = [NSTimeZone timeZoneWithName: @"AWST"]; + fmt = [NSISO8601DateFormatter new]; + [fmt setTimeZone: tz]; + ostr = [fmt stringFromDate: date]; + RELEASE(fmt); + + PASS_EQUAL(ostr, estr, "date format matches for AWST") + + END_SET("NSISO8601DateFormatter") + + return 0; +} +