Tzname() method of timezone class

Overview:

  • The method tzname() returns the name of the time zone given during the construction of the timezone object.
  • Class timezone only acts as a container of two vital pieces of information pretaining to a time zone:
    1. UTC Offset as a timedelta
    2. Name of the time zone as a string - Developer gives this name to identify a time zone for later processing.

Example:

  • Unless specifically stated that the created time zone denotes the "Chile Time" through the name parameter of the timezone constructor, -4.00 hours could generally mean Atlantic Standard Time or Amazon Time as well. The Developer can use this string elsewhere while processing the time.

# Example Python program that returns
# the name of the timezone given
# during the creation of a timezone object
from datetime import timedelta, timezone, datetime

# Create a time duration of -4:00 hours
# Here the negative sign means it is four hours
# behind the UTC 
diff            = timedelta(hours = -4)
tz              = timezone(diff, "Chile Time")
chileEvening    = datetime(year = 2021, month = 1, day = 1, tzinfo = tz)
print("Date and Time:%s"%chileEvening)
print("UTC Offset:%s"%tz.utcoffset(None).total_seconds())
print("Time zone:%s"%tz.tzname(None))

Output:

Date and Time:2021-01-01 00:00:00-04:00

UTC Offset:-14400.0

Time zone:Chile Time

 


Copyright 2024 © pythontic.com