forked from jerrykrinock/CategoriesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSDate+Components.m
153 lines (115 loc) · 3.54 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
#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:@"%04d", year] ;
[dateString replaceCharactersInRange:NSMakeRange(0,4)
withString:substitution] ;
}
if (month != NSNotFound) {
substitution = [NSString stringWithFormat:@"%02d", month] ;
[dateString replaceCharactersInRange:NSMakeRange(5,2)
withString:substitution] ;
}
if (day != NSNotFound) {
substitution = [NSString stringWithFormat:@"%02d", day] ;
[dateString replaceCharactersInRange:NSMakeRange(8,2)
withString:substitution] ;
}
if (hour != NSNotFound) {
substitution = [NSString stringWithFormat:@"%02d", hour] ;
[dateString replaceCharactersInRange:NSMakeRange(11,2)
withString:substitution] ;
}
if (minute != NSNotFound) {
substitution = [NSString stringWithFormat:@"%02d", minute] ;
[dateString replaceCharactersInRange:NSMakeRange(14,2)
withString:substitution] ;
}
if (second != NSNotFound) {
substitution = [NSString stringWithFormat:@"%02d", 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%02d%02d",
timeZoneSign,
timeZoneHours,
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 {
return [self descriptionWithCalendarFormat:@"%Y"
timeZone:nil
locale:nil] ;
}
- (NSString*)monthString {
return [self descriptionWithCalendarFormat:@"%m"
timeZone:nil
locale:nil] ;
}
- (NSString*)dayString {
return [self descriptionWithCalendarFormat:@"%d"
timeZone:nil
locale:nil] ;
}
- (NSString*)hourString {
return [self descriptionWithCalendarFormat:@"%H"
timeZone:nil
locale:nil] ;
}
- (NSString*)minuteString {
return [self descriptionWithCalendarFormat:@"%M"
timeZone:nil
locale:nil] ;
}
- (NSString*)secondString {
return [self descriptionWithCalendarFormat:@"%S"
timeZone:nil
locale:nil] ;
}
- (NSString*)timeZoneOffsetString {
return [self descriptionWithCalendarFormat:@"%z"
timeZone:nil
locale:nil] ;
}
- (NSInteger)year {
return [[self yearString] intValue] ;
}
- (NSInteger)month {
return [[self monthString] intValue] ;
}
- (NSInteger)day {
return [[self dayString] intValue] ;
}
- (NSInteger)hour {
return [[self hourString] intValue] ;
}
- (NSInteger)minute {
return [[self minuteString] intValue] ;
}
- (NSInteger)second {
return [[self secondString] intValue] ;
}
@end