-
Notifications
You must be signed in to change notification settings - Fork 25
/
humanized_time_span.js
105 lines (96 loc) · 3.76 KB
/
humanized_time_span.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
// Copyright (C) 2011 by Will Tomlins
//
// Github profile: http://github.com/layam
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
function humanized_time_span(date, ref_date, date_formats, time_units) {
//Date Formats must be be ordered smallest -> largest and must end in a format with ceiling of null
date_formats = date_formats || {
past: [
{ ceiling: 60, text: "$seconds seconds ago" },
{ ceiling: 3600, text: "$minutes minutes ago" },
{ ceiling: 86400, text: "$hours hours ago" },
{ ceiling: 2629744, text: "$days days ago" },
{ ceiling: 31556926, text: "$months months ago" },
{ ceiling: null, text: "$years years ago" }
],
future: [
{ ceiling: 60, text: "in $seconds seconds" },
{ ceiling: 3600, text: "in $minutes minutes" },
{ ceiling: 86400, text: "in $hours hours" },
{ ceiling: 2629744, text: "in $days days" },
{ ceiling: 31556926, text: "in $months months" },
{ ceiling: null, text: "in $years years" }
]
};
//Time units must be be ordered largest -> smallest
time_units = time_units || [
[31556926, 'years'],
[2629744, 'months'],
[86400, 'days'],
[3600, 'hours'],
[60, 'minutes'],
[1, 'seconds']
];
date = new Date(date);
ref_date = ref_date ? new Date(ref_date) : new Date();
var seconds_difference = (ref_date - date) / 1000;
var tense = 'past';
if (seconds_difference < 0) {
tense = 'future';
seconds_difference = 0-seconds_difference;
}
function get_format() {
for (var i=0; i<date_formats[tense].length; i++) {
if (date_formats[tense][i].ceiling == null || seconds_difference <= date_formats[tense][i].ceiling) {
return date_formats[tense][i];
}
}
return null;
}
function get_time_breakdown() {
var seconds = seconds_difference;
var breakdown = {};
for(var i=0; i<time_units.length; i++) {
var occurences_of_unit = Math.floor(seconds / time_units[i][0]);
seconds = seconds - (time_units[i][0] * occurences_of_unit);
breakdown[time_units[i][1]] = occurences_of_unit;
}
return breakdown;
}
function render_date(date_format) {
var breakdown = get_time_breakdown();
var time_ago_text = date_format.text.replace(/\$(\w+)/g, function() {
return breakdown[arguments[1]];
});
return depluralize_time_ago_text(time_ago_text, breakdown);
}
function depluralize_time_ago_text(time_ago_text, breakdown) {
for(var i in breakdown) {
if (breakdown[i] == 1) {
var regexp = new RegExp("\\b"+i+"\\b");
time_ago_text = time_ago_text.replace(regexp, function() {
return arguments[0].replace(/s\b/g, '');
});
}
}
return time_ago_text;
}
return render_date(get_format());
}