[Python] Is there a way of generating 3 letters codes / abbreviations for time zones in python?

Discussão em 'Python' iniciado por Stack, Janeiro 26, 2025.

  1. Stack

    Stack Membro Participativo

    Obviously, printing now time, date and time zone is possible by using datetime and time module (one of many ways):

    timezone_name = time.tzname[time.localtime().tm_isdst]

    However I need to get 3 letter code for this specific time zone - not knowing beforehand in which time zone we are located in.

    I checked many libraries (timezone, time, pytz etc.), however all of them base on either passing the time zone by oneself or generating the whole time zone name - not the abbreviation.

    I wonder, is there any way of achieving that in python? Thank you for any help!

    Of course the simplest way is extracting capitalized letters, but it is not so elegant:

    time_zone = time.tzname[time.localtime().tm_isdst] tz_abbrev = ''.join([char for char in time_zone if char.isupper()])

    Continue reading...

Compartilhe esta Página