Hello. You need to get the time zone number on the python time.timezone gives you a cost like. -14000 and I need to get for example Moscow 3. Someone will tell you how to do it. Thank you in advance

2 answers 2

Python developers recommend using the pytz module to work with time zones .

    To get the right value for different dates, you must use the time zone database (zoneinfo) , which stores the rules for changing time zones in the world. In Moscow, these rules have changed in recent years.

    In Python, the tzlocal module allows you to return an pytz.timezone object that contains the rules for the local time zone:

     from datetime import datetime from tzlocal import get_localzone # $ pip install tzlocal tz = get_localzone() # local timezone d = datetime.now(tz) # or some other local date utc_offset = d.utcoffset().total_seconds() 

    This code returns the current time zone in a portable manner, taking into account the rules provided by the installed version of the pytz module .

    In most cases, you can limit the standard library:

     #!/usr/bin/env python3 from datetime import datetime, timedelta, timezone d = datetime.now(timezone.utc).astimezone() # local time utc_offset = d.utcoffset() // timedelta(seconds=1) 

    In rare cases, on a system such as Windows that does not provide access to zoneinfo, this code may return an erroneous value .

    Additional options that bypass this problem and / or options for older versions of Python can be found here .