gabriel / gh-kit

Utilities and categories for Objective-C

This URL has Read+Write access

gh-kit / Classes / GHNSDate+Utils.m
100644 128 lines (105 sloc) 4.372 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
//
// GHNSDate+Utils.m
// GHKit
//
// Created by Gabriel Handford on 2/18/09.
//
// 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+Utils.h"
#import "GHNSString+TimeInterval.h"
 
// Common date formats
NSString *const kDateFormatShortMonthFullYearTime = @"LLL d, yyyy hh:mm a";
 
@implementation NSDate (GHUtils)
 
NSUInteger const kUnitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit;
 
+ (NSDate *)_gh_normalizedDate:(NSDate *)date adjustDay:(NSInteger)adjustDay {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:kUnitFlags fromDate:date];
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];
if (adjustDay != 0) [comps setDay:[comps day] + adjustDay];
return [calendar dateFromComponents:comps];
}
 
- (NSDate *)gh_addDays:(NSInteger)days {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:kUnitFlags fromDate:self];
if (days != 0) [comps setDay:[comps day] + days];
return [calendar dateFromComponents:comps];
}
 
- (NSDate *)gh_beginningOfDay {
return [NSDate _gh_normalizedDate:self adjustDay:0];
}
 
+ (NSDate *)gh_yesterday {
return [NSDate _gh_normalizedDate:[NSDate date] adjustDay:-1];
}
 
+ (NSDate *)gh_tomorrow {
return [NSDate _gh_normalizedDate:[NSDate date] adjustDay:1];
}
 
- (BOOL)gh_isTomorrow {
return ([[self gh_beginningOfDay] compare:[NSDate gh_tomorrow]] == NSOrderedSame);
}
 
- (BOOL)gh_isToday {
return ([[self gh_beginningOfDay] compare:[[NSDate date] gh_beginningOfDay]] == NSOrderedSame);
}
 
- (BOOL)gh_wasYesterday {
return ([[self gh_beginningOfDay] compare:[NSDate gh_yesterday]] == NSOrderedSame);
}
 
- (NSString *)gh_weekday:(NSDateFormatter *)formatter {
if ([self gh_isTomorrow]) return @"Tomorrow";
else if ([self gh_isToday]) return @"Today";
else if ([self gh_wasYesterday]) return @"Yesterday";
 
if (!formatter) return nil;
 
NSInteger weekday = [[[NSCalendar currentCalendar] components:kUnitFlags fromDate:self] weekday] - 1;
return [[formatter weekdaySymbols] objectAtIndex:weekday];
}
 
- (NSString *)gh_format:(NSString *)format useWeekday:(BOOL)useWeekday {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
NSString *formatted = [dateFormatter stringFromDate:self];
if (useWeekday) {
NSString *specialWeekday = [self gh_weekday:dateFormatter];
formatted = [NSString stringWithFormat:@"%@, %@", specialWeekday, formatted];
}
[dateFormatter release];
return formatted;
}
 
- (NSString *)gh_timeAgo:(BOOL)includeSeconds {
return [NSString gh_stringForTimeInterval:[self timeIntervalSinceNow] includeSeconds:includeSeconds];
}
 
- (long long)gh_millisSince1970 {
NSTimeInterval secondsSince1970 = [self timeIntervalSince1970];
return (long long)round(secondsSince1970 * 1000);
}
 
- (NSNumber *)gh_millisNumberSince1970 {
long long millis = [self gh_millisSince1970];
return [NSNumber numberWithLongLong:millis];
}
 
- (long long)gh_secondsSince1970 {
NSTimeInterval secondsSince1970 = [self timeIntervalSince1970];
return (long long)round(secondsSince1970);
}
 
- (NSNumber *)gh_secondsNumberSince1970 {
long long seconds = [self gh_secondsSince1970];
return [NSNumber numberWithLongLong:seconds];
}
 
@end