Commits

Peter Hosey  committed 61d2959

Cache time zones by offset. Gets another speed-up of about 1.5×, and makes this now faster than C stdlib.

  • Participants
  • Parent commits cadf0f0

Comments (0)

Files changed (1)

File ISO8601DateFormatter.m

 
 @end
 
+static NSMutableDictionary *timeZonesByOffset;
+
 @implementation ISO8601DateFormatter
 
++ (void) initialize {
+	if (!timeZonesByOffset) {
+		timeZonesByOffset = [[NSMutableDictionary alloc] init];
+	}
+}
+
 - (NSCalendar *) makeCalendarWithDesiredConfiguration {
 	NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
 	calendar.firstWeekday = 2; //Monday
 								if (negative) tz_minute = -tz_minute;
 							}
 
-							timeZone = [NSTimeZone timeZoneForSecondsFromGMT:(tz_hour * 3600) + (tz_minute * 60)];
+							NSTimeInterval timeZoneOffset = (tz_hour * 3600) + (tz_minute * 60);
+							NSNumber *offsetNum = [NSNumber numberWithDouble:timeZoneOffset];
+							timeZone = [timeZonesByOffset objectForKey:offsetNum];
+							if (!timeZone) {
+								timeZone = [NSTimeZone timeZoneForSecondsFromGMT:timeZoneOffset];
+								if (timeZone)
+									[timeZonesByOffset setObject:timeZone forKey:offsetNum];
+							}
 						}
 				}
 			}