Created
December 6, 2023 14:46
-
-
Save shinkou/ea72017eef6d375fb61abb8d1061532e to your computer and use it in GitHub Desktop.
A snippet which finds out whether a given date is DST (Daylight Saving Time) or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Reference Source | |
# https://www.nist.gov/pml/time-and-frequency-division/popular-links/daylight-saving-time-dst | |
function is_dst() | |
{ | |
epoch_secs=$(date --date="$1" +%s) | |
src_yr=$(date --date="$1" +%Y) | |
mar1wday=$(date --date="$src_yr-03-01" +%w) | |
nov1wday=$(date --date="$src_yr-11-01" +%w) | |
# 2nd Sunday of March | |
days=$((mar1wday % 7 + 7)) | |
dst_start=$(date --date="$src_yr-03-01 + $days days" +%s) | |
# 1st Sunday of November | |
days=$((nov1wday % 7)) | |
dst_stop=$(date --date="$src_yr-11-01 + $days days" +%s) | |
if [[ $epoch_secs -gt $dst_start && $epoch_secs -le $dst_stop ]]; then | |
return 1 | |
else | |
return 0 | |
fi | |
} | |
for arg in $@; do | |
is_dst $arg | |
if [[ $? -eq 0 ]]; then | |
echo no | |
else | |
echo yes | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment