DST and datetime

W. eWatson wolftracks at invalid.com
Wed Dec 30 14:49:13 EST 2009


Try this. It works for me and my application.

=============Program================
from datetime import datetime, timedelta
import time

DST_dict = { # West coast, 8 hours from Greenwich for PST
          2007:("2007/03/11 02:00:00", "2007/11/04 02:00:00"),
          2008:("2008/03/09 02:00:00", "2008/11/02 02:00:00"),
          2009:("2009/03/08 02:00:00", "2009/11/01 02:00:00"),
          2010:("2010/03/14 02:00:00", "2010/11/07 02:00:00")}

def adjust_DST(DT_stamp, spring, fall):
     # yyyy/mm/dd hh:mm:ss in,
     print "Date: ", DT_stamp
     format = '%Y/%m/%d %H:%M:%S'
     dt = datetime(*(time.strptime(DT_stamp, format)[0:6])) # get six tuple
     dspring = datetime(*(time.strptime(spring, format)[0:6]))
     dfall   = datetime(*(time.strptime(fall, format)[0:6]))
     if ((dt >= dspring) and (dt <= dfall)):
         print "   adjustment"
         adj = timedelta(seconds = 3600)
         dt = dt + adj
     else:
         print "   no adjustment"
     format = '%Y/%m/%d %H:%M:%S'
     return dt.strftime(format)

print "DST Adjustment"
print "   " + adjust_DST("2007/03/28 12:45:10", \
         "2007/03/11 02:00:00", "2007/11/04 02:00:00" )
print "   " + adjust_DST("2009/11/26 20:35:15", \
         "2007/03/11 02:00:00", "2007/11/04 02:00:00" )

=======================Results==============
ST Adjustment
Date:  2007/03/28 12:45:10
    adjustment
    2007/03/28 13:45:10
Date:  2009/11/26 20:35:15
    no adjustment
    2009/11/26 20:35:15



More information about the Python-list mailing list