Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is zoneinfo.ZoneInfo(key) in Python?

Gutha Vamsi Krishna

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Definition

  • ZoneInfo is a class in the zoneinfo module.
  • ZoneInfo creates an instance of zone from the key passed to the constructor.
  • This key is a string that indicates the name of a zone file in the system timezone database.
  • Example keys are America/Los_Angeles, Europe/Paris

Note: The zoneinfo module is only available in Python versions 3.9 or higher.

Syntax

ZoneInfo(key)

Parameters

  • key: The ZoneInfo class constructor accepts key as a parameter.

Return value

ZoneInfo returns a ZoneInfo object that is constructed from the first matching data source on the search path.

Example 1

In the example below, we:

  • Import the ZoneInfo class from the zoneinfo module.
  • Construct the ZoneInfo object.
  • Pass America/Los_Angeles as the key to the constructor.
  • Store ZoneInfo in a and print to the console.
from zoneinfo import ZoneInfo

# constructing the ZoneInfo object
a = ZoneInfo("America/Los_Angeles")
print(a)

Example 2

In the example below, we:

  • Import the ZoneInfo class in line 1.
  • In lines 3 and 4, create ZoneInfo objects a and b from the same key, America/Los_Angeles.
  • In line 6, assert if a and b are equal.

If the ZoneInfo objects are constructed from the same key and data source path, then they point to the same object.

The code snippet outputs:

  • Succeeded: if a and b are equal.

  • Assertion Error: if a and b are not equal.

from zoneinfo import ZoneInfo

# constructing ZoneInfo objects
a = ZoneInfo("America/Los_Angeles")
b = ZoneInfo("America/Los_Angeles")

# asserting the ZoneInfo objects created above
assert a is b

Example 3

In the code below, we:

  • Import ZoneInfo and datatime in lines 1 and 2.
  • Create DateTime dt1 and dt2 of the same date with two timezones as America/Los_Angeles, Pacific/Kwajalein, by assigning ZoneInfo objects to tzinfo.
  • Run the code snippet below to see the difference in DateTime.
from zoneinfo import ZoneInfo
from datetime import datetime

#creating datetime for different timezones
dt1 = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo("America/Los_Angeles"))
dt2 = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo("Pacific/Kwajalein"))

#printing the dt1 and dt2
print("America/Los_Angels --> "+ str(dt1))
print("Pacific/Kwajalein --> "+ str(dt2))

Example 4

In the code snippet below, we deliberately pass an incorrect key to the ZoneInfo constructor to raise ZoneInfoNotFoundError.

If the key passed to ZoneInfo constructor is not valid or it cannot find the zone file in the database, then the constructor will raise ZoneInfoNotFoundError.

from zoneinfo import ZoneInfo

# deliberately passing the wrong key to raise error
a = ZoneInfo("America/xyz")

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring