-
Notifications
You must be signed in to change notification settings - Fork 41
/
NSDate+Components.m
191 lines (151 loc) · 5.49 KB
/
NSDate+Components.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
#import "NSDate+Components.h"
static NSDateFormatter* static_standardDateFormatter = nil ;
@implementation NSDate (Components)
+ (NSDateFormatter*)standardDateFormatter {
if (!static_standardDateFormatter) {
static_standardDateFormatter = [[NSDateFormatter alloc] init] ;
// Get current date in the international format YYYY-MM-DD HH:MM:SS ±HHMM
[static_standardDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss "] ;
}
return static_standardDateFormatter ;
}
+ (NSDate*)dateWithYear:(NSInteger)year
month:(NSInteger)month
day:(NSInteger)day
hour:(NSInteger)hour
minute:(NSInteger)minute
second:(NSInteger)second
timeZoneOffset:(NSInteger)timeZoneOffset {
/* There may be a better way to do this. This method was originally
written a long time ago, using deprecated NSDate methods. I just
replaced the deprecations with NSDateFormatter. */
NSMutableString* dateString = [[[self standardDateFormatter] stringFromDate:[NSDate date]] mutableCopy] ;
NSString* substitution ;
if (year != NSNotFound) {
substitution = [NSString stringWithFormat:@"%04ld", (long)year] ;
[dateString replaceCharactersInRange:NSMakeRange(0,4)
withString:substitution] ;
}
if (month != NSNotFound) {
substitution = [NSString stringWithFormat:@"%02ld", (long)month] ;
[dateString replaceCharactersInRange:NSMakeRange(5,2)
withString:substitution] ;
}
if (day != NSNotFound) {
substitution = [NSString stringWithFormat:@"%02ld", (long)day] ;
[dateString replaceCharactersInRange:NSMakeRange(8,2)
withString:substitution] ;
}
if (hour != NSNotFound) {
substitution = [NSString stringWithFormat:@"%02ld", (long)hour] ;
[dateString replaceCharactersInRange:NSMakeRange(11,2)
withString:substitution] ;
}
if (minute != NSNotFound) {
substitution = [NSString stringWithFormat:@"%02ld", (long)minute] ;
[dateString replaceCharactersInRange:NSMakeRange(14,2)
withString:substitution] ;
}
if (second != NSNotFound) {
substitution = [NSString stringWithFormat:@"%02ld", (long)second] ;
[dateString replaceCharactersInRange:NSMakeRange(17,2)
withString:substitution] ;
}
if (timeZoneOffset != NSNotFound) {
unichar timeZoneSign = (timeZoneOffset > 0) ? '+': '-' ;
NSInteger timeZoneMagnitude = abs((int)timeZoneOffset) ;
NSInteger timeZoneHours = timeZoneMagnitude/3600 ;
NSInteger timeZoneMinutes = (timeZoneMagnitude - 3600*timeZoneHours)/60 ;
substitution = [NSString stringWithFormat:@"%c%02ld%02ld",
timeZoneSign,
(long)timeZoneHours,
(long)timeZoneMinutes] ;
[dateString replaceCharactersInRange:NSMakeRange(20,5)
withString:substitution] ;
}
// Create from the mutated string a new date
NSDate* date = [[self standardDateFormatter] dateFromString:dateString] ;
[dateString release] ;
return date ;
}
- (NSString*)yearString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy"];
NSString* string = [dateFormatter stringFromDate:self] ;
[dateFormatter release] ;
return string ;
}
- (NSString*)monthStringWithLeadingZero:(BOOL)leadingZero {
NSString* formatString = leadingZero ? @"MM" : @"M" ;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];
NSString* string = [dateFormatter stringFromDate:self] ;
[dateFormatter release] ;
return string ;
}
- (NSString*)monthString {
return [self monthStringWithLeadingZero:YES] ;
}
- (NSString*)monthStringWithoutLeadingZero {
return [self monthStringWithLeadingZero:NO] ;
}
- (NSString*)dayStringWithLeadingZero:(BOOL)leadingZero {
NSString* formatString = leadingZero ? @"dd" : @"d" ;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];
NSString* string = [dateFormatter stringFromDate:self] ;
[dateFormatter release] ;
return string ;
}
- (NSString*)dayString {
return [self dayStringWithLeadingZero:YES] ;
}
- (NSString*)dayStringWithoutLeadingZero {
return [self dayStringWithLeadingZero:NO] ;
}
- (NSString*)hourString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH"];
NSString* string = [dateFormatter stringFromDate:self] ;
[dateFormatter release] ;
return string ;
}
- (NSString*)minuteString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm"];
NSString* string = [dateFormatter stringFromDate:self] ;
[dateFormatter release] ;
return string ;
}
- (NSString*)secondString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"ss"];
NSString* string = [dateFormatter stringFromDate:self] ;
[dateFormatter release] ;
return string ;
}
- (NSInteger)year {
return [[self yearString] integerValue] ;
}
- (NSInteger)month {
return [[self monthString] integerValue] ;
}
- (NSInteger)day {
return [[self dayString] integerValue] ;
}
- (NSInteger)hour {
return [[self hourString] integerValue] ;
}
- (NSInteger)minute {
return [[self minuteString] integerValue] ;
}
- (NSInteger)second {
return [[self secondString] integerValue] ;
}
// Todo: Change *all* of the above methods to use NSDateFormatter, like -minutesString does.
// (This is the method now recommended by Apple.)
// This date format shows how to get time with milliseconds:
// @"yyyy-MM-dd HH:mm:ss.SSS"];
// For more format specifiers:
// http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns
@end