2

Maybe someone came across and will be able to give advise.

I am getting the time zone "Europe / Moscow", but it needs to be converted to "UTC + 3". Only an idea with a dictionary comes to mind, but this is rather a last resort. I would not be surprised if there are ready-made solutions, but I did not google it well)

Example:

timezone = "Europe/Moscow" -> timezone = "UTC+3" 

1 Answer 1

3

Python 3.9+:

You can use the zoneinfo package. (link)

Make sure to consider daylight saving time.

from datetime import datetime import zoneinfo zone = zoneinfo.ZoneInfo("Europe/Moscow") offset = datetime.now(zone).utcoffset().total_seconds()//(60*60) print(offset) # 3 / 4 - This depends on daylight saving time print(f"UTC+{int(offset)}") # UTC+3 / UTC+4 

Older:

Try pytz, but make sure to consider daylight saving time. (link)

import pytz from datetime import datetime moscow_tz = pytz.timezone("Europe/Moscow") offset = moscow_tz.utcoffset(datetime.now()) print(offset) # 3:00:00 / 4:00:00 - This depends on daylight saving time print(f"UTC+{int(offset.total_seconds()//(60*60))}") # UTC+3 / UTC+4 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.