forked from jerrykrinock/CategoriesObjC
/
NSDate+Components.m
170 lines (133 loc) · 4.58 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
#import "NSDate+Components.h"
@implementation NSDate (Components)
+ (NSDate*)dateWithYear:(NSInteger)year
month:(NSInteger)month
day:(NSInteger)day
hour:(NSInteger)hour
minute:(NSInteger)minute
second:(NSInteger)second
timeZoneOffset:(NSInteger)timeZoneOffset {
// Get current date in the international format YYYY-MM-DD HH:MM:SS ±HHMM
NSMutableString* dateString = [[[NSDate date] descriptionWithCalendarFormat:nil
timeZone:nil
locale:nil] 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(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 = [NSDate dateWithString: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*)monthString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM"];
NSString* string = [dateFormatter stringFromDate:self] ;
[dateFormatter release] ;
return string ;
}
- (NSString*)dayString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];
NSString* string = [dateFormatter stringFromDate:self] ;
[dateFormatter release] ;
return string ;
}
- (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 ;
}
- (NSString*)timeZoneOffsetString {
return [self descriptionWithCalendarFormat:@"%z"
timeZone:nil
locale:nil] ;
}
- (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