Timezone Constructor

Method Name:

timezone

Method Signature:

timezone()

Parameters:

utcOffset - A timedelta instance. The timedelta instance can not represent more than -24 hours or +24 hours.

name - name of the time zone. No validation is done by the timezone class. A string value to name the time zone.

Return Value:

A timezone instance.

Overview:

  • A timezone instance can be created by calling the constructor timezone() with the arguments UTCOffset and a name representing the time zone. 

 

Example:

# Example Python program that creates a timezone instance
# using the datetime.timezone() constructor
from datetime import timedelta, timezone, time

# Difference from UTC in Seconds for Pacific Standard Time Zone
dstDiff = 8 * 60 * 60

# Create a timedelta
durationDiff = timedelta(seconds=dstDiff)

# Create a timezone instance for PST
tzone   = timezone(-durationDiff, "PST")
seattleMorning  = time(hour = 10, minute = 30, tzinfo = tzone)
print("Time in San Francisco when DST is not observed:")
print(seattleMorning)
print(seattleMorning.tzname())

Output:

Time in San Francisco when DST is not observed:

10:30:00-08:00

PST


Copyright 2023 © pythontic.com