-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_seconds
More file actions
executable file
·62 lines (55 loc) · 1.31 KB
/
format_seconds
File metadata and controls
executable file
·62 lines (55 loc) · 1.31 KB
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
#!/usr/bin/env bash
show_help() {
cat << __EOF__
Takes unsigned integer/decimal seconds and prints them as simple hours, minutes, & seconds as the time overflows.
Usage: $0 SECONDS
or: $0 --help
or: $0 --version
Options:
__EOF__
}
show_version() {
cat << __EOF__
$0 4.0.0
Copyright (C) 2025 Justin Morris
__EOF__
}
declare formatStr=''
declare sign=''
declare -i timeInput=0
shopt -s 'extglob'
allowContinue='true'
while eval "$allowContinue" && (( $# > 0 )); do
case "$1" in
(-h | --help)
show_help
exit
;;
(-v | --version)
show_version
exit
;;
(*)
allowContinue='false'
;;
esac
done
if [[ "$1" =~ ^(-?)([[:digit:]]+)(\.[[:digit:]]*)$ ]]; then
declare -r sign="${BASH_REMATCH[1]}"
declare -ir timeInput="${BASH_REMATCH[2]}"
declare formatStr="S${BASH_REMATCH[3]}"
elif [[ "$1" =~ ^(-?)(\.[[:digit:]]+)$ ]]; then
declare -r sign="${BASH_REMATCH[1]}"
declare -ir timeInput=0
declare formatStr="S${BASH_REMATCH[2]}"
else
declare -r sign=''
declare -ir timeInput="$1"
declare formatStr="S"
fi
if ((timeInput >= 86400)); then formatStr="$((timeInput / 86400))days %H hrs %M min %$formatStr s"
elif ((timeInput >= 3600)); then formatStr="%-H:%M:%$formatStr"
elif ((timeInput >= 60)); then formatStr="%-M:%$formatStr"
else formatStr="%-${formatStr}s"
fi
date -u -d "@$timeInput" +"$sign$formatStr"