Test current time against target hour in a shell script to determine if current time is off-hours or business hours

Got a shell script that needs to adjust some variables depending on time of day. Specifically, between the hours of 9 PM and 6 AM Eastern time, which are considered "off-hours" for this application.

I can grab the current hour with "date +%k", then test it against the target hour with "-ge" "-le" operators. To make sure I got it right, set the time with "date -d" to test against using the following script. Seems to be working..

1
2
3
4
5
6
7
8
9
10
11
12
for hour in {0..23}; do
  for min in 00 30; do
    date=$(date -d "2011-11-18 $hour:$min" "+%F %T %Z")
    hour=$(date -d "2011-11-18 $hour:$min" "+%k")
    if [ $hour -ge 18 ] || [ $hour -le 2 ]; then
      OffHours=yes
    else
      OffHours=no
    fi
    echo "test time is $date, OffHours = $OffHours"
  done
done
test time is 2011-11-18 00:00:00 PST, OffHours = yes
test time is 2011-11-18 00:30:00 PST, OffHours = yes
test time is 2011-11-18 01:00:00 PST, OffHours = yes
test time is 2011-11-18 01:30:00 PST, OffHours = yes
test time is 2011-11-18 02:00:00 PST, OffHours = yes
test time is 2011-11-18 02:30:00 PST, OffHours = yes
test time is 2011-11-18 03:00:00 PST, OffHours = no
test time is 2011-11-18 03:30:00 PST, OffHours = no
test time is 2011-11-18 04:00:00 PST, OffHours = no
test time is 2011-11-18 04:30:00 PST, OffHours = no
test time is 2011-11-18 05:00:00 PST, OffHours = no
test time is 2011-11-18 05:30:00 PST, OffHours = no
test time is 2011-11-18 06:00:00 PST, OffHours = no
test time is 2011-11-18 06:30:00 PST, OffHours = no
test time is 2011-11-18 07:00:00 PST, OffHours = no
test time is 2011-11-18 07:30:00 PST, OffHours = no
test time is 2011-11-18 08:00:00 PST, OffHours = no
test time is 2011-11-18 08:30:00 PST, OffHours = no
test time is 2011-11-18 09:00:00 PST, OffHours = no
test time is 2011-11-18 09:30:00 PST, OffHours = no
test time is 2011-11-18 10:00:00 PST, OffHours = no
test time is 2011-11-18 10:30:00 PST, OffHours = no
test time is 2011-11-18 11:00:00 PST, OffHours = no
test time is 2011-11-18 11:30:00 PST, OffHours = no
test time is 2011-11-18 12:00:00 PST, OffHours = no
test time is 2011-11-18 12:30:00 PST, OffHours = no
test time is 2011-11-18 13:00:00 PST, OffHours = no
test time is 2011-11-18 13:30:00 PST, OffHours = no
test time is 2011-11-18 14:00:00 PST, OffHours = no
test time is 2011-11-18 14:30:00 PST, OffHours = no
test time is 2011-11-18 15:00:00 PST, OffHours = no
test time is 2011-11-18 15:30:00 PST, OffHours = no
test time is 2011-11-18 16:00:00 PST, OffHours = no
test time is 2011-11-18 16:30:00 PST, OffHours = no
test time is 2011-11-18 17:00:00 PST, OffHours = no
test time is 2011-11-18 17:30:00 PST, OffHours = no
test time is 2011-11-18 18:00:00 PST, OffHours = yes
test time is 2011-11-18 18:30:00 PST, OffHours = yes
test time is 2011-11-18 19:00:00 PST, OffHours = yes
test time is 2011-11-18 19:30:00 PST, OffHours = yes
test time is 2011-11-18 20:00:00 PST, OffHours = yes
test time is 2011-11-18 20:30:00 PST, OffHours = yes
test time is 2011-11-18 21:00:00 PST, OffHours = yes
test time is 2011-11-18 21:30:00 PST, OffHours = yes
test time is 2011-11-18 22:00:00 PST, OffHours = yes
test time is 2011-11-18 22:30:00 PST, OffHours = yes
test time is 2011-11-18 23:00:00 PST, OffHours = yes
test time is 2011-11-18 23:30:00 PST, OffHours = yes

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).