-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhmadd
More file actions
executable file
·62 lines (58 loc) · 1.68 KB
/
hmadd
File metadata and controls
executable file
·62 lines (58 loc) · 1.68 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
#!/bin/bash
exec 3>&2 2>/dev/null
long="$(</etc/default/longitude)"
lat="$(</etc/default/latitude)"
cache="/tmp/.sunriseset.${USER:-$(id -un)}"
usage() {
echo "${0##*/} { --seconds | --minutes | --hm } HHMM | sunrise | sunset ..." >&3
exit 1
}
sunriseset() {
/usr/bin/perl -e '
use DateTime;
use DateTime::Event::Sunrise;
my $s = DateTime::Event::Sunrise->new(longitude => $ARGV[0],
latitude => $ARGV[1]);
my $d = DateTime->now(time_zone => "local");
print $s->sunrise_datetime($d), "\n";
print $s->sunset_datetime($d), "\n";
' -- "$@"
}
if grep "$(date +%Y-%m-%dT)" "${cache}" >&/dev/null; then
sunrise="$(sed -n 's/.*T0*\([^:]*\):\([^:]*\).*/\1\2/;1p' "${cache}")"
sunset="$(sed -n 's/.*T0*\([^:]*\):\([^:]*\).*/\1\2/;2p' "${cache}")"
else
>"${cache}"
data="$(sunriseset "${long:-0}" "${lat:-0}" | tee -a "${cache}")"
sunrise="$(sed -n 's/.*T0*\([^:]*\):\([^:]*\).*/\1\2/;1p' <<<"${data}")"
sunset="$(sed -n 's/.*T0*\([^:]*\):\([^:]*\).*/\1\2/;2p' <<<"${data}")"
fi
acc=0
seconds=
minutes=
for i in "$@"; do
case "${i}" in
--seconds) seconds=t; continue;;
--minutes) minutes=t; continue;;
--hm) seconds=; minutes=; continue;;
sunrise) i="${sunrise}";;
sunset) i="${sunset}";;
*) [[ "${i}" =~ ^-?[0-9]?[0-9]?[0-9]?[0-9]$ ]] || usage
[[ "${i}" =~ ^0*([0-9].*) ]] && i="${BASH_REMATCH[1]}"
;;
esac
i=$((i/100*60+i%100))
acc=$((acc+i))
done
if [ $acc -lt 0 ]; then
acc=$((1440-(-acc%1440)))
else
acc=$((acc%1440))
fi
if [ -n "${seconds}" ]; then
echo "$((60*${acc}))"
elif [ -n "${minutes}" ]; then
echo "${acc}"
else
printf '%02d%02d\n' $((acc/60)) $((acc%60))
fi