Created
February 16, 2015 02:30
-
-
Save alexcurylo/1b1147bc94e8bc59d84f to your computer and use it in GitHub Desktop.
Threadsafe Date Formatting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (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