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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use std::cmp::max;
use std::fmt;
use chrono::{DateTime, Duration, TimeZone, Utc};
use Humanize;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Tense {
Past,
Present,
Future,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Accuracy {
Rough,
Precise,
}
impl Accuracy {
pub fn is_precise(&self) -> bool {
*self == Accuracy::Precise
}
pub fn is_rough(&self) -> bool {
*self == Accuracy::Rough
}
}
impl From<bool> for Accuracy {
fn from(precise: bool) -> Self {
if precise {
Accuracy::Precise
} else {
Accuracy::Rough
}
}
}
const MINUTE: i64 = 60;
const HOUR: i64 = MINUTE * 60;
const DAY: i64 = HOUR * 24;
const WEEK: i64 = DAY * 7;
const MONTH: i64 = DAY * 30;
const YEAR: i64 = DAY * 365;
#[derive(Debug)]
enum TimePeriod {
Now,
Seconds(i64),
Minutes(i64),
Hours(i64),
Days(i64),
Weeks(i64),
Months(i64),
Years(i64),
Eternity,
}
impl TimePeriod {
fn to_string_precise(&self) -> String {
use self::TimePeriod::*;
match *self {
Now => String::from("now"),
Seconds(1) => String::from("1 second"),
Seconds(n) => format!("{} seconds", n),
Minutes(1) => String::from("1 minute"),
Minutes(n) => format!("{} minutes", n),
Hours(1) => String::from("1 hour"),
Hours(n) => format!("{} hours", n),
Days(1) => String::from("1 day"),
Days(n) => format!("{} days", n),
Weeks(1) => String::from("1 week"),
Weeks(n) => format!("{} weeks", n),
Months(1) => String::from("1 month"),
Months(n) => format!("{} months", n),
Years(1) => String::from("1 year"),
Years(n) => format!("{} years", n),
Eternity => String::from("eternity"),
}
}
fn to_string_rough(&self) -> String {
use self::TimePeriod::*;
match *self {
Now => String::from("now"),
Seconds(n) => format!("{} seconds", n),
Minutes(1) => String::from("a minute"),
Minutes(n) => format!("{} minutes", n),
Hours(1) => String::from("an hour"),
Hours(n) => format!("{} hours", n),
Days(1) => String::from("a day"),
Days(n) => format!("{} days", n),
Weeks(1) => String::from("a week"),
Weeks(n) => format!("{} weeks", n),
Months(1) => String::from("a month"),
Months(n) => format!("{} months", n),
Years(1) => String::from("a year"),
Years(n) => format!("{} years", n),
Eternity => String::from("eternity"),
}
}
fn to_string(&self, accuracy: Accuracy) -> String {
match accuracy {
Accuracy::Rough => self.to_string_rough(),
Accuracy::Precise => self.to_string_precise(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct HumanTime(Duration);
impl HumanTime {
pub fn to_text_en(&self, accuracy: Accuracy, tense: Tense) -> String {
let mut periods = match accuracy {
Accuracy::Rough => self.rough_period(),
Accuracy::Precise => self.precise_period(),
};
let first = periods.remove(0);
let last = periods.pop();
let append = |acc: String, p: TimePeriod| format!("{}, {}", acc, p.to_string(accuracy));
let mut text = periods.into_iter().fold(first.to_string(accuracy), append);
if let Some(last) = last {
text = format!("{} and {}", text, last.to_string(accuracy));
}
match tense {
Tense::Past => format!("{} ago", text),
Tense::Future => format!("in {}", text),
Tense::Present => format!("{}", text),
}
}
fn tense(&self, accuracy: Accuracy) -> Tense {
match self.0.num_seconds() {
-10...10 if accuracy.is_rough() => Tense::Present,
seconds if seconds.is_negative() => Tense::Past,
seconds if seconds.is_positive() => Tense::Future,
_ => Tense::Present,
}
}
fn rough_period(&self) -> Vec<TimePeriod> {
use self::TimePeriod::*;
let period = match self.0.num_seconds().abs() {
n if n > 547 * DAY => Years(max(n / YEAR, 2)),
n if n > 345 * DAY => Years(1),
n if n > 45 * DAY => Months(max(n / MONTH, 2)),
n if n > 29 * DAY => Months(1),
n if n > 10 * DAY + 12 * HOUR => Weeks(max(n / WEEK, 2)),
n if n > 6 * DAY + 12 * HOUR => Weeks(1),
n if n > 36 * HOUR => Days(max(n / DAY, 2)),
n if n > 22 * HOUR => Days(1),
n if n > 90 * MINUTE => Hours(max(n / HOUR, 2)),
n if n > 45 * MINUTE => Hours(1),
n if n > 90 => Minutes(max(n / MINUTE, 2)),
n if n > 45 => Minutes(1),
n if n > 10 => Seconds(n),
0...10 => Now,
_ => Eternity,
};
vec![period]
}
fn precise_period(&self) -> Vec<TimePeriod> {
use self::TimePeriod::*;
let zero = Duration::zero().num_seconds();
let mut duration = self.0.num_seconds().abs();
let mut periods = Vec::<TimePeriod>::new();
if duration >= YEAR {
periods.push(Years(duration / YEAR));
duration %= YEAR;
}
if duration >= MONTH {
periods.push(Months(duration / MONTH));
duration %= MONTH;
}
if duration >= WEEK {
periods.push(Weeks(duration / WEEK));
duration %= WEEK;
}
if duration >= DAY {
periods.push(Days(duration / DAY));
duration %= DAY;
}
if duration >= HOUR {
periods.push(Hours(duration / HOUR));
duration %= HOUR;
}
if duration >= MINUTE {
periods.push(Minutes(duration / MINUTE));
duration %= MINUTE;
}
if duration > zero || periods.is_empty() {
periods.push(Seconds(duration));
}
periods
}
fn locale_en(&self, accuracy: Accuracy) -> String {
let tense = self.tense(accuracy);
self.to_text_en(accuracy, tense)
}
}
impl fmt::Display for HumanTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let accuracy = f.alternate().into();
f.pad(&self.locale_en(accuracy))
}
}
impl From<Duration> for HumanTime {
fn from(duration: Duration) -> Self {
HumanTime(duration)
}
}
impl<TZ> From<DateTime<TZ>> for HumanTime
where
TZ: TimeZone,
{
fn from(dt: DateTime<TZ>) -> Self {
dt.signed_duration_since(Utc::now()).into()
}
}
impl Humanize for Duration {
fn humanize(&self) -> String {
format!("{}", HumanTime::from(*self))
}
}
impl<TZ> Humanize for DateTime<TZ>
where
TZ: TimeZone,
{
fn humanize(&self) -> String {
format!("{}", HumanTime::from(self.clone()))
}
}