0

I've got tz names in format: "EDT", "CDT".

Is it possible to set timezone to datetime instance with such tz names?

uCodista
  • 9
  • 3

1 Answers1

0

On many Unix systems (including *BSD, Linux, Solaris, and Darwin), it is more convenient to use the system’s zoneinfo (tzfile(5)) database to specify the timezone rules. To do this, set the TZ environment variable to the path of the required timezone datafile, relative to the root of the systems ‘zoneinfo’ timezone database, usually located at /usr/share/zoneinfo. For example, 'US/Eastern', 'Australia/Melbourne', 'Egypt' or 'Europe/Amsterdam'.

>>> os.environ['TZ'] = 'US/Eastern'
>>> time.tzset()
>>> time.tzname
('EST', 'EDT')
>>> os.environ['TZ'] = 'Egypt'
>>> time.tzset()
>>> time.tzname
('EET', 'EEST')

It work fine with valid timezone strings too

Yaroslav Surzhikov
  • 1,568
  • 1
  • 11
  • 16
  • Unfortunately it's not about my system. That are the values I receive in JSON data from scraper. I have two separate fields - datetime and tz in format I defined upper. And I need to somehow to apply tz to datetime. I know that 'America/Chicago' == 'CDT'. If I have 'America/Chicago' string - I can make tz easily. But I have only 'CDT' string - and I can't find out how to create tz from that. – uCodista Sep 10 '17 at 17:14
  • You could try creatr datetime object from string using strptime – Yaroslav Surzhikov Sep 10 '17 at 17:52
  • I have 2 values; `tstamp = 15051384000` `tzname = 'CDT'` `import datetime as dt` `my_time = dt.datetime.fromtimestamp(tstamp)` How to apply tz to "my_time" with given "tz_name"? – uCodista Sep 10 '17 at 18:02
  • Build string first, add timezone to it and parse string than. It might do the trick – Yaroslav Surzhikov Sep 10 '17 at 18:34