2019-10-29 16:46:18 +00:00
|
|
|
/* Implementation of class NSISO8601DateFormatter
|
|
|
|
Copyright (C) 2019 Free Software Foundation, Inc.
|
|
|
|
|
2019-10-31 07:19:31 +00:00
|
|
|
By: Gregory John Casamento <greg.casamento@gmail.com>
|
2019-10-29 16:46:18 +00:00
|
|
|
Date: Tue Oct 29 04:43:13 EDT 2019
|
|
|
|
|
|
|
|
This file is part of the GNUstep Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2019-12-09 23:36:00 +00:00
|
|
|
Lesser General Public License for more details.
|
2019-10-29 16:46:18 +00:00
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
2019-12-09 23:36:00 +00:00
|
|
|
Boston, MA 02110 USA.
|
2019-10-29 16:46:18 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-10 21:07:27 +00:00
|
|
|
#import "Foundation/NSCoder.h"
|
|
|
|
#import "Foundation/NSDateFormatter.h"
|
|
|
|
#import "Foundation/NSISO8601DateFormatter.h"
|
|
|
|
#import "Foundation/NSString.h"
|
|
|
|
#import "Foundation/NSTimeZone.h"
|
2019-10-29 16:46:18 +00:00
|
|
|
|
|
|
|
@implementation NSISO8601DateFormatter
|
|
|
|
|
|
|
|
- (instancetype) init
|
|
|
|
{
|
|
|
|
self = [super init];
|
2020-05-10 21:07:27 +00:00
|
|
|
if (self != nil)
|
2019-10-29 16:46:18 +00:00
|
|
|
{
|
2019-10-31 07:19:31 +00:00
|
|
|
_formatter = [[NSDateFormatter alloc] init];
|
|
|
|
_timeZone = RETAIN([NSTimeZone localTimeZone]);
|
|
|
|
_formatOptions = NSISO8601DateFormatWithInternetDateTime;
|
2019-10-29 16:46:18 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
2019-10-31 07:19:31 +00:00
|
|
|
|
2020-05-10 21:07:27 +00:00
|
|
|
- (oneway void) dealloc
|
2019-10-31 07:19:31 +00:00
|
|
|
{
|
|
|
|
RELEASE(_formatter);
|
|
|
|
RELEASE(_timeZone);
|
2020-05-10 21:07:27 +00:00
|
|
|
[super dealloc];
|
2019-10-31 07:19:31 +00:00
|
|
|
}
|
2019-10-29 16:46:18 +00:00
|
|
|
|
|
|
|
- (NSTimeZone *) timeZone
|
|
|
|
{
|
|
|
|
return _timeZone;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setTimeZone: (NSTimeZone *)tz
|
|
|
|
{
|
|
|
|
_timeZone = tz;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSISO8601DateFormatOptions) formatOptions
|
|
|
|
{
|
|
|
|
return _formatOptions;
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:19:31 +00:00
|
|
|
- (NSString *) _buildFormatWithOptions
|
|
|
|
{
|
|
|
|
NSString *result = @"";
|
|
|
|
|
|
|
|
// Build date...
|
2020-05-10 21:07:27 +00:00
|
|
|
if (_formatOptions & NSISO8601DateFormatWithYear)
|
2019-10-31 07:19:31 +00:00
|
|
|
{
|
|
|
|
result = [result stringByAppendingString: @"yyyy"];
|
|
|
|
}
|
2020-05-10 21:07:27 +00:00
|
|
|
if (_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate &&
|
|
|
|
_formatOptions & NSISO8601DateFormatWithMonth)
|
2019-10-31 07:19:31 +00:00
|
|
|
{
|
|
|
|
result = [result stringByAppendingString: @"-"];
|
|
|
|
}
|
2020-05-10 21:07:27 +00:00
|
|
|
if (_formatOptions & NSISO8601DateFormatWithMonth)
|
2019-10-31 07:19:31 +00:00
|
|
|
{
|
|
|
|
result = [result stringByAppendingString: @"MM"];
|
|
|
|
}
|
2020-05-10 21:07:27 +00:00
|
|
|
if (_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate &&
|
|
|
|
_formatOptions & NSISO8601DateFormatWithDay)
|
2019-10-31 07:19:31 +00:00
|
|
|
{
|
|
|
|
result = [result stringByAppendingString: @"-"];
|
|
|
|
}
|
2020-05-10 21:07:27 +00:00
|
|
|
if (_formatOptions & NSISO8601DateFormatWithDay)
|
2019-10-31 07:19:31 +00:00
|
|
|
{
|
|
|
|
result = [result stringByAppendingString: @"dd"];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build time...
|
2020-05-10 21:07:27 +00:00
|
|
|
if (_formatOptions & NSISO8601DateFormatWithSpaceBetweenDateAndTime &&
|
|
|
|
_formatOptions & NSISO8601DateFormatWithTime)
|
2019-10-31 07:19:31 +00:00
|
|
|
{
|
|
|
|
result = [result stringByAppendingString: @" "];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Add T in format if we have a time component...
|
|
|
|
result = [result stringByAppendingString: @"'T'"];
|
|
|
|
}
|
2020-05-10 21:07:27 +00:00
|
|
|
if (_formatOptions & NSISO8601DateFormatWithTime)
|
2019-10-31 07:19:31 +00:00
|
|
|
{
|
2020-05-10 21:07:27 +00:00
|
|
|
if (_formatOptions & NSISO8601DateFormatWithColonSeparatorInTime)
|
2019-10-31 07:19:31 +00:00
|
|
|
{
|
|
|
|
result = [result stringByAppendingString: @"HH:mm:ss"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = [result stringByAppendingString: @"HHmmss"];
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 21:07:27 +00:00
|
|
|
if (_formatOptions & NSISO8601DateFormatWithFractionalSeconds)
|
2019-10-31 07:19:31 +00:00
|
|
|
{
|
|
|
|
result = [result stringByAppendingString: @".SSSSSS"];
|
|
|
|
}
|
2020-05-10 21:07:27 +00:00
|
|
|
if (_formatOptions & NSISO8601DateFormatWithTimeZone)
|
2019-10-31 07:19:31 +00:00
|
|
|
{
|
2020-05-10 21:07:27 +00:00
|
|
|
if (_formatOptions & NSISO8601DateFormatWithColonSeparatorInTimeZone)
|
2019-10-31 07:19:31 +00:00
|
|
|
{
|
|
|
|
result = [result stringByAppendingString: @"ZZ:ZZ"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = [result stringByAppendingString: @"ZZZZ"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:46:18 +00:00
|
|
|
- (void) setFormatOptions: (NSISO8601DateFormatOptions)options
|
|
|
|
{
|
|
|
|
_formatOptions = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) stringFromDate: (NSDate *)date
|
|
|
|
{
|
2019-10-31 07:19:31 +00:00
|
|
|
NSString *formatString = [self _buildFormatWithOptions];
|
|
|
|
[_formatter setTimeZone: _timeZone];
|
|
|
|
[_formatter setDateFormat: formatString];
|
|
|
|
return [_formatter stringFromDate: date];
|
2019-10-29 16:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDate *) dateFromString: (NSString *)string
|
|
|
|
{
|
2019-10-31 07:19:31 +00:00
|
|
|
NSString *formatString = [self _buildFormatWithOptions];
|
|
|
|
[_formatter setTimeZone: _timeZone];
|
|
|
|
[_formatter setDateFormat: formatString];
|
|
|
|
return [_formatter dateFromString: string];
|
2019-10-29 16:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSString *) stringFromDate: (NSDate *)date
|
|
|
|
timeZone: (NSTimeZone *)timeZone
|
|
|
|
formatOptions: (NSISO8601DateFormatOptions)formatOptions
|
|
|
|
{
|
2019-10-31 07:19:31 +00:00
|
|
|
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
|
|
|
|
AUTORELEASE(formatter);
|
|
|
|
[formatter setTimeZone: timeZone];
|
|
|
|
[formatter setFormatOptions: formatOptions];
|
|
|
|
return [formatter stringFromDate: date];
|
2019-10-29 16:46:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 21:07:27 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder *)coder
|
|
|
|
{
|
|
|
|
if ([coder allowsKeyedCoding])
|
|
|
|
{
|
|
|
|
[coder encodeObject: _timeZone forKey: @"NS.timeZone"];
|
|
|
|
[coder encodeInteger: _formatOptions forKey: @"NS.formatOptions"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[coder encodeObject: _timeZone];
|
|
|
|
[coder encodeValueOfObjCType: @encode(NSUInteger) at: &_formatOptions];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithCoder: (NSCoder *)decoder
|
|
|
|
{
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
if ([decoder allowsKeyedCoding])
|
|
|
|
{
|
|
|
|
ASSIGN(_timeZone, [decoder decodeObjectForKey: @"NS.timeZone"]);
|
|
|
|
_formatOptions = [decoder decodeIntegerForKey: @"NS.formatOptions"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ASSIGN(_timeZone, [decoder decodeObject]);
|
|
|
|
[decoder decodeValueOfObjCType: @encode(NSUInteger) at: &_formatOptions];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
2019-10-29 16:46:18 +00:00
|
|
|
@end
|
|
|
|
|