Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Another 5× speed-up: Create and always use a single calendar per date…
… formatter, instead of creating them and throwing them away, and make sure it always has our default time zone.
  • Loading branch information
Peter Hosey committed Oct 15, 2011
1 parent 2992c5e commit 0899cf0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
3 changes: 2 additions & 1 deletion ISO8601DateFormatter.h
Expand Up @@ -39,14 +39,15 @@ extern unichar ISO8601DefaultTimeSeparatorCharacter;
NSString *lastUsedFormatString;
NSDateFormatter *unparsingFormatter;

NSCalendar *calendar;
NSTimeZone *defaultTimeZone;
ISO8601DateFormat format;
unichar timeSeparator;
BOOL includeTime;
BOOL parsesStrictly;
}

@property(retain) NSTimeZone *defaultTimeZone;
@property(nonatomic, retain) NSTimeZone *defaultTimeZone;

#pragma mark Parsing

Expand Down
27 changes: 18 additions & 9 deletions ISO8601DateFormatter.m
Expand Up @@ -35,6 +35,10 @@ @implementation ISO8601DateFormatter

- (id) init {
if ((self = [super init])) {
calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
calendar.firstWeekday = 2; //Monday
calendar.timeZone = [NSTimeZone defaultTimeZone];

format = ISO8601DateFormatCalendar;
timeSeparator = ISO8601DefaultTimeSeparatorCharacter;
includeTime = NO;
Expand All @@ -52,6 +56,14 @@ - (void) dealloc {
}

@synthesize defaultTimeZone;
- (void) setDefaultTimeZone:(NSTimeZone *)tz {
if (defaultTimeZone != tz) {
[defaultTimeZone release];
defaultTimeZone = [tz retain];

calendar.timeZone = defaultTimeZone;
}
}

//The following properties are only here because GCC doesn't like @synthesize in category implementations.

Expand Down Expand Up @@ -120,8 +132,6 @@ - (NSDateComponents *) dateComponentsFromString:(NSString *)string timeZone:(out
return [self dateComponentsFromString:string timeZone:outTimeZone range:NULL];
}
- (NSDateComponents *) dateComponentsFromString:(NSString *)string timeZone:(out NSTimeZone **)outTimeZone range:(out NSRange *)outRange {
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
calendar.firstWeekday = 2; //Monday
NSDate *now = [NSDate date];

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
Expand Down Expand Up @@ -579,9 +589,6 @@ - (NSDate *) dateFromString:(NSString *)string timeZone:(out NSTimeZone **)outTi
return [self dateFromString:string timeZone:outTimeZone range:NULL];
}
- (NSDate *) dateFromString:(NSString *)string timeZone:(out NSTimeZone **)outTimeZone range:(out NSRange *)outRange {
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
calendar.firstWeekday = 2; //Monday

NSTimeZone *timeZone = nil;
NSDateComponents *components = [self dateComponentsFromString:string timeZone:&timeZone range:outRange];
if (outTimeZone)
Expand Down Expand Up @@ -640,6 +647,8 @@ - (NSString *) stringFromDate:(NSDate *)date formatString:(NSString *)dateFormat
if (includeTime)
dateFormat = [dateFormat stringByAppendingFormat:@"'T'%@", [self replaceColonsInString:ISO_TIME_FORMAT withTimeSeparator:self.timeSeparator]];

calendar.timeZone = timeZone;

if (dateFormat != lastUsedFormatString) {
[unparsingFormatter release];
unparsingFormatter = nil;
Expand All @@ -649,9 +658,6 @@ - (NSString *) stringFromDate:(NSDate *)date formatString:(NSString *)dateFormat
}

if (!unparsingFormatter) {
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
calendar.firstWeekday = 2; //Monday

unparsingFormatter = [[NSDateFormatter alloc] init];
unparsingFormatter.formatterBehavior = NSDateFormatterBehavior10_4;
unparsingFormatter.dateFormat = dateFormat;
Expand All @@ -668,6 +674,10 @@ - (NSString *) stringFromDate:(NSDate *)date formatString:(NSString *)dateFormat
else
str = [str stringByAppendingFormat:ISO_TIMEZONE_OFFSET_FORMAT, offset / 60, offset % 60];
}

//Undo the change we made earlier
calendar.timeZone = self.defaultTimeZone;

return str;
}

Expand All @@ -683,7 +693,6 @@ - (NSString *) stringForObjectValue:(id)value {
* http://personal.ecu.edu/mccartyr/ISOwdALG.txt
*/
- (NSString *) weekDateStringForDate:(NSDate *)date timeZone:(NSTimeZone *)timeZone {
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
calendar.timeZone = timeZone;
NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSWeekdayCalendarUnit | NSDayCalendarUnit fromDate:date];

Expand Down

0 comments on commit 0899cf0

Please sign in to comment.