Skip to content

Instantly share code, notes, and snippets.

@alexcurylo
Created February 16, 2015 02:30
Show Gist options
  • Save alexcurylo/1b1147bc94e8bc59d84f to your computer and use it in GitHub Desktop.
Save alexcurylo/1b1147bc94e8bc59d84f to your computer and use it in GitHub Desktop.
Threadsafe Date Formatting
+ (NSDateFormatter *)dateReader
{
NSMutableDictionary *dictionary = [[NSThread currentThread] threadDictionary];
NSDateFormatter *dateReader = [dictionary objectForKey:@"SCDateReader"];
if (!dateReader)
{
dateReader = [[[NSDateFormatter alloc] init] autorelease];
dateReader.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
dateReader.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
dateReader.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss Z";
[dictionary setObject:dateReader forKey:@"SCDateReader"];
}
return dateReader;
}
+ (NSDateFormatter *)dateWriter
{
NSMutableDictionary *dictionary = [[NSThread currentThread] threadDictionary];
NSDateFormatter *dateWriter = [dictionary objectForKey:@"SCDateWriter"];
if (!dateWriter)
{
dateWriter = [[[NSDateFormatter alloc] init] autorelease];
dateWriter.locale = [NSLocale currentLocale];
dateWriter.timeZone = [NSTimeZone defaultTimeZone];
dateWriter.dateStyle = NSDateFormatterMediumStyle;
[dictionary setObject:dateWriter forKey:@"SCDateWriter"];
}
return dateWriter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment