libs-base/Source/NSISO8601DateFormatter.m

201 lines
5.4 KiB
Mathematica
Raw Normal View History

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
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,
Boston, MA 02110 USA.
2019-10-29 16:46:18 +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];
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
- (oneway void) dealloc
2019-10-31 07:19:31 +00:00
{
RELEASE(_formatter);
RELEASE(_timeZone);
[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...
if (_formatOptions & NSISO8601DateFormatWithYear)
2019-10-31 07:19:31 +00:00
{
result = [result stringByAppendingString: @"yyyy"];
}
if (_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate &&
_formatOptions & NSISO8601DateFormatWithMonth)
2019-10-31 07:19:31 +00:00
{
result = [result stringByAppendingString: @"-"];
}
if (_formatOptions & NSISO8601DateFormatWithMonth)
2019-10-31 07:19:31 +00:00
{
result = [result stringByAppendingString: @"MM"];
}
if (_formatOptions & NSISO8601DateFormatWithDashSeparatorInDate &&
_formatOptions & NSISO8601DateFormatWithDay)
2019-10-31 07:19:31 +00:00
{
result = [result stringByAppendingString: @"-"];
}
if (_formatOptions & NSISO8601DateFormatWithDay)
2019-10-31 07:19:31 +00:00
{
result = [result stringByAppendingString: @"dd"];
}
// Build time...
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'"];
}
if (_formatOptions & NSISO8601DateFormatWithTime)
2019-10-31 07:19:31 +00:00
{
if (_formatOptions & NSISO8601DateFormatWithColonSeparatorInTime)
2019-10-31 07:19:31 +00:00
{
result = [result stringByAppendingString: @"HH:mm:ss"];
}
else
{
result = [result stringByAppendingString: @"HHmmss"];
}
}
if (_formatOptions & NSISO8601DateFormatWithFractionalSeconds)
2019-10-31 07:19:31 +00:00
{
result = [result stringByAppendingString: @".SSSSSS"];
}
if (_formatOptions & NSISO8601DateFormatWithTimeZone)
2019-10-31 07:19:31 +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
}
- (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