-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUUDate.m
632 lines (510 loc) · 19.6 KB
/
UUDate.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//
// UUDate.m
// Useful Utilities - Extensions for NSDate
// (c) 2013, Jonathan Hays. All Rights Reserved.
//
// Smile License:
// You are free to use this code for whatever purposes you desire. The only requirement is that you smile everytime you use it.
//
// Contact: @cheesemaker or jon@threejacks.com
#import "UUDate.h"
//If you want to impement your own UU_RELEASE and UU_AUTORELEASE mechanisms in your .pch, you may do so, just remember to define UU_MEMORY_MANAGEMENT
#ifndef UU_MEMORY_MANAGEMENT
#if !__has_feature(objc_arc)
#define UU_AUTORELEASE(x) [(x) autorelease]
#define UU_RELEASE(x) [(x) release]
#else
#define UU_AUTORELEASE(x) x
#define UU_RELEASE(x) (void)(0)
#endif
#endif
#define kUUAllDateComponents (NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond)
// Localization Keys used by uuFormatAsDeltaFromNow
NSString * const kUUTimeDeltaJustNowFormat = @"Just now";
NSString * const kUUTimeDeltaSecondsSingularFormat = @"%d second ago";
NSString * const kUUTimeDeltaSecondsPluralFormat = @"%d seconds ago";
NSString * const kUUTimeDeltaMinutesSingularFormat = @"%d minute ago";
NSString * const kUUTimeDeltaMinutesPluralFormat = @"%d minutes ago";
NSString * const kUUTimeDeltaHoursSingularFormat = @"%d hour ago";
NSString * const kUUTimeDeltaHoursPluralFormat = @"%d hours ago";
NSString * const kUUTimeDeltaDaysSingularFormat = @"%d day ago";
NSString * const kUUTimeDeltaDaysPluralFormat = @"%d days ago";
NSString * const kUUTimeDeltaJustNowFormatKey = @"UUTimeDeltaJustNowFormatKey";
NSString * const kUUTimeDeltaSecondsSingularFormatKey = @"UUTimeDeltaSecondsSingularFormatKey";
NSString * const kUUTimeDeltaSecondsPluralFormatKey = @"UUTimeDeltaSecondsPluralFormatKey";
NSString * const kUUTimeDeltaMinutesSingularFormatKey = @"UUTimeDeltaMinutesSingularFormatKey";
NSString * const kUUTimeDeltaMinutesPluralFormatKey = @"UUTimeDeltaMinutesPluralFormatKey";
NSString * const kUUTimeDeltaHoursSingularFormatKey = @"UUTimeDeltaHoursSingularFormatKey";
NSString * const kUUTimeDeltaHoursPluralFormatKey = @"UUTimeDeltaHoursPluralFormatKey";
NSString * const kUUTimeDeltaDaysSingularFormatKey = @"UUTimeDeltaDaysSingularFormatKey";
NSString * const kUUTimeDeltaDaysPluralFormatKey = @"UUTimeDeltaDaysPluralFormatKey";
// Localization Keys used by uuDayOfMonthSuffix
NSString * const kUUDayOfMonthSuffixFirst = @"st";
NSString * const kUUDayOfMonthSuffixSecond = @"nd";
NSString * const kUUDayOfMonthSuffixThird = @"rd";
NSString * const kUUDayOfMonthSuffixNth = @"th";
NSString * const kUUDayOfMonthSuffixFirstKey = @"UUDayOfMonthSuffixFirstKey";
NSString * const kUUDayOfMonthSuffixSecondKey = @"UUDayOfMonthSuffixSecondKey";
NSString * const kUUDayOfMonthSuffixThirdKey = @"UUDayOfMonthSuffixThirdKey";
NSString * const kUUDayOfMonthSuffixNthKey = @"UUDayOfMonthSuffixNthKey";
// Common Date Formats
NSString * const kUURFC3339DateTimeFormatter = @"yyyy-MM-dd'T'HH:mm:ssZZ";
NSString * const kUURFC3339DateTimeAlternateFormatter = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z";
NSString * const kUUISO8601DateFormatter = @"yyyy-MM-dd";
NSString * const kUUISO8601TimeFormatter = @"HH:mm:ss";
NSString * const kUUISO8601DateTimeFormatter = @"yyyy-MM-dd HH:mm:ss";
NSString * const kUUTimeOfDayDateformatter = @"h:mm a";
NSString * const kUUHourFormatter = @"h";
NSString * const kUUMinuteFormatter = @"mm";
NSString * const kUUDayOfMonthDateFormatter = @"d";
NSString * const kUUNumericMonthOfYearDateFormatter = @"L";
NSString * const kUUShortMonthOfYearDateFormatter = @"LLL";
NSString * const kUULongMonthOfYearDateFormatter = @"LLLL";
NSString * const kUUDayOfWeekDateFormatter = @"EEEE";
NSString * const kUUDayOfWeekShortDateFormatter = @"EE";
NSString * const kUULongYearFormatter = @"yyyy";
NSString * const kUUShortYearFormatter = @"yy";
NSString * const kUUShortDayMonthFormatter = @"EE LLL dd";
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// Date Format Caching
static NSMutableDictionary* theSharedDateFormatterCache = nil;
static NSTimeZone* theDefaultTimeZone = nil;
@implementation NSDate (UUStringFormatters)
// Instance Methods
- (NSString*) uuRfc3339String
{
NSString* string = [self uuStringFromDate:kUURFC3339DateTimeFormatter timeZone:nil];
if (!string)
string = [self uuStringFromDate:kUURFC3339DateTimeAlternateFormatter timeZone:nil];
return string;
}
- (NSString*) uuRfc3339StringForUTCTimeZone
{
NSTimeZone* utcTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSString* string = [self uuStringFromDate:kUURFC3339DateTimeFormatter timeZone:utcTimeZone];
if (!string)
string = [self uuStringFromDate:kUURFC3339DateTimeAlternateFormatter timeZone:utcTimeZone];
return string;
}
- (NSString*) uuRfc3339StringForTimeZone:(NSTimeZone*)timeZone
{
NSString* string = [self uuStringFromDate:kUURFC3339DateTimeFormatter timeZone:timeZone];
if (!string)
string = [self uuStringFromDate:kUURFC3339DateTimeAlternateFormatter timeZone:timeZone];
return string;
}
- (NSString*) uuIso8601DateTimeString
{
return [self uuStringFromDate:kUUISO8601DateTimeFormatter timeZone:nil];
}
- (NSString*) uuIso8601StringForUTCTimeZone
{
NSTimeZone* utcTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
return [self uuStringFromDate:kUUISO8601DateTimeFormatter timeZone:utcTimeZone];
}
- (NSString*) uuIso8601StringForTimeZone:(NSTimeZone*)timeZone
{
return [self uuStringFromDate:kUUISO8601DateTimeFormatter timeZone:timeZone];
}
- (NSString*) uuIso8601DateString
{
return [self uuStringFromDate:kUUISO8601DateFormatter timeZone:nil];
}
- (NSString*) uuIso8601TimeString
{
return [self uuStringFromDate:kUUISO8601TimeFormatter timeZone:nil];
}
- (NSString*) uuDayOfWeekShort
{
return [self uuStringFromDate:kUUDayOfWeekShortDateFormatter timeZone:nil];
}
- (NSString*) uuDayOfWeekDayShort
{
NSString* str = [self uuStringFromDate:kUUDayOfMonthDateFormatter timeZone:nil];
int dayValue = [str intValue];
NSString* day = [str stringByAppendingString:[NSDate uuDayOfMonthSuffix:dayValue]];
return [NSString stringWithFormat:@"%@ %@ %@", [self uuDayOfWeekShort], [self uuShortMonthOfYear], day];
}
- (NSString*) uuDayOfWeek
{
return [self uuStringFromDate:kUUDayOfWeekDateFormatter timeZone:nil];
}
- (NSUInteger) uuNumericMonthOfYear
{
NSCalendar* calendar = [NSCalendar currentCalendar];
return [calendar component:NSCalendarUnitMonth fromDate:self];
}
- (NSUInteger) uuNumericWeekOfYear
{
NSCalendar* calendar = [NSCalendar currentCalendar];
return [calendar component:NSCalendarUnitWeekOfYear fromDate:self];
}
- (NSUInteger) uuNumericWeekOfMonth
{
NSCalendar* calendar = [NSCalendar currentCalendar];
return [calendar component:NSCalendarUnitWeekOfMonth fromDate:self];
}
- (NSUInteger) uuNumericDayOfMonth
{
NSCalendar* calendar = [NSCalendar currentCalendar];
return [calendar component:NSCalendarUnitDay fromDate:self];
}
- (NSString*) uuLongMonthOfYear
{
return [self uuStringFromDate:kUULongMonthOfYearDateFormatter timeZone:nil];
}
- (NSString*) uuShortMonthOfYear
{
return [[NSDateFormatter uuCachedDateFormatter:kUUShortMonthOfYearDateFormatter] stringFromDate:self];
}
- (NSString*) uuDayOfMonthShort
{
return [self uuStringFromDate:kUUDayOfMonthDateFormatter timeZone:nil];
}
- (NSString*) uuDayOfMonth
{
NSString* str = [self uuStringFromDate:kUUDayOfMonthDateFormatter timeZone:nil];
int dayValue = [str intValue];
return [str stringByAppendingString:[NSDate uuDayOfMonthSuffix:dayValue]];
}
- (NSString*) uuShortYear
{
return [self uuStringFromDate:kUUShortYearFormatter timeZone:nil];
}
- (NSString*) uuLongYear
{
return [self uuStringFromDate:kUULongYearFormatter timeZone:nil];
}
- (NSString*) uuTimeOfDay
{
return [self uuStringFromDate:kUUTimeOfDayDateformatter timeZone:nil];
}
- (NSString*) uuHour
{
return [self uuStringFromDate:kUUHourFormatter timeZone:nil];
}
- (NSString*) uuMinute
{
return [self uuStringFromDate:kUUMinuteFormatter timeZone:nil];
}
- (NSString*) uuFormatAsDeltaFromNow
{
return [self uuFormatAsDeltaFromNow:YES];
}
- (NSString*) uuFormatAsDeltaFromNow:(BOOL)adjustTimeZone
{
NSTimeInterval timeZoneDiff = 0;
if (adjustTimeZone)
{
timeZoneDiff = [[NSTimeZone systemTimeZone] secondsFromGMT];
}
NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:self] - timeZoneDiff;
return [NSDate uuFormatTimeDelta:interval];
}
// Private Instance Methods
- (NSString*) uuStringFromDate:(NSString*)formatter timeZone:(NSTimeZone*)timeZone
{
NSDateFormatter* df = [NSDateFormatter uuCachedDateFormatter:formatter];
if (timeZone)
{
df.timeZone = timeZone;
}
return [df stringFromDate:self];
}
// Class Methods
const double kUUSecondsPerDay = (60 * 60 * 24);
+ (NSString*) uuFormatTimeDelta:(NSTimeInterval)interval
{
double days = (interval / kUUSecondsPerDay);
if (days < 1)
{
double hours = days * 24;
if (hours < 1)
{
double minutes = hours * 60;
if (minutes < 1)
{
double seconds = minutes * 60;
if (seconds >= 1 && seconds < 2)
{
return [self uuFormatDelta:kUUTimeDeltaSecondsSingularFormatKey defaultFormatter:kUUTimeDeltaSecondsSingularFormat value:seconds];
}
else if (seconds <= 0)
{
return [[NSBundle mainBundle] localizedStringForKey:kUUTimeDeltaJustNowFormatKey value:kUUTimeDeltaJustNowFormat table:nil];
}
else
{
return [self uuFormatDelta:kUUTimeDeltaSecondsPluralFormatKey defaultFormatter:kUUTimeDeltaSecondsPluralFormat value:seconds];
}
}
else if (minutes >= 1 && minutes < 2)
{
return [self uuFormatDelta:kUUTimeDeltaMinutesSingularFormatKey defaultFormatter:kUUTimeDeltaMinutesSingularFormat value:minutes];
}
else
{
return [self uuFormatDelta:kUUTimeDeltaMinutesPluralFormatKey defaultFormatter:kUUTimeDeltaMinutesPluralFormat value:minutes];
}
}
else if (hours >= 1 && hours < 2)
{
return [self uuFormatDelta:kUUTimeDeltaHoursSingularFormatKey defaultFormatter:kUUTimeDeltaHoursSingularFormat value:hours];
}
else
{
return [self uuFormatDelta:kUUTimeDeltaHoursPluralFormatKey defaultFormatter:kUUTimeDeltaHoursPluralFormat value:hours];
}
}
else if (days >= 1 && days < 2)
{
return [self uuFormatDelta:kUUTimeDeltaDaysSingularFormatKey defaultFormatter:kUUTimeDeltaDaysSingularFormat value:days];
}
else
{
return [self uuFormatDelta:kUUTimeDeltaDaysPluralFormatKey defaultFormatter:kUUTimeDeltaDaysPluralFormat value:days];
}
}
+ (NSString*) uuFormatDelta:(NSString*)key defaultFormatter:(NSString*)defaultFormatter value:(int)value
{
NSString* formatter = [[NSBundle mainBundle] localizedStringForKey:key value:defaultFormatter table:nil];
return [NSString stringWithFormat:formatter, value];
}
+ (NSString*) uuDayOfMonthSuffix:(int)dayOfMonth
{
switch (dayOfMonth)
{
case 1:
case 21:
case 31:
return [[NSBundle mainBundle] localizedStringForKey:kUUDayOfMonthSuffixFirstKey value:kUUDayOfMonthSuffixFirst table:nil];
case 2:
case 22:
return [[NSBundle mainBundle] localizedStringForKey:kUUDayOfMonthSuffixSecondKey value:kUUDayOfMonthSuffixSecond table:nil];
case 3:
case 23:
return [[NSBundle mainBundle] localizedStringForKey:kUUDayOfMonthSuffixThirdKey value:kUUDayOfMonthSuffixThird table:nil];
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
case 24:
case 25:
case 26:
case 27:
case 28:
case 29:
case 30:
return [[NSBundle mainBundle] localizedStringForKey:kUUDayOfMonthSuffixNthKey value:kUUDayOfMonthSuffixNth table:nil];
default:
return @"";
}
}
@end
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// Date Creation
@implementation NSDate (UUDateCreation)
- (NSDate*) uuDateWithHour:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second
{
NSCalendar* cal = [NSCalendar currentCalendar];
NSCalendarUnit units = (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay);
NSDateComponents* dc = [cal components:units fromDate:self];
dc.hour = 0;
dc.minute = 0;
dc.second = 0;
return [cal dateFromComponents:dc];
}
- (NSDate*) uuStartOfDay
{
return [self uuDateWithHour:0 minute:0 second:0];
}
@end
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// Date Comparison
@implementation NSDate (UUDateComparison)
#define kUUDateOnlyComponents (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
- (BOOL) uuIsDatePartEqual:(NSDate*)other
{
NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* thisDate = [cal components:kUUDateOnlyComponents fromDate:self];
NSDateComponents* otherDate = [cal components:kUUDateOnlyComponents fromDate:other];
return (thisDate.year == otherDate.year && thisDate.month == otherDate.month && thisDate.day == otherDate.day);
}
- (BOOL) uuIsToday
{
return [self uuIsDatePartEqual:[NSDate date]];
}
- (BOOL) uuIsTomorrow
{
return [self uuIsDatePartEqual:[[NSDate date] uuAddDays:1]];
}
- (BOOL) uuIsYesterday
{
return [self uuIsDatePartEqual:[[NSDate date] uuAddDays:-1]];
}
- (BOOL) uuIsMorning
{
return ![self uuIsEvening];
}
- (BOOL) uuIsEvening
{
NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* dc = [cal components:kUUAllDateComponents fromDate:self];
return (dc.hour >= 12);
}
@end
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// Date Math
@implementation NSDate (UUDateMath)
- (NSDate*) uuAddSeconds:(NSInteger)seconds
{
NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* dc = [cal components:kUUAllDateComponents fromDate:self];
dc.second += seconds;
return [cal dateFromComponents:dc];
}
- (NSDate*) uuAddMinutes:(NSInteger)minutes
{
NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* dc = [cal components:kUUAllDateComponents fromDate:self];
dc.minute += minutes;
return [cal dateFromComponents:dc];
}
- (NSDate*) uuAddHours:(NSInteger)hours
{
NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* dc = [cal components:kUUAllDateComponents fromDate:self];
dc.hour += hours;
return [cal dateFromComponents:dc];
}
- (NSDate*) uuAddDays:(NSInteger)days
{
NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* dc = [cal components:kUUAllDateComponents fromDate:self];
dc.day += days;
return [cal dateFromComponents:dc];
}
- (NSDate*) uuAddWeeks:(NSInteger)weeks
{
return [self uuAddDays:(weeks * 7)];
}
- (NSDate*) uuAddMonths:(NSInteger)months
{
NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* dc = [cal components:kUUAllDateComponents fromDate:self];
dc.month += months;
return [cal dateFromComponents:dc];
}
- (NSDate*) uuAddYears:(NSInteger)years
{
NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* dc = [cal components:kUUAllDateComponents fromDate:self];
dc.year += years;
return [cal dateFromComponents:dc];
}
@end
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// Date Parsing
@implementation NSDate (UUDateParsing)
+ (NSDate*) uuDateFromRfc3339String:(NSString*)string
{
NSDate* date = [self uuDateFromString:string withFormat:kUURFC3339DateTimeFormatter timeZone:theDefaultTimeZone];
if (!date)
date = [self uuDateFromString:string withFormat:kUURFC3339DateTimeAlternateFormatter timeZone:theDefaultTimeZone];
return date;
}
+ (NSDate*) uuDateFromRfc3339StringUTCTimezone:(NSString*)string
{
NSTimeZone* utcTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate* date = [self uuDateFromString:string withFormat:kUURFC3339DateTimeFormatter timeZone:utcTimeZone];
if (!date)
date = [self uuDateFromString:string withFormat:kUURFC3339DateTimeAlternateFormatter timeZone:utcTimeZone];
return date;
}
+ (NSDate*) uuDateFromRfc3339String:(NSString*)string timeZone:(NSTimeZone*)timeZone
{
NSDate* date = [self uuDateFromString:string withFormat:kUURFC3339DateTimeFormatter timeZone:timeZone];
if (!date)
date = [self uuDateFromString:string withFormat:kUURFC3339DateTimeAlternateFormatter timeZone:timeZone];
return date;
}
+ (NSDate*) uuDateFromIso8601String:(NSString*)string
{
NSDate* date = [self uuDateFromString:string withFormat:kUUISO8601DateTimeFormatter timeZone:nil];
if (!date)
date = [self uuDateFromString:string withFormat:kUUISO8601DateFormatter timeZone:nil];
return date;
}
+ (NSDate*) uuDateFromIso8601StringUTCTimezone:(NSString*)string
{
NSTimeZone* utcTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate* date = [self uuDateFromString:string withFormat:kUUISO8601DateTimeFormatter timeZone:utcTimeZone];
if (!date)
date = [self uuDateFromString:string withFormat:kUUISO8601DateFormatter timeZone:utcTimeZone];
return date;
}
+ (NSDate*) uuDateFromIso8601String:(NSString*)string timeZone:(NSTimeZone*)timeZone
{
NSDate* date = [self uuDateFromString:string withFormat:kUUISO8601DateTimeFormatter timeZone:timeZone];
if (!date)
date = [self uuDateFromString:string withFormat:kUUISO8601DateFormatter timeZone:timeZone];
return date;
}
+ (NSDate*) uuDateFromString:(NSString*)string withFormat:(NSString*)format
{
NSDateFormatter* df = [NSDateFormatter uuCachedDateFormatter:format];
return [df dateFromString:string];
}
+ (NSDate*) uuDateFromStringUTCTimezone:(NSString*)string withFormat:(NSString*)format
{
NSTimeZone* utcTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
return [self uuDateFromString:string withFormat:format timeZone:utcTimeZone];
}
+ (NSDate*) uuDateFromString:(NSString*)string withFormat:(NSString*)format timeZone:(NSTimeZone*)timeZone
{
NSDateFormatter* df = [NSDateFormatter uuCachedDateFormatter:format];
if (timeZone)
[df setTimeZone:timeZone];
return [df dateFromString:string];
}
@end
@implementation NSDateFormatter (UUDateFormatterCache)
+ (NSMutableDictionary*) uuSharedDateFormatterCache
{
if (theSharedDateFormatterCache == nil)
{
theSharedDateFormatterCache = [[NSMutableDictionary alloc] init];
}
return theSharedDateFormatterCache;
}
+ (NSDateFormatter*) uuCachedDateFormatter:(NSString*)dateFormat
{
NSMutableDictionary* cache = [self uuSharedDateFormatterCache];
NSDateFormatter* df = [cache valueForKey:dateFormat];
if (df == nil)
{
df = UU_AUTORELEASE([[NSDateFormatter alloc] init]);
[df setDateFormat:dateFormat];
[cache setValue:df forKey:dateFormat];
}
[df setTimeZone:theDefaultTimeZone];
return df;
}
+ (void) uuSetDefaultTimeZone:(NSTimeZone*)timeZone
{
theDefaultTimeZone = timeZone;
}
@end