Implement NSISO8601DateFormatter fully

This commit is contained in:
Gregory John Casamento 2019-10-31 03:19:31 -04:00
parent cfa2b65cdb
commit 6ec1aca427
2 changed files with 100 additions and 7 deletions

View file

@ -1,7 +1,7 @@
/* Definition of class NSISO8601DateFormatter
Copyright (C) 2019 Free Software Foundation, Inc.
By: heron
By: Gregory John Casamento <greg.casamento@gmail.com>
Date: Tue Oct 29 04:43:13 EDT 2019
This file is part of the GNUstep Library.
@ -43,7 +43,7 @@ enum
NSISO8601DateFormatWithTimeZone = (1UL << 6),
NSISO8601DateFormatWithSpaceBetweenDateAndTime = (1UL << 7),
NSISO8601DateFormatWithDashSeparatorInDate = (1UL << 8),
NSISO8601DateFormatWithColonSeparatorInTime = (1UL << 9),
NSISO8601DateFormatWithColonSeparatorInTime = (1UL << 9),
NSISO8601DateFormatWithColonSeparatorInTimeZone = (1UL << 10),
NSISO8601DateFormatWithFractionalSeconds = (1UL << 11),
NSISO8601DateFormatWithFullDate = NSISO8601DateFormatWithYear |
@ -58,12 +58,13 @@ enum
};
typedef NSUInteger NSISO8601DateFormatOptions;
@class NSTimeZone, NSString, NSDate;
@class NSTimeZone, NSString, NSDate, NSDateFormatter;
@interface NSISO8601DateFormatter : NSFormatter <NSCoding>
{
NSTimeZone *_timeZone;
NSISO8601DateFormatOptions _formatOptions;
NSDateFormatter *_formatter;
}
- (NSTimeZone *) timeZone;

View file

@ -1,7 +1,7 @@
/* Implementation of class NSISO8601DateFormatter
Copyright (C) 2019 Free Software Foundation, Inc.
By: heron
By: Gregory John Casamento <greg.casamento@gmail.com>
Date: Tue Oct 29 04:43:13 EDT 2019
This file is part of the GNUstep Library.
@ -23,6 +23,9 @@
*/
#include <Foundation/NSISO8601DateFormatter.h>
#include <Foundation/NSDateFormatter.h>
#include <Foundation/NSTimeZone.h>
#include <Foundation/NSString.h>
@implementation NSISO8601DateFormatter
@ -31,9 +34,19 @@
self = [super init];
if(self != nil)
{
_formatter = [[NSDateFormatter alloc] init];
_timeZone = RETAIN([NSTimeZone localTimeZone]);
_formatOptions = NSISO8601DateFormatWithInternetDateTime;
}
return self;
}
- (oneway void) release
{
RELEASE(_formatter);
RELEASE(_timeZone);
[super release];
}
- (NSTimeZone *) timeZone
{
@ -50,6 +63,75 @@
return _formatOptions;
}
- (NSString *) _buildFormatWithOptions
{
NSString *result = @"";
// Build date...
if(_formatOptions & NSISO8601DateFormatWithYear)
{
result = [result stringByAppendingString: @"yyyy"];
}
if(_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate &&
_formatOptions & NSISO8601DateFormatWithMonth)
{
result = [result stringByAppendingString: @"-"];
}
if(_formatOptions & NSISO8601DateFormatWithMonth)
{
result = [result stringByAppendingString: @"MM"];
}
if(_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate &&
_formatOptions & NSISO8601DateFormatWithDay)
{
result = [result stringByAppendingString: @"-"];
}
if(_formatOptions & NSISO8601DateFormatWithDay)
{
result = [result stringByAppendingString: @"dd"];
}
// Build time...
if(_formatOptions & NSISO8601DateFormatWithSpaceBetweenDateAndTime &&
_formatOptions & NSISO8601DateFormatWithTime)
{
result = [result stringByAppendingString: @" "];
}
else
{
// Add T in format if we have a time component...
result = [result stringByAppendingString: @"'T'"];
}
if(_formatOptions & NSISO8601DateFormatWithTime)
{
if(_formatOptions & NSISO8601DateFormatWithColonSeparatorInTime)
{
result = [result stringByAppendingString: @"HH:mm:ss"];
}
else
{
result = [result stringByAppendingString: @"HHmmss"];
}
}
if(_formatOptions & NSISO8601DateFormatWithFractionalSeconds)
{
result = [result stringByAppendingString: @".SSSSSS"];
}
if(_formatOptions & NSISO8601DateFormatWithTimeZone)
{
if(_formatOptions & NSISO8601DateFormatWithColonSeparatorInTimeZone)
{
result = [result stringByAppendingString: @"ZZ:ZZ"];
}
else
{
result = [result stringByAppendingString: @"ZZZZ"];
}
}
return result;
}
- (void) setFormatOptions: (NSISO8601DateFormatOptions)options
{
_formatOptions = options;
@ -57,19 +139,29 @@
- (NSString *) stringFromDate: (NSDate *)date
{
return nil;
NSString *formatString = [self _buildFormatWithOptions];
[_formatter setTimeZone: _timeZone];
[_formatter setDateFormat: formatString];
return [_formatter stringFromDate: date];
}
- (NSDate *) dateFromString: (NSString *)string
{
return nil;
NSString *formatString = [self _buildFormatWithOptions];
[_formatter setTimeZone: _timeZone];
[_formatter setDateFormat: formatString];
return [_formatter dateFromString: string];
}
+ (NSString *) stringFromDate: (NSDate *)date
timeZone: (NSTimeZone *)timeZone
formatOptions: (NSISO8601DateFormatOptions)formatOptions
{
return nil;
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
AUTORELEASE(formatter);
[formatter setTimeZone: timeZone];
[formatter setFormatOptions: formatOptions];
return [formatter stringFromDate: date];
}
@end