-
Notifications
You must be signed in to change notification settings - Fork 401
/
SCTimeIntervalFormatter.m
97 lines (80 loc) · 3.54 KB
/
SCTimeIntervalFormatter.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
//
// SCTimeIntervalFormatter.m
// SelfControl
//
// Created by Sam Stigler on 10/14/14.
//
//
#import <FormatterKit/TTTTimeIntervalFormatter.h>
#import "SCTimeIntervalFormatter.h"
@implementation SCTimeIntervalFormatter
- (NSString *)stringForObjectValue:(id)obj {
NSString* string = @"";
if ([obj isKindOfClass:[NSNumber class]]) {
string = [self formatSeconds:[obj doubleValue]];
}
return string;
}
- (NSString *)formatSeconds:(NSTimeInterval)seconds {
NSString* formatted;
BOOL useModernBehavior = (NSAppKitVersionNumber >= NSAppKitVersionNumber10_8);
if (useModernBehavior) {
formatted = [self formatSecondsUsingModernBehavior:seconds];
}
else {
formatted = [self formatSecondsUsingLegacyBehavior:seconds];
}
return formatted;
}
- (NSString *)formatSecondsUsingModernBehavior:(NSTimeInterval)seconds
{
static TTTTimeIntervalFormatter* timeIntervalFormatter = nil;
if (timeIntervalFormatter == nil) {
timeIntervalFormatter = [[TTTTimeIntervalFormatter alloc] init];
timeIntervalFormatter.pastDeicticExpression = @"";
timeIntervalFormatter.presentDeicticExpression = @"";
timeIntervalFormatter.futureDeicticExpression = @"";
timeIntervalFormatter.significantUnits = (NSCalendarUnitYear |
NSCalendarUnitMonth |
NSCalendarUnitDay |
NSCalendarUnitHour |
NSCalendarUnitMinute);
timeIntervalFormatter.numberOfSignificantUnits = 0;
timeIntervalFormatter.leastSignificantUnit = NSCalendarUnitMinute;
}
NSString* formatted = [timeIntervalFormatter stringForTimeInterval:seconds];
if ([formatted length] == 0) {
formatted = [self stringIndicatingZeroMinutes];
}
return formatted;
}
- (NSString *)formatSecondsUsingLegacyBehavior:(NSTimeInterval)seconds {
int formatDays, formatHours, formatMinutes;
int numMinutes = seconds / 60;
formatDays = numMinutes / 1440;
formatHours = (numMinutes % 1440) / 60;
formatMinutes = (numMinutes % 60);
NSString* timeString = @"";
if(numMinutes > 0) {
if(formatDays > 0) {
timeString = [NSString stringWithFormat:@"%d %@", formatDays, (formatDays == 1 ? NSLocalizedString(@"day", @"Single day time string") : NSLocalizedString(@"days", @"Plural days time string"))];
}
if(formatHours > 0) {
timeString = [NSString stringWithFormat: @"%@%@%d %@", timeString, (formatDays > 0 ? @", " : @""), formatHours, (formatHours == 1 ? NSLocalizedString(@"hour", @"Single hour time string") : NSLocalizedString(@"hours", @"Plural hours time string"))];
}
if(formatMinutes > 0) {
timeString = [NSString stringWithFormat:@"%@%@%d %@", timeString, (formatHours > 0 || formatDays > 0 ? @", " : @""), formatMinutes, (formatMinutes == 1 ? NSLocalizedString(@"minute", @"Single minute time string") : NSLocalizedString(@"minutes", @"Plural minutes time string"))];
}
}
else {
timeString = [self stringIndicatingZeroMinutes];
}
return timeString;
}
- (NSString *)stringIndicatingZeroMinutes {
NSString* disabledString = [NSString stringWithFormat: @"0 %@ (%@)",
NSLocalizedString(@"minutes", @"Plural minutes time string"),
NSLocalizedString(@"disabled", "Shows that SelfControl is disabled")];
return disabledString;
}
@end