Skip to content

Commit

Permalink
NSISO8601DateFormatter bugfix (as reported by kevinpeizner) and tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
rfm committed Oct 26, 2023
1 parent 7924d3a commit 1942313
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 64 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2023-10-26 Richard Frith-Macdonald <rfm@gnu.org>

* 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 <rfm@gnu.org>

* Source/GSPrivate.h: New method to set up system error information.
Expand Down
132 changes: 68 additions & 64 deletions Source/NSISO8601DateFormatter.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 = @"";
Expand All @@ -73,17 +39,17 @@ - (NSString *) _buildFormatWithOptions
{
result = [result stringByAppendingString: @"yyyy"];
}
if (_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate &&
_formatOptions & NSISO8601DateFormatWithMonth)
if ((_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate)
&& (_formatOptions & NSISO8601DateFormatWithMonth))
{
result = [result stringByAppendingString: @"-"];
}
if (_formatOptions & NSISO8601DateFormatWithMonth)
{
result = [result stringByAppendingString: @"MM"];
}
if (_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate &&
_formatOptions & NSISO8601DateFormatWithDay)
if ((_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate)
&& (_formatOptions & NSISO8601DateFormatWithDay))
{
result = [result stringByAppendingString: @"-"];
}
Expand All @@ -93,8 +59,8 @@ - (NSString *) _buildFormatWithOptions
}

// Build time...
if (_formatOptions & NSISO8601DateFormatWithSpaceBetweenDateAndTime &&
_formatOptions & NSISO8601DateFormatWithTime)
if ((_formatOptions & NSISO8601DateFormatWithSpaceBetweenDateAndTime)
&& (_formatOptions & NSISO8601DateFormatWithTime))
{
result = [result stringByAppendingString: @" "];
}
Expand Down Expand Up @@ -122,7 +88,7 @@ - (NSString *) _buildFormatWithOptions
{
if (_formatOptions & NSISO8601DateFormatWithColonSeparatorInTimeZone)
{
result = [result stringByAppendingString: @"ZZ:ZZ"];
result = [result stringByAppendingString: @"ZZZZZ"];
}
else
{
Expand All @@ -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])
Expand All @@ -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)
Expand All @@ -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

62 changes: 62 additions & 0 deletions Tests/base/NSDateFormatter/NSISO8601DateFormatter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#import <Foundation/NSCalendar.h>
#import <Foundation/NSDate.h>
#import <Foundation/NSISO8601DateFormatter.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSLocale.h>
#import <Foundation/NSTimeZone.h>
#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;
}

0 comments on commit 1942313

Please sign in to comment.