[wplug] Daylight Saving Time

Robert Supansic rsupansic at libcom.com
Fri Mar 14 10:40:48 EST 2003


Several months ago, I raised a question at a Users' meeting about automatically adjusting 
for Daylight Saving Time (DST).  Here's the problem:

1.  Linux computers are generally set to Coordinated Universal Time (UTC, formerly 
Greenwich Mean Time or Zulu Time).   Under UTC, Linux computers automatically adjust for 
Daylight Saving Time.

2.  Windows computers run on local time, e.g. Eastern Standard Time, Pacific Standard 
Time, etc..  Under local time, Windows computers also automatically adjust for Daylight 
Saving Time.

3.  When running a Linux fileserver with Windows clients, the fileserver should be set to local 
time to avoid giving the Windows clients indigestion; however,  this disables the automatic 
updating of DST under Linux.  If the fileserver also synchronizes the system clock on the 
clients, this effectively disables the automatic DST updating on the clients as well.

The following script program fixes the problem, I think.  It is designed to run on any reboot 
and on Saturdays in April and October.

It is less simple than I would like.  It requires a small file in /etc to record the last timezone 
setting.  This is because I know of no way to change the timezone string which is output by 
the "date" command.  (If anyone does, I would appreciate the information.)

The script follows:

#!   /bin/bash

#    /sbin/init.d/DST: automatically sets system clock back/forward one
#                                     hour for Daylight Saving Time on Linux computers
#                                     set to local time rather than UTC.
#                                     Called from crontab and on bootup.

#    Usage: /sbin/init.d/DST

#    03/05/2003 by Bob Supansic / RK Associates

#    Distributions vary in the handling of the boot sequence.  SuSE
#    uses the directory /sbin/init.d.  To execute this program on bootup,
#    it should be called from /sbin/init.d/boot.local.

#    In the Eastern Time Zone of the US, Daylight Saving Time begins
#    at 2:00 am on the first Saturday in April and ends at 2:00 am on
#    the last Saturday in October.  In /etc/crontab, the program should
#    therfore run at 2:00 on every Saturday in April and October:

#    0 2 0   4  6   /sbin/init.d/DST
#    0 2 0 10  6   /sbin/init.d/DST

#    (In Europe, Summer Time begins at 1 am on the last Sunday in
#    March and ends at 1 am on the last Sunday in October.  Reprogram
#    accordingly.)


THIS_MONTH="$( date +%-m )"                       		# Month number of today
THIS_YEAR=$( date +%-Y )                          		# Year for cal command
TODAYS_DAY="$( date +%-d | tr -d " " )"           		# Day number of today
TZ_IS="EST"                                       			# Today's timezone; EST=default

if [ ! -f /etc/CurrentTZ ]                        			# Use this file to store 
then                                              				# previous TZ setting.
        echo EST > /etc/CurrentTZ
fi
TZ_WAS="$( cat /etc/CurrentTZ )"                  		# Previous time zone setting

#    If necessary, reset the time zone to Eastern Daylight Time (EDT).

if [ "$THIS_MONTH" -gt 4 -a "$THIS_MONTH" -lt 10 ]
then
        TZ_IS="EDT"
fi
if [ "$THIS_MONTH" -eq 4 ]
then
         DST_START=$( cal $THIS_MONTH $THIS_YEAR | tail +3 | head -n1 | \
        cut -b18- | tr -d " " )

        if [ "$TODAYS_DAY" -ge "$DST_START" ] 
        then
                 TZ_IS="EDT"
        fi
fi
if [ "$THIS_MONTH" -eq 10 ]
then

         DST_END=$( cal $THIS_MONTH $THIS_YEAR | tail +3 | tail -2 | cut -b18- | tr -d " " )

#      If there is no Saturday in the last week of October, get the Saturday
#      of the next-to-last week.

         if [ -z "$DST_END" ]
        then
                 DST_END=$( cal $THIS_MONTH $THIS_YEAR | tail +3 | tail -3 | head -n1 | \
                 cut -b18- | tr -d " " )
         fi
         if [ "$TODAYS_DAY" -lt "$DST_END" ] 
         then
                  TZ_IS="EDT"
         fi
fi

#    For testing/debugging, change -s below to -d

if [ "$TZ_WAS" == "EST" -a "$TZ_IS" == "EDT" ]
then
         date -s "-1 hour"
fi
if [ "$TZ_WAS" == "EDT" -a "$TZ_IS" == "EST" ]
then
         date -s "+1 hour"
fi




----------------
[Virus Scanned by Libcom]
[Libcom: The only true $9.95 Unlimited Service in Pittsburgh]




More information about the wplug mailing list