-
Notifications
You must be signed in to change notification settings - Fork 4
/
NSDateFormatter+TBL.m
73 lines (63 loc) · 3.23 KB
/
NSDateFormatter+TBL.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Thread safe reusable NSDateFormatter based on this post http://mobile.dzone.com/news/ios-threadsafe-date-formatting
#import "NSDateFormatter+TBL.h"
@implementation NSDateFormatter (TBL)
+(NSDateFormatter *)dateFormatterWithFormat:(NSString *)format {
return [self dateFormatterWithFormat:format timeZone:nil];
}
+(NSDateFormatter *)dateFormatterWithFormat:(NSString *)format timeZone:(NSTimeZone *)timeZone {
return [self dateFormatterWithFormat:format timeZone:timeZone locale:nil];
}
+(NSDateFormatter *)dateFormatterWithFormat:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale {
if (format == nil || [format isEqualToString:@""]) return nil;
NSString *key = [NSString stringWithFormat:@"NSDateFormatter-tz-%@-fmt-%@-loc-%@", [timeZone abbreviation], format, [locale localeIdentifier]];
NSMutableDictionary* dictionary = [[NSThread currentThread] threadDictionary];
NSDateFormatter* dateFormatter = [dictionary objectForKey:key];
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
[dictionary setObject:dateFormatter forKey:key];
#if !__has_feature(objc_arc)
[dateFormatter autorelease];
#endif
}
if (locale != nil) [dateFormatter setLocale:locale]; // this may change so don't cache
if (timeZone != nil) [dateFormatter setTimeZone:timeZone]; // this may change
return dateFormatter;
}
+(NSDateFormatter *)dateFormatterWithDateStyle:(NSDateFormatterStyle)style {
return [self dateFormatterWithDateStyle:style timeZone:nil];
}
+(NSDateFormatter *)dateFormatterWithDateStyle:(NSDateFormatterStyle)style timeZone:(NSTimeZone *)timeZone {
NSString *key = [NSString stringWithFormat:@"NSDateFormatter-%@-dateStyle-%d", [timeZone abbreviation], (int)style];
NSMutableDictionary* dictionary = [[NSThread currentThread] threadDictionary];
NSDateFormatter* dateFormatter = [dictionary objectForKey:key];
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:style];
[dictionary setObject:dateFormatter forKey:key];
#if !__has_feature(objc_arc)
[dateFormatter autorelease];
#endif
}
if (timeZone != nil) [dateFormatter setTimeZone:timeZone]; // this may change so don't cache
return dateFormatter;
}
+(NSDateFormatter *)dateFormatterWithTimeStyle:(NSDateFormatterStyle)style {
return [self dateFormatterWithTimeStyle:style timeZone:nil];
}
+(NSDateFormatter *)dateFormatterWithTimeStyle:(NSDateFormatterStyle)style timeZone:(NSTimeZone *)timeZone {
NSString *key = [NSString stringWithFormat:@"NSDateFormatter-%@-timeStyle-%d", [timeZone abbreviation], (int)style];
NSMutableDictionary* dictionary = [[NSThread currentThread] threadDictionary];
NSDateFormatter* dateFormatter = [dictionary objectForKey:key];
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:style];
[dictionary setObject:dateFormatter forKey:key];
#if !__has_feature(objc_arc)
[dateFormatter autorelease];
#endif
}
if (timeZone != nil) [dateFormatter setTimeZone:timeZone]; // this may change so don't cache
return dateFormatter;
}
@end