DST and datetime

W

W. eWatson

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
 
L

Lawrence D'Oliveiro

Or you could use the ready-made wheel maintained by others:

tzinfo Objects
<URL:http://docs.python.org/library/datetime.html#tzinfo-objects>

But that’s only an abstract base class, which means it doesn’t actually
implement any reading of actual timezone info.
World timezone definitions, modern and historical
<URL:http://pypi.python.org/pypi/pytz>

Shame that they maintain their own duplicate of the Olson database, instead
of reading the original directly from /usr/share/zoneinfo
<http://www.codecodex.com/wiki/Reading_time_zone_files>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top