neglecting daylight savings

Neil Berg nberg at atmos.ucla.edu
Fri Nov 12 02:26:34 CET 2010


Hi Python community, 

I am trying to convert UTC to PST and want to neglect daylight savings even for the days that are in PDT, not PST.  I simply want every UTC date to be pushed back by 8 hours and not worry about the days when the difference is really -7 due to daylight savings.  I have written a function called utc_to_local (pasted below) that reads in the UTC year, month, day, and hour from a main code and returns that value in local standard time.  The problem is that I can't figure out how to "turn off" the automatic daylight savings switch that appears be built in to somewhere.    

Here is some sample output showing the daylight savings switch on April 2, 1995 at 2am local time, which I would like to override. 

1995-04-02 08 UTC = 1995-04-02 00 PST   
1995-04-02 09 UTC = 1995-04-02 01 PST
1995-04-02 10 UTC = 1995-04-02 03 PDT
1995-04-02 11 UTC = 1995-04-02 04 PDT

I want the third line of the output to read "1995-04-02 02" PST, the fourth to read "1995-04-02 03", etc.  
___________________________

from datetime import datetime  
from pytz import timezone
import pytz

def utc_to_local(yr,mo,dy,hr):
   fmt = '%Y-%m-%d %H %Z'
   utc = pytz.utc
   pacific = pytz.timezone("America/Los_Angeles")
   utc_dt = datetime(yr,mo,dy,hr, tzinfo=utc)
   loc_dt = utc_dt.astimezone(pacific) # local date YY-MM-DD HR TZ  
   print utc_dt.strftime(fmt), "=", loc_dt.strftime(fmt)
   loc_y = loc_dt.year # local year
   loc_m = loc_dt.month # local month
   loc_d = loc_dt.day # local day
   loc_h = loc_dt.hour # local hour 

   return loc_y, loc_m, loc_d, loc_h
_____________________________ 
Python 2.6 
Mac OS X 10.6.4  
_____________________________ 
Any help is much appreciated. 

Thank you in advance,
Neil Berg 



More information about the Python-list mailing list