-1

I am trying to get the UTC offset as a string from my timezone using the Python library pytz.

I am defining it as follows:

import pytz
tz = pytz.timezone('Africa/Cairo')

Now, I want to get '+02:00' from the tz variable, as that is the corresponding UTC offset for Africa/Cairo.

How can I do this? Thanks!

Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110
darkhorse
  • 8,192
  • 21
  • 72
  • 148

1 Answers1

1

You can use datetime to get current date/time in given timezone and then extract UTC offset,

import pytz
import datetime
tz = pytz.timezone('Africa/Cairo')


print(datetime.datetime.now(tz).utcoffset().total_seconds()/3600)
# output,
2.0
Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110