mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-22 16:33:29 +00:00
NSISO8601DateFormatter bugfix (as reported by kevinpeizner) and tweaks.
This commit is contained in:
parent
7924d3a08f
commit
1942313dea
3 changed files with 137 additions and 64 deletions
|
@ -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.
|
||||
|
|
|
@ -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 @@
|
|||
{
|
||||
result = [result stringByAppendingString: @"yyyy"];
|
||||
}
|
||||
if (_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate &&
|
||||
_formatOptions & NSISO8601DateFormatWithMonth)
|
||||
if ((_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate)
|
||||
&& (_formatOptions & NSISO8601DateFormatWithMonth))
|
||||
{
|
||||
result = [result stringByAppendingString: @"-"];
|
||||
}
|
||||
|
@ -82,8 +48,8 @@
|
|||
{
|
||||
result = [result stringByAppendingString: @"MM"];
|
||||
}
|
||||
if (_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate &&
|
||||
_formatOptions & NSISO8601DateFormatWithDay)
|
||||
if ((_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate)
|
||||
&& (_formatOptions & NSISO8601DateFormatWithDay))
|
||||
{
|
||||
result = [result stringByAppendingString: @"-"];
|
||||
}
|
||||
|
@ -93,8 +59,8 @@
|
|||
}
|
||||
|
||||
// Build time...
|
||||
if (_formatOptions & NSISO8601DateFormatWithSpaceBetweenDateAndTime &&
|
||||
_formatOptions & NSISO8601DateFormatWithTime)
|
||||
if ((_formatOptions & NSISO8601DateFormatWithSpaceBetweenDateAndTime)
|
||||
&& (_formatOptions & NSISO8601DateFormatWithTime))
|
||||
{
|
||||
result = [result stringByAppendingString: @" "];
|
||||
}
|
||||
|
@ -122,7 +88,7 @@
|
|||
{
|
||||
if (_formatOptions & NSISO8601DateFormatWithColonSeparatorInTimeZone)
|
||||
{
|
||||
result = [result stringByAppendingString: @"ZZ:ZZ"];
|
||||
result = [result stringByAppendingString: @"ZZZZZ"];
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -133,38 +99,22 @@
|
|||
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 @@
|
|||
}
|
||||
}
|
||||
|
||||
- (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 @@
|
|||
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
Tests/base/NSDateFormatter/NSISO8601DateFormatter.m
Normal file
62
Tests/base/NSDateFormatter/NSISO8601DateFormatter.m
Normal file
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in a new issue