-
Notifications
You must be signed in to change notification settings - Fork 1
/
NSFormatter+Convenience.m
220 lines (166 loc) · 9 KB
/
NSFormatter+Convenience.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
213
214
215
216
217
218
219
220
//
// NSFormatter+Convenience.m
// Sleep Diary
//
// Created by Alexander Ivanov on 16.10.16.
// Copyright © 2016 Alexander Ivanov. All rights reserved.
//
#import "NSFormatter+Convenience.h"
@implementation NSDateFormatter (Convenience)
__static(NSDateFormatter *, defaultFormatter, ({
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateStyle = NSDateFormatterShortStyle;
formatter.timeStyle = NSDateFormatterMediumStyle;
formatter;
}))
__static(NSDateFormatter *, RFC3339Formatter, ({
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[formatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
formatter;
}))
__static(NSDateFormatter *, sqlFormatter, ({
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[formatter setDateFormat:@"yyyy'-'MM'-'dd' 'HH':'mm':'ss"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
formatter;
}))
__static(NSDateFormatter *, GMTDateFormatter, ({
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateStyle = NSDateFormatterShortStyle;
formatter.timeStyle = NSDateFormatterMediumStyle;
formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
formatter;
}))
__static(NSDateFormatter *, GMTTimeFormatter, ({
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateStyle = NSDateFormatterNoStyle;
formatter.timeStyle = NSDateFormatterMediumStyle;
formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
formatter;
}))
- (NSDate *)dateFromValue:(NSString *)string {
return string ? [self dateFromString:string] : Nil;
}
- (NSString *)stringFromValue:(NSDate *)date {
return date ? [self stringFromDate:date] : Nil;
}
- (NSString *)weekdaySymbol:(NSUInteger)weekday {
return idx(self.standaloneWeekdaySymbols, weekday - 1);
}
- (NSString *)monthSymbol:(NSUInteger)month {
return idx(self.standaloneMonthSymbols, month - 1);
}
- (NSString *)weekdaySymbolForDate:(NSDate *)date {
return date ? [self weekdaySymbol:[[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate:date]] : Nil;
}
- (NSString *)monthSymbolForDate:(NSDate *)date {
return date ? [self monthSymbol:[[NSCalendar currentCalendar] component:NSCalendarUnitMonth fromDate:date]] : Nil;
}
@end
@implementation NSDateComponentsFormatter (Convenience)
+ (instancetype)dateComponentsFormatterWithAllowedUnits:(NSCalendarUnit)allowedUnits unitsStyle:(NSDateComponentsFormatterUnitsStyle)unitsStyle {
NSDateComponentsFormatter *instance = [NSDateComponentsFormatter new];
instance.allowedUnits = allowedUnits;
instance.unitsStyle = unitsStyle;
instance.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorPad;
return instance;
}
+ (instancetype)dateComponentsFormatterWithAllowedUnits:(NSCalendarUnit)allowedUnits {
return [self dateComponentsFormatterWithAllowedUnits:allowedUnits unitsStyle:NSDateComponentsFormatterUnitsStylePositional];
}
__static(NSDateComponentsFormatter *, hhmmFormatter, [self dateComponentsFormatterWithAllowedUnits:NSCalendarUnitHour | NSCalendarUnitMinute])
__static(NSDateComponentsFormatter *, hhmmssFormatter, [self dateComponentsFormatterWithAllowedUnits:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond])
__static(NSDateComponentsFormatter *, mmssFormatter, [self dateComponentsFormatterWithAllowedUnits:NSCalendarUnitMinute | NSCalendarUnitSecond])
__static(NSDateComponentsFormatter *, hhmmFullFormatter, [NSDateComponentsFormatter dateComponentsFormatterWithAllowedUnits:NSCalendarUnitHour | NSCalendarUnitMinute unitsStyle:NSDateComponentsFormatterUnitsStyleFull])
__static(NSDateComponentsFormatter *, mmssAbbreviatedFormatter, [NSDateComponentsFormatter dateComponentsFormatterWithAllowedUnits:NSCalendarUnitMinute | NSCalendarUnitSecond unitsStyle:NSDateComponentsFormatterUnitsStyleAbbreviated])
__static(NSDateComponentsFormatter *, mmShortFormatter, [NSDateComponentsFormatter dateComponentsFormatterWithAllowedUnits:NSCalendarUnitMinute unitsStyle:NSDateComponentsFormatterUnitsStyleShort])
__static(NSDateComponentsFormatter *, ssShortFormatter, [NSDateComponentsFormatter dateComponentsFormatterWithAllowedUnits:NSCalendarUnitSecond unitsStyle:NSDateComponentsFormatterUnitsStyleShort])
__static(NSDateComponentsFormatter *, ssFullFormatter, [NSDateComponentsFormatter dateComponentsFormatterWithAllowedUnits:NSCalendarUnitSecond unitsStyle:NSDateComponentsFormatterUnitsStyleFull])
- (NSString *)stringFromValue:(NSDate *)startDate toValue:(NSDate *)endDate {
return startDate && endDate ? [self stringFromDate:startDate toDate:endDate] : Nil;
}
@end
@implementation NSDateIntervalFormatter (Convinience)
- (instancetype)initWithDateStyle:(NSDateIntervalFormatterStyle)dateStyle timeStyle:(NSDateIntervalFormatterStyle)timeStyle {
self = [self init];
if (self) {
self.dateStyle = dateStyle;
self.timeStyle = timeStyle;
}
return self;
}
- (NSString *)stringFromTimeInterval:(NSTimeInterval)fromTimeInterval toTimeInterval:(NSTimeInterval)toTimeInterval {
NSDate *fromDate = [NSDate dateWithTimeIntervalSinceReferenceDate:fromTimeInterval];
NSDate *toDate = [NSDate dateWithTimeIntervalSinceReferenceDate:toTimeInterval];
return [self stringFromDate:fromDate toDate:toDate];
}
@end
@implementation NSLengthFormatter (Convenience)
__static(NSLengthFormatter *, lengthFormatter, [[NSLengthFormatter alloc] init])
@end
@implementation NSDate (NSFormatter)
- (NSString *)descriptionForDate:(NSDateFormatterStyle)dateStyle andTime:(NSDateFormatterStyle)timeStyle {
return [NSDateFormatter localizedStringFromDate:self dateStyle:dateStyle timeStyle:timeStyle];
}
- (NSString *)descriptionForDateAndTime:(NSDateFormatterStyle)dateAndTimeStyle {
return [NSDateFormatter localizedStringFromDate:self dateStyle:dateAndTimeStyle timeStyle:dateAndTimeStyle];
}
- (NSString *)descriptionForDate:(NSDateFormatterStyle)dateStyle {
return [NSDateFormatter localizedStringFromDate:self dateStyle:dateStyle timeStyle:NSDateFormatterNoStyle];
}
- (NSString *)descriptionForTime:(NSDateFormatterStyle)timeStyle {
return [NSDateFormatter localizedStringFromDate:self dateStyle:NSDateFormatterNoStyle timeStyle:timeStyle];
}
- (NSString *)descriptionWithFormat:(NSString *)format calendar:(NSCalendar *)calendar {
NSDateFormatter *formatter = [NSDateFormatter new];
if (format)
formatter.dateFormat = format;
if (calendar)
formatter.calendar = calendar;
return [formatter stringFromDate:self];
}
- (NSString *)weekdayDescription {
return [self descriptionWithFormat:@"EEEE" calendar:Nil];
}
- (NSString *)timestampDescription {
return [self descriptionWithFormat:@"yyyy-MM-dd-HH-mm-ss" calendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]];
}
- (NSString *)descriptionFromNow {
NSCalendarUnit units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
NSDateComponents *components = [[NSCalendar currentCalendar] components:units fromDate:self toDate:[NSDate date] options:0];
if (components.year > 0)
return [NSString stringWithFormat:components.year == 1 ? @"A year ago" : @"%ld years ago", (long)components.year];
else if (components.year < 0)
return [NSString stringWithFormat:components.year == -1 ? @"In a year" : @"In %ld years", (long)components.year];
else if (components.month > 0)
return [NSString stringWithFormat:components.month == 1 ? @"A month ago" : @"%ld months ago", (long)components.month];
else if (components.month < 0)
return [NSString stringWithFormat:components.month == -1 ? @"In a month" : @"In %ld months", (long)components.month];
else if (components.day > 0)
return [NSString stringWithFormat:components.day == 1 ? @"A day ago" : @"%ld days ago", (long)components.day];
else if (components.day < 0)
return [NSString stringWithFormat:components.day == -1 ? @"In a day" : @"In %ld days", (long)components.day];
else if (components.hour > 0)
return [NSString stringWithFormat:components.hour == 1 ? @"An hour ago" : @"%ld hours ago", (long)components.hour];
else if (components.hour < 0)
return [NSString stringWithFormat:components.hour == -1 ? @"In an hour" : @"In %ld hours", (long)components.hour];
else if (components.minute > 0)
return [NSString stringWithFormat:components.minute == 1 ? @"A minute ago" : @"%ld minutes ago", (long)components.minute];
else if (components.minute < 0)
return [NSString stringWithFormat:components.minute == -1 ? @"In a minute" : @"In %ld minutes", (long)components.minute];
return @"Now";
}
@end
@implementation NSDateComponents (NSFormatter)
- (NSString *)description:(NSDateComponentsFormatterUnitsStyle)unitStyle {
return [NSDateComponentsFormatter localizedStringFromDateComponents:self unitsStyle:unitStyle];
}
@end
@implementation NSNumber (NSNumberFormatter)
- (NSString *)localizedStringWithNumberStyle:(NSNumberFormatterStyle)style {
return [NSNumberFormatter localizedStringFromNumber:self numberStyle:style];
}
@end