-
Notifications
You must be signed in to change notification settings - Fork 1
/
NSDateFormatter+cache.m
121 lines (98 loc) · 4.22 KB
/
NSDateFormatter+cache.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// NSDateFormatter+cache.m
//
// Created by Jonathan Dalrymple on 25/11/2013.
// Copyright (c) 2014 Float-right Ltd. All rights reserved.
//
#import "NSDateFormatter+cache.h"
@implementation NSDateFormatter (FRDateFormatter_cache)
#pragma mark - The NSDateFormatter cache
+ (NSCache *)FRDateFormatter_cache
{
static NSCache *cache;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
cache = [[NSCache alloc] init];
});
return cache;
}
#pragma mark - Handle current locale changes
+ (void)clearCachedDateFormattersWhenCurrentLocaleChanges {
static id observer;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
observer = [[NSNotificationCenter defaultCenter] addObserverForName:NSCurrentLocaleDidChangeNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
[[self FRDateFormatter_cache] removeAllObjects];
}];
});
}
#pragma mark - Generate cache keys
+ (NSString *)cacheKeyWithFormat:(NSString *)format
timezone:(NSString *)timezone
locale:(NSString *)locale {
return [NSString stringWithFormat:@"FRDateFormatter-%@-%@-%@",format,timezone,locale];
}
#pragma mark - Dates to Strings
+ (NSString *)stringWithFormat:(NSString *)aFormat
date:(NSDate *)aDate {
return [self stringWithFormat:aFormat
timezone:nil
locale:nil
date:aDate];
}
+ (NSString *)stringWithFormat:(NSString *)aFormat
timezone:(NSString *)timezoneName
locale:(NSString *)localeIdentifier
date:(NSDate *)aDate {
NSDateFormatter *dateFormatter;
NSString *key = [self cacheKeyWithFormat:aFormat
timezone:timezoneName
locale:localeIdentifier];
if (!(dateFormatter = [[self FRDateFormatter_cache] objectForKey:key])) {
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = aFormat;
if (timezoneName) {
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:timezoneName];
}
if (localeIdentifier) {
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:localeIdentifier];
}
[[self FRDateFormatter_cache] setObject:dateFormatter
forKey:key];
}
return [dateFormatter stringFromDate:aDate];
}
#pragma mark - Strings to dates
+ (NSDate *)dateWithFormat:(NSString *)aFormat
string:(NSString *)aDateString {
return [self dateWithFormat:aFormat
timezone:nil
locale:nil
string:aDateString];
}
+ (NSDate *)dateWithFormat:(NSString *)aFormat
timezone:(NSString *)timezoneName
locale:(NSString *)localeIdentifier
string:(NSString *)aDateString {
NSDateFormatter *dateFormatter;
NSString *key = [self cacheKeyWithFormat:aFormat
timezone:timezoneName
locale:localeIdentifier];
if (!(dateFormatter = [[self FRDateFormatter_cache] objectForKey:key])) {
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = aFormat;
if (timezoneName) {
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:timezoneName];
}
if (localeIdentifier) {
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:localeIdentifier];
}
[[self FRDateFormatter_cache] setObject:dateFormatter
forKey:key];
}
return [dateFormatter dateFromString:aDateString];
}
@end