-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtozero
120 lines (96 loc) · 2.92 KB
/
tozero
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
#!/bin/bash
#----------------------------------------------------------------------------------
# Project Name - miscellaneous/tozero
# Started On - Sun 15 Oct 23:29:20 BST 2017
# Last Change - Mon 9 Apr 21:54:25 BST 2018
# Author E-Mail - [email protected]
# Author GitHub - https://github.com/terminalforlife
#----------------------------------------------------------------------------------
_VERSION_="2018-04-09"
XERR(){ printf "[L%0.4d] ERROR: %s\n" "$1" "$2" 1>&2; exit 1; }
ERR(){ printf "[L%0.4d] ERROR: %s\n" "$1" "$2" 1>&2; }
USAGE(){
while read -r; do
printf "%s\n" "$REPLY"
done <<-EOF
TOZERO ($_VERSION_)
Written by terminalforlife ([email protected])
Simple program to display a countdown for a target date.
SYNTAX: tozero [OPTS] TARGET
OPTS: --help|-h|-? - Displays this help information.
--version|-v - Output only the version datestamp.
--debug|-D - Enables the built-in bash debugging.
--only|-o TYPE - Where TYPE is which to only show.
--int-only|-I - Show only the integer, not its type.
NOTE: Behind the scenes, 'date' is used, so strings like "tomorrow", "next -
month", and "yesterday" can be used instead of a YYYY-MM-DD format.
The default TARGET is "tomorrow"; if not TARGET is given.
Valid TYPEs are:
seconds|s minutes|m hours|h days|d weeks|w
EXAMPLE: Display countdown until December the 11th, 2017:
$ tozero 2018-10-02
S: 3,893,051
M: 64,884
H: 1,081
D: 45
W: 6
EOF
}
while [ "$1" ]; do
case "$1" in
--help|-h|-\?)
USAGE; exit 0 ;;
--version|-v)
printf "%s\n" "$_VERSION_"
exit 0 ;;
--debug|-D)
DEBUGME="true" ;;
--only|-o)
shift; TYPE="$1" ;;
--int-only|-I)
INTONLY="true" ;;
-*)
XERR "$LINENO" "Incorrect argument(s) specified." ;;
*)
break ;;
esac
shift
done
DATE="$1"
[ -x /bin/date ] || XERR "$LINENO" "Dependency '/bin/date' not met."
[ "$DEBUGME" == "true" ] && set -x
/bin/date -d "$DATE" &> /dev/null || XERR "$LINENO" "Incorrect TARGET string."
TARGET="${DATE:-tomorrow}"
BUF1=`/bin/date -d "$TARGET" +%s`
BUF2=`/bin/date +%s`
SECS=$((BUF1-BUF2))
MINUTES=$(((BUF1-BUF2)/60))
HOURS=$(((BUF1-BUF2)/60/60))
DAYS=$(((BUF1-BUF2)/60/60/24))
WEEKS=$(((BUF1-BUF2)/60/60/24/7))
SHOW(){
if [ "$INTONLY" == "true" ]; then
printf "%-'d\n" "$2"
else
printf "%s: %-'d\n" "$1" "$2"
fi
}
case "$TYPE" in
seconds|s)
SHOW S $SECS ;;
minutes|m)
SHOW M $MINUTES ;;
hours|h)
SHOW H $HOURS ;;
days|d)
SHOW D $DAYS ;;
weeks|w)
SHOW W $WEEKS ;;
*)
SHOW S $SECS
SHOW M $MINUTES
SHOW H $HOURS
SHOW D $DAYS
SHOW W $WEEKS ;;
esac
# vim: ft=sh noexpandtab colorcolumn=84 tabstop=8 noswapfile nobackup