-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUtils.elm
78 lines (64 loc) · 1.83 KB
/
Utils.elm
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
module Utils exposing (timeAgo)
import Time exposing (Time)
timeAgo : Time -> Time -> String
timeAgo time now =
-- Hint: Time is a Float
let
seconds =
(now - time) / 1000
minutes =
seconds / 60
hours =
minutes / 60
days =
hours / 24
months =
days / (365.25 / 12)
years =
days / 365.25
in
if seconds < 10 then
"a few seconds ago"
else if seconds < 55 then
plural "second" seconds
else if seconds |> within 55 65 then
"about a minute ago"
else if minutes < 55 then
plural "minute" minutes
else if minutes |> within 55 65 then
"about an hour ago"
else if hours < 22 then
plural "hour" hours
else if hours |> within 22 26 then
"about a day ago"
else if days |> within 6 8 then
"about a week ago"
else if days |> within 14 16 then
"about 2 weeks ago"
else if days |> within 20 22 then
"about 3 weeks ago"
else if days < 27 then
plural "day" days
else if days |> within 27 33 then
"about a month ago"
else if months < 11 then
plural "month" months
else if months |> within 11 13 then
"about a year ago"
else
plural "year" years
within : Float -> Float -> Float -> Bool
within low high value =
value >= low && value <= high
plural : String -> Float -> String
plural unit amount =
let
roundAmount =
round amount
unitPlural =
if roundAmount > 1 then
unit ++ "s"
else
unit
in
(toString roundAmount) ++ " " ++ unitPlural ++ " ago"