Unix/Linux date command fails when calculating “yesterday”

Problem: A shell-script running every day short after midnight doesn’t work properly once per year after daylight-saving time adjustment. e.g. an expected logfile is missing (“somefile.YYYYMMDD.log”)

Discussion: The Unix date command calculates the wrong date when used with “–date yesterday” on the day after daylight-saving time adjustment.

# date
Mon Mar 31 00:15:03 CEST 2014
# date --date 'yesterday'
Sat Mar 29 23:15:09 CET 2014

Date obviously uses “24 hours ago” for yesterday. The DST adjustment day has only 23 hours, so “yesterday” is calculated wrong, between 0:00 and 1:00 on these days.

It’s interessting that “yesterday” is not allways equal to “24 hours ago”:

# date --date "2014-03-31 0:15 yesterday"
Sun Mar 30 00:15:00 CET 2014
# date --date "2014-03-31 0:15 24 hours ago"
Sat Mar 29 23:15:00 CET 2014

 

Solution: I changed the “yesterday” date calculation for nightly jobs after midnight to:

# date --date "12:00 yesterday" +%Y%m%d