public
Description: Utilities and categories for Objective-C
Homepage:
Clone URL: git://github.com/gabriel/gh-kit.git
gabriel (author)
Mon Oct 12 17:03:25 -0700 2009
commit  488cee3aa8f928c22bb3ea6dbbf096e3022f715a
tree    e7f349947754e264a3a2c79005178d5be3898a2d
parent  66555e08bdc5fce92eaa91f41f421d8ba3addfc0
gh-kit / Classes / GHNSDate+Parsing.m
100644 131 lines (113 sloc) 5.785 kb
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
//
// GHNSDate+Parsing.m
//
// Created by Gabe on 3/18/08.
// Copyright 2008 Gabriel Handford
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
 
#import "GHNSDate+Parsing.h"
 
// Date formatters are not cached since they are not thread-safe. (Thanks Jae!)
// TODO: Investigate synchronized access to cached formatters.
 
@implementation NSDate (GHParsing)
 
+ (NSDate *)gh_parseISO8601:(NSString *)dateString {
if (!dateString) return nil;
  return [[self gh_iso8601DateFormatter] dateFromString:dateString];
}
 
+ (NSDate *)gh_parseRFC822:(NSString *)dateString {
if (!dateString) return nil;
  return [[self gh_rfc822DateFormatter] dateFromString:dateString];
}
 
+ (NSDate *)gh_parseHTTP:(NSString *)dateString {
if (!dateString) return nil;
  NSDate *parsed = nil;
  parsed = [[self gh_rfc1123DateFormatter] dateFromString:dateString];
  if (parsed) return parsed;
  parsed = [[self gh_rfc850DateFormatter] dateFromString:dateString];
  if (parsed) return parsed;
  parsed = [[self gh_ascTimeDateFormatter] dateFromString:dateString];
  return parsed;
}
 
+ (NSDate *)gh_parseTimeSinceEpoch:(id)timeSinceEpoch {
return [self gh_parseTimeSinceEpoch:timeSinceEpoch withDefault:timeSinceEpoch];
}
 
+ (NSDate *)gh_parseTimeSinceEpoch:(id)timeSinceEpoch withDefault:(id)value {
if (!timeSinceEpoch) return value;
return [NSDate dateWithTimeIntervalSince1970:[timeSinceEpoch doubleValue]];
}
 
- (NSString *)gh_formatRFC822 {
  return [[[self class] gh_rfc822DateFormatter] stringFromDate:self];
}
 
- (NSString *)gh_formatHTTP {
  return [[[self class] gh_rfc1123DateFormatter] stringFromDate:self];
}
 
- (NSString *)gh_formatISO8601 {
return [[[self class] gh_iso8601DateFormatter] stringFromDate:self];
}
 
+ (NSDateFormatter *)gh_rfc822DateFormatter {
  NSDateFormatter *gh_rfc822DateFormatter = [[[NSDateFormatter alloc] init] autorelease];
  [gh_rfc822DateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
  // Need to force US locale when generating otherwise it might not be 822 compatible
  [gh_rfc822DateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
  [gh_rfc822DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
  [gh_rfc822DateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
  return gh_rfc822DateFormatter;
}
 
+ (NSDateFormatter *)gh_iso8601DateFormatter {
  // Example: 2007-10-18T16:05:10.000Z
  NSDateFormatter *gh_is8601DateFormatter = [[[NSDateFormatter alloc] init] autorelease];
  [gh_is8601DateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
  // Need to force US locale when generating otherwise it might not be 8601 compatible
  [gh_is8601DateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
  [gh_is8601DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
  [gh_is8601DateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
  return gh_is8601DateFormatter;
}
 
+ (NSDateFormatter *)gh_rfc1123DateFormatter {
  // Example: "Wed, 01 Mar 2006 12:00:00 GMT"
  NSDateFormatter *gh_rfc1123DateFormatter = [[[NSDateFormatter alloc] init] autorelease];
  [gh_rfc1123DateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
  // Need to force US locale when generating otherwise it might not be 1123 compatible
  [gh_rfc1123DateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
  [gh_rfc1123DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
  [gh_rfc1123DateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"];
  return gh_rfc1123DateFormatter;
}
 
+ (NSDateFormatter *)gh_rfc850DateFormatter {
  // Example: Sunday, 06-Nov-94 08:49:37 GMT
  NSDateFormatter *gh_rfc850DateFormatter = [[[NSDateFormatter alloc] init] autorelease];
  [gh_rfc850DateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
  [gh_rfc850DateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
  [gh_rfc850DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
  [gh_rfc850DateFormatter setDateFormat:@"EEEE, dd-MMM-yy HH:mm:ss zzz"];
  return gh_rfc850DateFormatter;
}
 
+ (NSDateFormatter *)gh_ascTimeDateFormatter {
  // Example: Sun Nov 6 08:49:37 1994
  NSDateFormatter *gh_ascTimeDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
  [gh_ascTimeDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
  [gh_ascTimeDateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
  [gh_ascTimeDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
  [gh_ascTimeDateFormatter setDateFormat:@"EEE MMM d HH:mm:ss yyyy"];
  return gh_ascTimeDateFormatter;
}
 
@end