This repository has been archived by the owner on Aug 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathORSDateDifferenceFormatter.m
126 lines (112 loc) · 2.88 KB
/
ORSDateDifferenceFormatter.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
//
// ORSDateDifferenceFormatter.m
// Canary Controller
//
// Created by Nicholas Toumpelis on 12/04/2009.
// Copyright 2009 Ocean Road Software. All rights reserved.
//
// Version 0.7
#import "ORSDateDifferenceFormatter.h"
@implementation ORSDateDifferenceFormatter
- (NSString *) stringForObjectValue:(id)anObject {
// Get time difference
int currentDateAsSeconds = (int) [NSDate timeIntervalSinceReferenceDate];
int statusDateAsSeconds = (int)[(NSString *)anObject doubleValue];
int seconds = currentDateAsSeconds - statusDateAsSeconds;
// Check whether it's years away
int yearInSecs = 12 * 4 * 7 * 24 * 60 * 60;
int years = seconds / yearInSecs;
if (years > 0) {
if (years > 1) {
return @"> a year";
} else {
return @"a year";
}
}
// check whether it's months away
int monthInSecs = 4 * 7 * 24 * 60 * 60;
int months = seconds / monthInSecs;
int secsAfterMonths = seconds % monthInSecs;
if (secsAfterMonths > 2 * 7 * 24 * 60 * 60) {
months++;
}
if (months > 0) {
if (months > 1) {
return [NSString stringWithFormat:@"%i months", months];
} else {
return @"a month";
}
}
// check whether it's weeks away
int weekInSecs = 7 * 24 * 60 * 60;
int weeks = seconds / weekInSecs;
int secsAfterWeeks = seconds % weekInSecs;
if (secsAfterWeeks > 4 * 24 * 60 * 60) {
weeks++;
}
if (weeks > 0) {
if (weeks > 1) {
return [NSString stringWithFormat:@"%i weeks", weeks];
} else {
return @"a week";
}
}
// check whether it's days away
int dayInSecs = 24 * 60 * 60;
int days = seconds / dayInSecs;
int secsAfterDays = seconds % dayInSecs;
if (secsAfterDays > 12 * 60 * 60) {
days++;
}
if (days > 0) {
if (days > 1) {
return [NSString stringWithFormat:@"%i days", days];
} else {
return @"a day";
}
}
// check whether it's hours away
int hourInSecs = 60 * 60;
int hours = seconds / hourInSecs;
int secsAfterHours = seconds % hourInSecs;
if (secsAfterHours > 30 * 60) {
hours++;
}
if (hours > 0) {
if (hours > 1) {
return [NSString stringWithFormat:@"%i hrs", hours];
} else {
return @"an hr";
}
}
// check whether it's minutes away
int minuteInSecs = 60;
int minutes = seconds / minuteInSecs;
if (minutes > 0) {
if (minutes > 1) {
return [NSString stringWithFormat:@"%i mins", minutes];
} else {
return @"a min";
}
}
// if not any of the above, it's seconds away
if (seconds > 1) {
return [NSString stringWithFormat:@"%i secs", seconds];
} else if (seconds == 1) {
return @"a sec";
} else {
return @"< a sec";
}
}
- (BOOL) getObjectValue:(id *)anObject
forString:(NSString *)string
errorDescription:(NSString **)error {
return YES;
}
- (NSAttributedString *) attributedStringForObjectValue:(id)anObject
withDefaultAttributes:(NSDictionary *)attributes {
return [[NSAttributedString alloc]
initWithString:[self stringForObjectValue:anObject]
attributes:attributes];
}
@end