1

I am looking to get the timezone of the physical, virtual, container node?

The following provides me with short timezone name -

import datetime
import dateutil.tz
tz_name = datetime.datetime.now(dateutil.tz.tzlocal()).tzname()

The above gives me MDT or EDT depending on the node's timezone.

How I can get the full timezone name e.g. America/New_York or America/Denver instead?

Edit: OK I think the following would work for me -

from datetime import datetime
from tzlocal import get_localzone  
_now = datetime.now(tz=get_localzone())  
_now.tzinfo.key # prints America/New_York
VAgrawal
  • 11
  • 2
  • 2
    While you can do this *sometimes*, there are various time zone abbreviations which are ambiguous - e.g. CST, which could be America/Havana or America/Chicago (amongst others). Does `dateutil.tz.tzlocal()` (without referring to the current date/time) not have anything like an ID or name? (Or if not, can you use a different API?) – Jon Skeet Nov 03 '22 at 17:16
  • 1
    have a look at [tzlocal](https://github.com/regebro/tzlocal) - imho the most reliable way to get the IANA name of your local time zone (what your machine is configured to use). – FObersteiner Nov 03 '22 at 17:28
  • @JonSkeet - that's a good point. However, I was hoping if there was a way to find out the IANA name of the timezone configured on the machine somehow. I agree mapping CDT or CST to a timezone is 1 to multi issue... I guess my second question in the OP is invalid. – VAgrawal Nov 03 '22 at 18:41
  • @FObersteiner - Hoping to find the right method... looking further in tzlocal. – VAgrawal Nov 03 '22 at 18:43
  • related: [Get local time zone name on Windows](https://stackoverflow.com/q/62330675/10197418) – FObersteiner Nov 04 '22 at 07:17
  • @VAgrawal- Instead of editing your question to contain your solution, you can actually [answer your own question](https://stackoverflow.com/help/self-answer) once you have 15 rep. – Matt Johnson-Pint Nov 04 '22 at 18:06

0 Answers0