|
|||
|
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 |
|
|
||||
|
||||
|
|
|
|||
|
Neil Berg <nberg@atmos.ucla.edu> writes:
> I am trying to convert UTC to PST and want to neglect daylight savings > even for the days that are in PDT, not PST. Then you don't want “PST”. That name entails all the rules of the time zone. If that's not what you want, don't ask timezone libraries for it. Similarly for the timezone named “America/Los_Angeles”; if you ask for that you are explicitly asking for all the time changes that zone entails. If you don't want it, don't ask for it. If you want exactly the same offset all the time, regardless of government DST silliness, ask for it: “UTC-8”, for example, or whatever the fixed offset from UTC you want. -- \ “Outside of a dog, a book is man's best friend. Inside of a | `\ dog, it's too dark to read.” —Groucho Marx | _o__) | Ben Finney |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|