-
Notifications
You must be signed in to change notification settings - Fork 1
/
format_time_delta
executable file
·58 lines (48 loc) · 1.81 KB
/
format_time_delta
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
#!/bin/bash
# A small demo for neat formatingof time deltas
format_time_delta() {
local ref_time=$1
local new_time=$2
if [ -z "$new_time" ]; then
new_time=$(date '+%s')
fi
local delta=$(( new_time - ref_time ))
local suffix=""
if (( delta < 0 )); then
delta=$(( -delta ))
else
suffix=" ago"
fi
local years=$(( delta / 31536000 ))
local remainder=$(( delta % 31536000 ))
local months=$(( remainder / 2592000 ))
remainder=$(( remainder % 2592000 ))
local weeks=$(( remainder / 604800 ))
remainder=$(( remainder % 604800 ))
local days=$(( remainder / 86400 ))
remainder=$(( remainder % 86400 ))
local hours=$(( remainder / 3600 ))
remainder=$(( remainder % 3600 ))
local minutes=$(( remainder / 60 ))
local seconds=$(( remainder % 60 ))
local result=()
(( years > 0 )) && result+=("${years} year$([ $years -ne 1 ] && echo "s")")
(( months > 0 )) && result+=("${months} month$([ $months -ne 1 ] && echo "s")")
(( weeks > 0 )) && result+=("${weeks} week$([ $weeks -ne 1 ] && echo "s")")
(( days > 0 )) && result+=("${days} day$([ $days -ne 1 ] && echo "s")")
(( hours > 0 )) && result+=("${hours} hour$([ $hours -ne 1 ] && echo "s")")
(( minutes > 0 )) && result+=("${minutes} minute$([ $minutes -ne 1 ] && echo "s")")
(( seconds > 0 )) && result+=("${seconds} second$([ $seconds -ne 1 ] && echo "s")")
local result_str=""
local len=${#result[@]}
if (( len > 1 )); then
result_str=$(printf ", %s" "${result[@]:0:$((len-1))}")
result_str="${result_str:2} and ${result[-1]}"
else
result_str=${result[*]}
fi
echo "${result_str}${suffix}"
}
ref_time=$(date -d "2024-01-01 12:00:00" +%s)
formatted_delta=$(format_time_delta "$ref_time")
echo "$formatted_delta"