forked from miketsprague/NiceDate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNiceDate.m
212 lines (176 loc) · 4.51 KB
/
NiceDate.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
//
// NiceDate.m
// NiceDate
//
// Created by Michael Sprague on 5/20/13.
//
//
#import "NiceDate.h"
@interface NiceDate()
@property (nonatomic, strong) NSDateFormatter *formatter;
@property (nonatomic, strong) NSCalendar *calendar;
@end
@implementation NiceDate
@synthesize second = _second;
@synthesize minute = _minute;
@synthesize hour = _hour;
@synthesize day = _day;
@synthesize month = _month;
@synthesize year = _year;
@synthesize timeZone = _timeZone;
-(id)init
{
if(self = [self initWithDate:[NSDate date]])
{
}
return self;
}
-(id)initWithDate:(NSDate*)date
{
if(self = [super init]){
_format = @"MM-dd-yyyy HH:mm:ss";
_timeZone = [[NSCalendar autoupdatingCurrentCalendar] timeZone];
_formatter = [NSDateFormatter new];
[_formatter setDateFormat:_format];
_calendarIdentifier = NSGregorianCalendar;
_calendar = [[NSCalendar alloc] initWithCalendarIdentifier:_calendarIdentifier];
self.date = date;
}
return self;
}
// unix timestamp
- (NSTimeInterval) unixTime
{
return [self.date timeIntervalSince1970];
}
// gets a date/time stamp formatted YYYY-mm-dd H:i:s
- (NSString *) mysqlDateTime
{
self.format = @"yyyy-MM-dd HH:mm:ss";
return [self description];
}
+(NiceDate*)niceDate
{
NiceDate *niceDate = [NiceDate new];
return niceDate;
}
+(NiceDate*)niceDateWithDate:(NSDate*)date
{
NiceDate *niceDate = [[NiceDate alloc] initWithDate:date];
return niceDate;
}
+ (NSString *) currentMysqlDateTime
{
NSString *ret = nil;
@autoreleasepool {
ret = [[NiceDate new] mysqlDateTime];
}
return ret;
}
+ (NSTimeInterval) currentUnixTime
{
NSTimeInterval ret = -1;
@autoreleasepool {
ret = [[NiceDate new] unixTime];
}
return ret;
}
-(NSUInteger)second
{
return _second;
}
-(void)setSecond:(NSUInteger)second
{
_second = second;
NSDateComponents *dateComponents = [self getDateComps];
[dateComponents setSecond:_second];
_date = [_calendar dateFromComponents:dateComponents];
}
-(NSUInteger)minute
{
return _minute;
}
-(void)setMinute:(NSUInteger)minute
{
_minute = minute;
NSDateComponents *dateComponents = [self getDateComps];
[dateComponents setMinute:_minute];
_date = [_calendar dateFromComponents:dateComponents];
}
-(NSUInteger)hour
{
return _hour;
}
-(void)setHour:(NSUInteger)hour
{
_hour = hour;
NSDateComponents *dateComponents = [self getDateComps];
[dateComponents setHour:_hour];
_date = [_calendar dateFromComponents:dateComponents];
}
-(NSUInteger)day
{
return _day;
}
-(void)setDay:(NSUInteger)day
{
_day = day;
NSDateComponents *dateComponents = [self getDateComps];
[dateComponents setDay:_day];
_date = [_calendar dateFromComponents:dateComponents];
}
-(NSUInteger)month
{
return _month;
}
-(void)setMonth:(NSUInteger)month
{
_month = month;
NSDateComponents *dateComponents = [self getDateComps];
[dateComponents setMonth:_month];
_date = [_calendar dateFromComponents:dateComponents];
}
-(NSUInteger)year
{
return _year;
}
-(void)setYear:(NSUInteger)year
{
_year = year;
NSDateComponents *dateComponents = [self getDateComps];
[dateComponents setYear:_year];
_date = [_calendar dateFromComponents:dateComponents];
}
-(void)setCalendarIdentifier:(NSString *)calendarIdentifier
{
_calendarIdentifier = calendarIdentifier;
// Update our calendar.
_calendar = [[NSCalendar alloc] initWithCalendarIdentifier:_calendarIdentifier];
}
// We're overriding our date, so we need to make sure that we update all of our fields to take the NSDate's values.
-(void)setDate:(NSDate *)date
{
_date = date;
// Update all of our fields. We need to be overridden by the new date.
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:_date];
_second = [components second];
_minute = [components minute];
_hour = [components hour];
_day = [components day];
_month = [components month];
_year = [components year];
}
-(NSString*)description
{
[_formatter setDateFormat:_format];
[_formatter setTimeZone:_timeZone];
return [_formatter stringFromDate:self.date];
}
-(NSDateComponents*)getDateComps
{
return [_calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:self.date];
}
- (BOOL)isEqualToNiceDate:(NiceDate *)otherDate {
return [self.date isEqualToDate:otherDate.date];
}
@end