This repository has been archived by the owner on Jun 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDWTimeInterval.m
54 lines (43 loc) · 1.53 KB
/
DWTimeInterval.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
//
// DWTimeInterval.m
// Tweeterena
//
// Created by Dominic Wroblewski on 12/02/2011.
// Copyright 2011 TerraCoding. All rights reserved.
//
#import "DWTimeInterval.h"
@implementation DWTimeInterval
-(DWTimeInterval *)initWithSinceDate:(NSDate*)date
{
NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:date];
int distance = interval;
timeAgo = @"";
if (distance < 0) distance = 0;
if (distance < 60) {
timeAgo = [NSString stringWithFormat:@"%d%s", distance, (distance == 1) ? " second ago" : " seconds ago"];
} else if (distance < 60 * 60) {
distance = distance / 60;
timeAgo = [NSString stringWithFormat:@"%d%s", distance, (distance == 1) ? " minute ago" : " minutes ago"];
} else if (distance < 60 * 60 * 24) {
distance = distance / 60 / 60;
timeAgo = [NSString stringWithFormat:@"%d%s", distance, (distance == 1) ? " hour ago" : " hours ago"];
} else if (distance < 60 * 60 * 24 * 7) {
distance = distance / 60 / 60 / 24;
timeAgo = [NSString stringWithFormat:@"%d%s", distance, (distance == 1) ? " day ago" : " days ago"];
} else if (distance < 60 * 60 * 24 * 7 * 4) {
distance = distance / 60 / 60 / 24 / 7;
timeAgo = [NSString stringWithFormat:@"%d%s", distance, (distance == 1) ? " week ago" : " weeks ago"];
} else {
NSDate *d = date;
NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:@"dd MMM yyyy"];
timeAgo = [myFormatter stringFromDate:d];
[myFormatter release];
}
return self;
}
-(NSString *)timeAgo
{
return timeAgo;
}
@end