uforanch
uforanch

Reputation: 11

Reading time zone strings in python

I'm have a series of strings of dates and want to make all of them into datetime objects for a searchable database. I'm really having trouble understanding the time zone.

In the applications I'm working with, sometimes the time zone is expressed as three letters ("EST") and sometimes it's "-5:00".

pytz.timezone('EST') is okay. pytz.timezone('UTC+05:00') does not seem to work. However, I CAN get it pytz to output UTC as a string. For the utc times we have datetime.timezone(datetime.timedetla(hours=-5), 'UTC') but that won't be consistent with pytz.timezone. I would like to use pytz.timezone for all the time zones. How do I do it?

Upvotes: 0

Views: 67

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241515

Since some of your strings have only abbreviations, sorry but you're out of luck. There is no solution.

The problem is that time zone abbreviations are not unique. Some common examples: CST has 3 different interpretations (Central (US), China, or Cuba), and IST has 3 different interpretations (Israel, Ireland, and India). Which should your program pick?

It's not a problem just for Python. It's the nature of time zone abbreviations. You'd need some other information to disambiguate.

Upvotes: 1

Related Questions