0

I have two code segments set up which return a date to the user. The first one converts the date from the CET time zone to CST and appears as follows:

from datetime import datetime
from pytz import timezone
import pytz

fmt = '%Y-%m-%d %H:%M:%S %Z%z'

cet = pytz.timezone("CET")
install_date = datetime(day=18, month=5, year=2015, hour=18, minute=00, second=00, tzinfo=cet)

hometime = timezone('Canada/Saskatchewan')
loc_dt = install_date.astimezone(hometime)

result=loc_dt.strftime(fmt)

which returns the correct result:

2015-05-18 11:00:00 CST-0600

For the second segment, I just want the date returned under the CST timezone to ensure that it will not be affected by daylight savings time:

from datetime import datetime
from pytz import timezone
import pytz

fmt = '%Y-%m-%d %H:%M:%S %Z%z'
cst = pytz.timezone('Canada/Saskatchewan')
calibration_date = datetime(day=2, month=6, year=2015, hour=10, minute=00, second=00, tzinfo=cst)
result = calibration_date.strftime(fmt)

which returns the result:

2015-06-02 10:00:00 LMT-0659

Notice the LMT-0659 result. I need it to return CST-0600 since CST is unaffected by daylight savings time. I am not sure why the conversion segment would return the CST time zone while the LMT time zone is returned in the second segment.

Does anyone know why I am receiving the LMT-0659 result and what time zone I should be using to obtain the correct CST-0600 time zone? Should I just use the 'Etc/GMT+6' time zone instead to avoid any daylight savings issues in the future?

  • Somewhat related: http://stackoverflow.com/questions/13866926/python-pytz-list-of-timezones – NightShadeQueen Jul 15 '15 at 23:08
  • Read the very beginning of the pytz docs. You need to use the `localize` function. – Matt Johnson-Pint Jul 16 '15 at 04:33
  • The pytz doc states: "normalize() and localize() are not really necessary when there are no daylight saving time transitions to deal with.". I would like to avoid using any kind of conversion. The time I am stating under datetime() is in CST so I would like to avoid extra lines of code to convert it to CST. – Matthew Strugari Jul 16 '15 at 14:31
  • That particular line is with regard to UTC. When it says "there are no daylight saving time transitions to deal with", it means over the *entire* time zone - not just that particular point in time. Since the time zones you're working with *do* have DST, then you *must* use `localize`. Otherwise you just get the first offset in the zone definition, which happens to be the LMT value. – Matt Johnson-Pint Jul 18 '15 at 05:02
  • Also note, that the definition for `CET` in the tzdb does indeed switch between CET and CEST - it just has no LMT value assigned, so you happen to get the UTC+01:00 offset, because it's the first offset in the zone definition. – Matt Johnson-Pint Jul 18 '15 at 05:07
  • Thanks for the help @MattJohnson, however the timezones I am working with do not have DST. I ended up using cst = pytz.timezone('Etc/GMT+6) and then adding in the extra line of code with .astimezone(hometime) to display everything in the correct CST timezone. – Matthew Strugari Jul 23 '15 at 20:01

0 Answers0