Utcoffset() Method Of Timezone Class In Python

Overview:

  • UTC offset is the time difference between one place on the Earth and the UTC.
  • The method utcoffset() returns the UTC offset for a timezone instance.

Example:

# Example Python program that prints the UTC offset
# from a timezone instance
from datetime import timedelta, timezone, time

# Create a timedelta with +9 hours as duration
jstOffset = timedelta(hours = 9)
jst = timezone(jstOffset, "Japanese Standard Time")

# Print the UTC offset
print("UTC offset from time zone:")

oft = jst.utcoffset(None)
print(oft)
print(type(oft))

# Japanese afternoon
jpAfternoon = time(hour = 13, tzinfo = jst)
print("Time:")
print(jpAfternoon.__format__("%I:%M %p JST"))

Output:

UTC offset from time zone:

9:00:00

<class 'datetime.timedelta'>

Time:

01:00 PM JST

 


Copyright 2023 © pythontic.com