-
Notifications
You must be signed in to change notification settings - Fork 3
/
TimeLeft.m
executable file
·121 lines (99 loc) · 2.92 KB
/
TimeLeft.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
//
// My eBay App - TimeLeft.m
// Copyright 2010 eBay Inc. All rights reserved.
//
// Created by: Jon Reid
//
#import "TimeLeft.h"
@implementation TimeLeft
@synthesize days;
@synthesize hours;
@synthesize minutes;
@synthesize seconds;
- (id)initWithISODuration:(NSString *)duration;
{
self = [super init];
if (self != nil)
{
// Split duration into days/time components
NSRange marker = [duration rangeOfString:@"T"];
NSRange daysRange = NSMakeRange(1, marker.location - 1); // Skip past leading "P"
NSRange timeRange = NSMakeRange(marker.location + 1, [duration length] - (marker.location + 1));
NSString *daysComponents = [duration substringWithRange:daysRange];
NSString *timeComponents = [duration substringWithRange:timeRange];
// Parse days components
marker = [daysComponents rangeOfString:@"D"];
if (marker.length > 0)
{
daysRange = NSMakeRange(0, marker.location);
days = [[daysComponents substringWithRange:daysRange] intValue];
}
// Parse time components
NSUInteger scanFrom = 0;
marker = [timeComponents rangeOfString:@"H"];
if (marker.length > 0)
{
timeRange = NSMakeRange(scanFrom, marker.location - scanFrom);
hours = [[timeComponents substringWithRange:timeRange] intValue];
scanFrom = marker.location + 1;
}
marker = [timeComponents rangeOfString:@"M"];
if (marker.length > 0)
{
timeRange = NSMakeRange(scanFrom, marker.location - scanFrom);
minutes = [[timeComponents substringWithRange:timeRange] intValue];
scanFrom = marker.location + 1;
}
marker = [timeComponents rangeOfString:@"S"];
if (marker.length > 0)
{
timeRange = NSMakeRange(scanFrom, marker.location - scanFrom);
seconds = [[timeComponents substringWithRange:timeRange] intValue];
}
}
return self;
}
- (NSString *)shortString
{
if (days != 0)
{
return [NSString stringWithFormat:@"%dd %dh", days, hours];
}
else if (hours != 0)
{
return [NSString stringWithFormat:@"%dh %dm", hours, minutes];
}
else if (minutes != 0)
{
return [NSString stringWithFormat:@"%dm %ds", minutes, seconds];
}
else if (seconds != 0)
{
return [NSString stringWithFormat:@"%ds", seconds];
}
else
{
return @"Ended";
}
}
- (BOOL)hasEnded
{
return days == 0 && hours == 0 && minutes == 0 && seconds == 0;
}
- (BOOL)isEndingSoon
{
return days == 0 && hours == 0 &&
((minutes == 1 && seconds == 0) || (minutes == 0 && seconds > 0));
}
- (UIColor *)textColor
{
if ([self isEndingSoon])
{
return [UIColor redColor];
}
else
{
return [UIColor blackColor];
}
}
@end