Using bash it is possible to compare time easily in epoch format. If you need to compare date like 14:23 with 14:17 you need to transform it to seconds since epoch and compare as two integers. Here it is my function in ~/.bashrc and examples
# compare_time time1 time2 - say which is greater: 1 or 2; if they are equal say 1.
# Arguments should fit for utility date.
compare_time() {
greater='1'
s1=$(date -d"$1" +%s)
s2=$(date -d"$2" +%s)
[ $s2 -gt $s1 ] && greater='2'
echo $greater
}
After adding this to ~/.bashrc need to source it to terminal for use new function compare_time:
source ~/.bashrc
Let's try with some dates:
compare_time 10:34 02:00
1compare_time 10:34 22:00
2compare_time 10:34:55 10:34:41
1compare_time "2022-08-04" "2022-08-15"
2compare_time "2022-08-04 15:15" "2022-08-04 11:45"
1
The simplest alarm on Bash
Check time and play music if current time is after necessary date&time point:
# clo until - simplest alarm playing music when date and time reach 'until'
# argument should fit for utility date.
clo() {
until="$1"
while [ $(compare_time "$(date)" "$until") = "2" ]; do
echo `date +%H:%M` wait until $until
sleep 10
done
echo `date` it\'s time now
mplayer -really-quiet -volume 40 /home/user/music/alarm_sound.mp3;
}clo 15:05
15:03 wait until 15:05
..
Fri 12 Aug 2022 03:05:10 PM UTC it's time now
..Playing /home/user/music/alarm_sound.mp3.
Here we use mplayer, that may be you need to install `apt install mplayer` or use another like audacious, vlc, and so on.
Also there is utility to compare date points dateutils.ddiff, it is more complicated.