/
NSDate-Relative.m
91 lines (79 loc) · 1.68 KB
/
NSDate-Relative.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
//
// NSDate-Relative.m
// MobileTwitterrific
//
// Created by Craig Hockenberry on 9/6/07.
// Copyright 2007 __MyCompanyName__. All rights reserved.
//
#import "NSDate-Relative.h"
@implementation NSDate(Relative)
- (NSString *)relativeDate
{
#if 0
return @"N/A";
#else
NSTimeInterval timeSinceNow = fabsf([self timeIntervalSinceNow]);
NSTimeInterval delta = timeSinceNow;
if (delta < 60.0)
{
if (roundf(delta) == 0.0)
{
return NSLocalizedString(@"JustNow", nil);
}
else if (roundf(delta) == 1.0)
{
return [NSString stringWithFormat:NSLocalizedString(@"Second", nil), delta];
}
else
{
return [NSString stringWithFormat:NSLocalizedString(@"Seconds", nil), delta];
}
}
delta = delta / 60.0;
if (delta < 60.0)
{
if (roundf(delta) == 1.0)
{
return [NSString stringWithFormat:NSLocalizedString(@"Minute", nil), delta];
}
else
{
return [NSString stringWithFormat:NSLocalizedString(@"Minutes", nil), delta];
}
}
delta = delta / 60.0;
if (delta < 24.0)
{
if (roundf(delta) == 1.0)
{
return [NSString stringWithFormat:NSLocalizedString(@"Hour", nil), delta];
}
else
{
return [NSString stringWithFormat:NSLocalizedString(@"Hours", nil), delta];
}
}
delta = delta / 24.0;
if (delta < 7.0)
{
if (roundf(delta) == 1.0)
{
return [NSString stringWithFormat:NSLocalizedString(@"Day", nil), delta];
}
else
{
return [NSString stringWithFormat:NSLocalizedString(@"Days", nil), delta];
}
}
delta = delta / 7.0;
if (roundf(delta) == 1.0)
{
return [NSString stringWithFormat:NSLocalizedString(@"Week", nil), delta];
}
else
{
return [NSString stringWithFormat:NSLocalizedString(@"Weeks", nil), delta];
}
#endif
}
@end