0

I've went through a few useful discussions on the topic, but still don't understand the following:

Say, I have a string "20080730 06:23:54 PDT" which I want to convert to a timezone aware datetime object. I understand, that dateutil.parser.parse() doesn't generally parse timezones like "PDT", but looks like it does parse "UTC" and "EDT" correctly. Are these exceptions? Is "EDT" an exception b/c it's my local timezone?

import pytz
from dateutil.parser import parse
sdt="20080730 06:23:54 UTC"
dt_obj = parse(sdt)
dt_obj
datetime.datetime(2008, 7, 30, 6, 23, 54, tzinfo=tzutc())
sdt="20080730 06:23:54 EDT"
dt_obj = parse(sdt)
dt_obj
datetime.datetime(2008, 7, 30, 6, 23, 54, tzinfo=tzlocal())
sdt="20080730 06:23:54 PDT"
dt_obj = parse(sdt)
dt_obj
datetime.datetime(2008, 7, 30, 6, 23, 54)
LazyCat
  • 496
  • 4
  • 14
  • You can't rely on time zone strings because they aren't unique world-wide. – Mark Ransom Dec 06 '17 at 20:54
  • @MarkRansom Nethertheless, it converts/localizes "EDT" – LazyCat Dec 06 '17 at 20:55
  • I have no idea why EDT would work but PDT not. However if you check the timezone abbreviation list at https://www.timeanddate.com/time/zones/ you'll see that PST could mean Pacific Standard Time or it could mean Pitcairn Standard Time. – Mark Ransom Dec 06 '17 at 21:01
  • It's probably related to the fact that there are predefined methods tzutc() and tzlocal(), but I'd be intererested to see more details – LazyCat Dec 06 '17 at 21:06
  • It almost certainly is due to an abbreviation being available from `tzlocal`. I can't even get `CST` or `CDT` to work for me, even though that's the time zone I'm in. I'm using Windows 7, what's your OS? – Mark Ransom Dec 08 '17 at 16:41
  • unix. I don't understand, - you're saying, your tzlocal is CST, but "... CST" doesn't work for you? – LazyCat Dec 08 '17 at 17:10
  • What I mean is that I'm physically in the U.S. Central time zone, but the objects available to me in Python aren't aware of the CST or CDT name of this time zone. When I do the `parse` I get an unadorned `datetime` just as you do with PDT. – Mark Ransom Dec 08 '17 at 19:18

2 Answers2

0

As a pilot ,i think you just need to learn more about Time Zones .It is not as easy as you think but not difficult too.It is always safer to work/convert to UTC time first and then continue(also you must see about daylight saving time). If you want to do this for many inputs/outputs you will need a database(internet) and some regex(https://docs.python.org/3.6/howto/regex.html). Look at re-gex lib , and maybe selenium to improve your programm.
Oh sorry , to clear out EDT stands for Eastern Daylight Time (summer) and EST for Eastern Standard Time(winter).E.G. North America observe both and divides them in territories

EDITED FROM http://pytz.sourceforge.net/*

Unfortunately using the tzinfo argument of the standard datetime constructors ‘’does not work’’ with pytz for many timezones.

Issues & Limitations

Offsets from UTC are rounded to the nearest whole minute, so timezones such as Europe/Amsterdam pre 1937 will be up to 30 seconds out. This is a limitation of the Python datetime library. If you think a timezone definition is incorrect, I probably can’t fix it. pytz is a direct translation of the Olson timezone database, and changes to the timezone definitions need to be made to this source. If you find errors they should be reported to the time zone mailing list, linked from http://www.iana.org/time-zones.

0

In similar situations I've found that Eastern Daylight Time is an exception: It's often available / parseable alongside the equivalent Standard timezone names, even though the same isn't true for Pacific time or others.

Jeremy Jones
  • 4,561
  • 3
  • 16
  • 26