forked from jerrykrinock/CategoriesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSDate+NiceFormats.m
77 lines (64 loc) · 2.72 KB
/
NSDate+NiceFormats.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
#import "NSDate+NiceFormats.h"
#import "NSString+SSYExtraUtils.h"
#if (MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5) \
|| (MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4)
/*!
@brief Declares stuff defined in the 10.6 SDK,
to eliminate compiler warnings.
@details Be careful to only invoke super on these methods after
you've checked that you are running under Mac OS X 10.6.
*/
@interface NSDate (DefinedInMac_OS_X_10_6)
- (id)dateByAddingTimeInterval:(NSTimeInterval)seconds ;
@end
#endif
@implementation NSDate (NiceFormats)
- (NSString*)medDateShortTimeString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateStyle:NSDateFormatterMediumStyle] ;
[dateFormatter setTimeStyle:NSDateFormatterShortStyle] ;
NSString* string = [dateFormatter stringFromDate:self];
[dateFormatter release] ;
return string ;
}
- (NSString*)geekDateTimeString {
// After spending 45 minutes reading NSDateFormatter trying to
// figure out a non-depracated and supported way to get a date
// in geek format, I noticed that -[NSDate description] is
// *documented* to return a string in this format:
// YYYY-MM-DD HH:MM:SS ±HHMM
// Prior to Mac OS 10.7, ±HHMM = the local time zone offset
// Starting in Mac OS 10.7, ±HHMM = +0000.
// We want the time in the local time zone, so we need to read and adjust if necessary
NSString* s = [self description] ;
NSInteger tzSign = [[s substringWithRange:NSMakeRange(20,1)] isEqualToString:@"+"] ? +1 : -1 ;
NSInteger tzHours = [[s substringWithRange:NSMakeRange(21,2)] integerValue] ;
NSInteger tzMinutes = [[s substringWithRange:NSMakeRange(23,2)] integerValue] ;
NSInteger tzSeconds = tzSign * (3600*tzHours + 60*tzMinutes) ;
NSInteger localTzSeconds = [[NSTimeZone localTimeZone] secondsFromGMT] ;
NSString* localTimeString ;
if (tzSeconds == localTzSeconds) {
// This must be Mac OS 10.5 or 10.6.
localTimeString = s ;
}
else {
// This must be Mac OS 10.7.
// Fortunately, starting in Mac OS 10.6 we have -[NSDate dateByAddingTimeInterval]
NSDate* localDate = [self dateByAddingTimeInterval:(localTzSeconds - tzSeconds)] ;
localTimeString = [localDate description] ;
}
NSString* answer = [localTimeString substringToIndex:19] ;
return answer ;
}
- (NSString*)compactDateTimeString {
// Remove spaces, dash and colon from YYYY-MM-DD HH:MM:SS
NSString* s1 = [[self geekDateTimeString] stringByReplacingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" :-"]
withString:@""] ;
// Time zone
NSString* tz = [[self description] substringFromIndex:20] ;
return [s1 stringByAppendingString:tz] ;
}
+ (NSString*)currentDateFormattedConcisely {
return [[NSDate date] medDateShortTimeString] ;
}
@end