Skip to content

Commit

Permalink
Merge 7b189ae into fed060f
Browse files Browse the repository at this point in the history
  • Loading branch information
ChocoChipset committed Mar 27, 2014
2 parents fed060f + 7b189ae commit 195c188
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
19 changes: 13 additions & 6 deletions ISO8601DateFormatter.m
Expand Up @@ -25,6 +25,8 @@
#define ISO_TIMEZONE_OFFSET_FORMAT_NO_SEPARATOR @"%+.2d%.2d"
#define ISO_TIMEZONE_OFFSET_FORMAT_WITH_SEPARATOR @"%+.2d%C%.2d"

static NSString * const ISO8601TwoCharIntegerFormat = @"%.2d";

@interface ISO8601DateFormatter ()
+ (void) createGlobalCachesThatDoNotAlreadyExist;
//Used when a memory warning occurs (if at least one ISO 8601 Date Formatter exists at the time).
Expand Down Expand Up @@ -778,12 +780,17 @@ - (NSString *) stringFromDate:(NSDate *)date formatString:(NSString *)dateFormat
if (offset == 0)
str = [str stringByAppendingString:ISO_TIMEZONE_UTC_FORMAT];
else {
int timeZoneOffsetHour = (int)(offset / 60);
int timeZoneOffsetMinute = (int)(offset % 60);
if (self.timeZoneSeparator)
str = [str stringByAppendingFormat:ISO_TIMEZONE_OFFSET_FORMAT_WITH_SEPARATOR, timeZoneOffsetHour, self.timeZoneSeparator, timeZoneOffsetMinute];
else
str = [str stringByAppendingFormat:ISO_TIMEZONE_OFFSET_FORMAT_NO_SEPARATOR, timeZoneOffsetHour, timeZoneOffsetMinute];
int timeZoneOffsetHour = abs((int)(offset / 60));
int timeZoneOffsetMinute = abs((int)(offset % 60));

if (offset > 0) str = [str stringByAppendingString:@"+"];
else str = [str stringByAppendingString:@"-"];

str = [str stringByAppendingFormat:ISO8601TwoCharIntegerFormat, timeZoneOffsetHour];

if (self.timeZoneSeparator) str = [str stringByAppendingFormat:@"%C", self.timeZoneSeparator];

str = [str stringByAppendingFormat:ISO8601TwoCharIntegerFormat, timeZoneOffsetMinute];
}
}

Expand Down
Expand Up @@ -376,6 +376,22 @@ - (void) testParsingOctober9th2013 {
STAssertNotNil(date, @"1 PM UTC on October 9th, 2013 should not be nil");
}

// https://github.com/boredzo/iso-8601-date-formatter/issues/36
- (void) testParsingFractionaryTimeZone
{
_iso8601DateFormatter.includeTime = YES;

NSTimeZone *UTCTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:gSecondsPerHour*-2.5];

NSDate *date= [NSDate dateWithTimeIntervalSinceReferenceDate:354987473.0];

NSString *expectedString = @"2012-04-01T13:07:53-0230";

NSString *string = [_iso8601DateFormatter stringFromDate:date
timeZone:UTCTimeZone];
STAssertEqualObjects(string, expectedString, @"Got wrong string for fractionary time zone");
}

- (void) testStrictModeRejectsSlashyDates {
_iso8601DateFormatter.parsesStrictly = true;

Expand Down

0 comments on commit 195c188

Please sign in to comment.