-
Notifications
You must be signed in to change notification settings - Fork 2
/
fuzzytime.js
153 lines (149 loc) · 5.5 KB
/
fuzzytime.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Fuzzytime jquery time prettifying plugin
*
* What it does
* There are two kinds of times that you can pull out of fuzzytime
*
* - a fuzzy time:
* - less than an hour ago
* - a couple of days ago
* - an implicit time
* - 5 minutes ago
* - 3 weeks ago
* Use:
*
* send your Twitter, Facebook, Unix or Javascript formatted timestamp to the $.fuzzytime() function
* ex. var myFaceBookDate = '2010-10-19T02:57:48+0000';
* var myPrettyTime = $.fuzzytime(myFaceBookDate) // returns something like 'a couple weeks ago'
*
* to make the date explicit ex. '35 minutes ago', send a value of true as the second argument to $.fuzzytime()
* ex. var myTwitterDate = 'Tue Oct 19 14:01:57 +0000 2010';
* var myPrettyImplicitTime = $.fuzzytime(myTwitterDate,true); // returns something like '3 days ago'
*
* Thanks to:
* the Kohana project's date helper (http://kohanaframework.com)
* rmm5t for the iso8601 method to make facebook possible (http://github.com/rmm5t/jquery-timeago)
*/
(function ($) {
var Fuzzy = function (implicit,now) {
var implicit = implicit || false,
now = now || new Date().valueOf(),
year = 31556926,
month = 2629744,
week = 604800,
day = 86400,
hour = 3600,
minute = 60,
span = 'a long time',
times = [
minute,
minute * 20,
hour,
hour * 4,
day,
day * 2,
day * 4,
week,
week * 2,
month,
month * 2,
month * 4,
year,
year * 2,
year * 4,
year * 8,
year * 12,
year * 24,
year * 64
],
fuzzies = [
'moments',
'a few minutes',
'less than an hour',
'a couple of hours',
'less than a day',
'about a day',
'a couple of days',
'less than a week',
'about a week',
'less than a month',
'about a month',
'a couple of months',
'less than a year',
'about a year',
'a couple of years',
'a few years',
'about a decade',
'a couple of decades',
'several decades',
],
implicits = [
{val : minute, string : 'minute'},
{val : hour, string : 'hour'},
{val : day, string : 'day'},
{val : week, string : 'week'},
{val : month, string : 'month'},
{val : year, string : 'year'}
];
this.parse = function (timestamp) {
var timestamp = timestamp || new Date(),
future = false,
offset,
stamp;
// check for iso8601
if(/\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\+\d{4}/.test(timestamp)){
stamp = this.iso8601(timestamp);
console.log("is iso8601");
}else{
if (navigator && (/msie/i).test(navigator.userAgent))
stamp = new Date(timestamp.replace(/(\d\d:\d\d:\d\d\s)\+/,'\1 GMT+')).valueOf();
else
stamp = new Date(timestamp).valueOf();
}
offset = (now - stamp)/1000;
if (offset < 0) future = true;
offset = Math.abs(offset);
if(!span){
return !future ? 'some time ago' : 'in some time';
}else if(!implicit){
for (var i=0; i < times.length; i++) {
if(offset < times[i]){
span = fuzzies[i];
break;
}
};
}else{
for (var i=0; i < implicits.length; i++) {
if(offset < implicits[i]['val']){
if(i == 0){
span = 'less than a minute';
break;
}else{
var howMany = Math.floor(offset / implicits[i - 1]['val']),
newString = howMany + " " + implicits[i - 1]['string'];
span = howMany > 1 ? newString + 's' : newString;
break;
}
}
};
}
return !future ? span + ' ago' : 'in ' + span;
};
// facebook, for example, uses iso8601 for date encoding
this.iso8601 = function (timestamp) {
var s = timestamp.replace(/^\s*(.*)\s*$/,'');
s = s.replace(/\.\d\d\d+/, ""); // remove milliseconds
s = s.replace(/-/, "/").replace(/-/, "/");
s = s.replace(/T/, " ").replace(/Z/, " UTC");
s = s.replace(/([\+-]\d\d)\:?(\d\d)/, " $1$2"); // -04:00 -> -0400
return new Date(s).valueOf();
}
}
// monkeypatch number and string classes
for (var i = 0, klasses = [Number, String]; i < klasses.length; i++) {
klasses[i].prototype.fuzzytime = function (implicit,now) {
var fuzzy = new Fuzzy(implicit,now);
return fuzzy.parse(this);
};
}
}());