-
Notifications
You must be signed in to change notification settings - Fork 5
/
DFDateFormatterFactory.m
98 lines (74 loc) · 3.31 KB
/
DFDateFormatterFactory.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
//
// DFDateFormatterFactory.m
// GitHub Community
//
// Created by Douglas Fischer on 4/20/13.
// Copyright (c) 2013 XT3 Studios. All rights reserved.
//
#import "DFDateFormatterFactory.h"
// Define the default cache limit only if not defined by the host application code
#ifndef DFDATEFORMATTERFACTORY_CACHE_LIMIT
#define DFDATEFORMATTERFACTORY_CACHE_LIMIT 15
#endif
@implementation DFDateFormatterFactory
- (id)init {
self = [super init];
if (self) {
loadedDataFormatters = [[NSCache alloc] init];
loadedDataFormatters.countLimit = DFDATEFORMATTERFACTORY_CACHE_LIMIT;
}
return self;
}
#pragma mark -
#pragma mark Static Methods
+ (DFDateFormatterFactory *)sharedFactory {
static DFDateFormatterFactory *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[DFDateFormatterFactory alloc] init];
});
return sharedInstance;
}
#pragma mark -
#pragma mark NSDateFormatter Initialization Methods
- (NSDateFormatter *)dateFormatterWithFormat:(NSString *)format andLocale:(NSLocale *)locale {
@synchronized(self) {
NSString *key = [NSString stringWithFormat:@"%@|%@", format, locale.localeIdentifier];
NSDateFormatter *dateFormatter = [loadedDataFormatters objectForKey:key];
if (!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = format;
dateFormatter.locale = locale;
[loadedDataFormatters setObject:dateFormatter forKey:key];
}
return dateFormatter;
}
}
- (NSDateFormatter *)dateFormatterWithFormat:(NSString *)format andLocaleIdentifier:(NSString *)localeIdentifier {
return [self dateFormatterWithFormat:format andLocale:[[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier]];
}
- (NSDateFormatter *)dateFormatterWithFormat:(NSString *)format
{
return [self dateFormatterWithFormat:format andLocale:[NSLocale currentLocale]];
}
- (NSDateFormatter *)dateFormatterWithDateStyle:(NSDateFormatterStyle)dateStyle timeStyle:(NSDateFormatterStyle)timeStyle andLocale:(NSLocale *)locale {
@synchronized(self) {
NSString *key = [NSString stringWithFormat:@"d%lu|t%lu%@", (unsigned long)dateStyle, (unsigned long)timeStyle, locale.localeIdentifier];
NSDateFormatter *dateFormatter = [loadedDataFormatters objectForKey:key];
if (!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = dateStyle;
dateFormatter.timeStyle = timeStyle;
dateFormatter.locale = locale;
[loadedDataFormatters setObject:dateFormatter forKey:key];
}
return dateFormatter;
}
}
- (NSDateFormatter *)dateFormatterWithDateStyle:(NSDateFormatterStyle)dateStyle timeStyle:(NSDateFormatterStyle)timeStyle andLocaleIdentifier:(NSString *)localeIdentifier {
return [self dateFormatterWithDateStyle:dateStyle timeStyle:timeStyle andLocale:[[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier]];
}
- (NSDateFormatter *)dateFormatterWithDateStyle:(NSDateFormatterStyle)dateStyle andTimeStyle:(NSDateFormatterStyle)timeStyle {
return [self dateFormatterWithDateStyle:dateStyle timeStyle:timeStyle andLocale:[NSLocale currentLocale]];
}
@end