{"id":2110,"date":"2020-01-04T11:46:28","date_gmt":"2020-01-04T11:46:28","guid":{"rendered":"https:\/\/www.askpython.com\/?p=2110"},"modified":"2023-05-19T12:00:25","modified_gmt":"2023-05-19T12:00:25","slug":"python-pytz-module","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/python-pytz-module","title":{"rendered":"Python pytz Module: Mastering Timezones in Python\u00a0"},"content":{"rendered":"\n<p>Python offers many useful libraries and modules to simplify coding. One such module that stands out for handling date and time data is pytz.\u00a0It accounts for daylight saving time, enabling precise date-time conversions. As developers, it saves us the trouble of manual time calculations. It also supports a wide range of timezones, making it ideal for applications with a diverse user base.<\/p>\n\n\n\n<p>This module is particularly useful when working with databases stored in Coordinated Universal Time (UTC) but requiring conversions to local times for display. Understanding how to effectively use pytz can greatly enhance your Python applications, especially those dealing with time-sensitive data.<\/p>\n\n\n\n<p>The pytz module&#8217;s versatility and functionality allow it to shine in enabling accurate timezone handling in Python software. By simplifying timezone management for developers and providing local time data for international users, pytz makes a valuable addition to any Python developer&#8217;s toolkit<\/p>\n\n\n\n<p><em><strong>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/python-time-module\" data-type=\"post\" data-id=\"3789\">Python time module<\/a><\/strong><\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Install the Python pytz Module for Better Date-Time Handling<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>The <strong>Python pytz module<\/strong> offers a comprehensive set of functions and variables to handle date-time conversions across various timezones. The module provides a list or set of all supported timezones, commonly used timezones, country names, and country timezones. The pytz.timezone() function can fetch the current time of a specified timezone.<\/p>\n<\/blockquote>\n\n\n\n<p>By running the &#8216;pip install pytz&#8217; command, you can easily install the pytz module in your Python environment.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npip install pytz\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"260\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/pytz-installation-1.png\" alt=\"Pytz Installation 1\" class=\"wp-image-2241\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/pytz-installation-1.png 940w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/pytz-installation-1-300x83.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/pytz-installation-1-768x212.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><figcaption class=\"wp-element-caption\">Verifying Your Installation of the pytz Module<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Dive into the Core Functions and Variables of the Python pytz Module<\/h2>\n\n\n\n<p>Let&#8217;s now go ahead and explore the different methods and variables of the pytz module.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. Exploring Global Timezones with all_timezones and all_timezones_set<\/h3>\n\n\n\n<p>The all_timezones function of the pytz module returns a list of all the time-zones supported by Python&#8217;s pytz module.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pytz\n\nprint(&#039;The timezones supported by pytz module: &#039;, pytz.all_timezones, &#039;\\n&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Output Snippet:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The timezones supported by pytz module:  &#91;'Africa\/Abidjan', 'Africa\/Accra', 'Africa\/Addis_Ababa', 'Africa\/Algiers', 'Africa\/Asmara', 'Africa\/Asmera', 'Africa\/Bamako', 'Africa\/Bangui', 'Africa\/Banjul']<\/code><\/pre>\n\n\n\n<p>We have shown only a few values because the actual list is very long.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>This variable returns a set of all the supported timezones.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pytz\nprint(&#039;List of supported timezones by pytz module: &#039;, pytz.all_timezones_set, &#039;\\n&#039;)\n\n<\/pre><\/div>\n\n\n<p><strong>Output Snippet:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nList of supported timezones by pytz module:  LazySet({&#039;America\/Port-au-Prince&#039;, &#039;Asia\/Ust-Nera&#039;, &#039;Asia\/Vientiane&#039;, &#039;Australia\/Hobart&#039;, &#039;Asia\/Ulaanbaatar&#039;, &#039;Africa\/Lome&#039;})\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python\/examples\/difference-between-two-dates\" data-type=\"post\" data-id=\"42979\">Difference between two dates in Python<\/a><\/em><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Common Timezones Using common_timezones and common_timezones_set<\/h3>\n\n\n\n<p>This variable returns a list of commonly used time-zones.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pytz\n\nprint(&#039;Commonly used time-zones: &#039;, pytz.common_timezones, &#039;\\n&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Output Snippet:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCommonly used time-zones:  &#x5B;&#039;Africa\/Abidjan&#039;, &#039;Africa\/Accra&#039;, &#039;Africa\/Addis_Ababa&#039;, &#039;Africa\/Algiers&#039;, &#039;Africa\/Asmara&#039;, &#039;Africa\/Bamako&#039;, &#039;Africa\/Bangui&#039;, &#039;Africa\/Banjul&#039;, &#039;US\/Pacific&#039;, &#039;UTC&#039;]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pytz\nprint(&#039;common_timezones_set() = &#039;, pytz.common_timezones_set, &#039;\\n&#039;)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>common_timezones_set() =  LazySet({'America\/Eirunepe', 'Africa\/Kinshasa', 'Australia\/Sydney', 'Europe\/Malta', 'America\/Tortola', 'Canada\/Pacific', 'America\/Argentina\/Cordoba'})<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. Understanding the Time in Different Zones Using pytz.timezone() in Python<\/h3>\n\n\n\n<p>The <code>pytz.timezone()<\/code> returns the timezone object by the name. And, the <code>datetime.now()<\/code> returns the date-time of that particular time-zone.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom datetime import datetime\nimport pytz\n# getting utc timezone\nutc_time = pytz.utc\n\n# getting timezone by name\nist_time = pytz.timezone(&#039;Asia\/Kolkata&#039;)\n\n# getting datetime of specified timezone\nprint(&#039;Datetime of UTC Time-zone: &#039;, datetime.now(tz=utc_time))\nprint(&#039;Datetime of IST Time-zone: &#039;, datetime.now(tz=ist_time))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nDatetime of UTC Time-zone:  2020-01-03 17:49:14.220167+00:00\nDatetime of IST Time-zone:  2020-01-03 23:19:14.220167+05:30\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. Decode Country Names with Python&#8217;s pytz Module: country_names<\/h3>\n\n\n\n<p>The <code>country_names<\/code> returns a <a href=\"https:\/\/www.askpython.com\/python\/dictionary\/python-dictionary-dict-tutorial\" class=\"rank-math-link\">dictionary<\/a> of country ISO Alpha-2 Code and country name as a key-value pair.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pytz\n\nprint(&#039;country_names =&#039;)\nfor key, val in pytz.country_names.items():\n    print(key, &#039;=&#039;, val, end=&#039;,&#039;)\nprint(&#039;\\n&#039;)\nprint(&#039;Country name equivalent to the input country code: &#039;, pytz.country_names&#x5B;&#039;AQ&#039;])\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncountry_names =\nAD = Andorra,AE = United Arab Emirates,AF = Afghanistan,AG = Antigua &amp;amp; Barbuda,AI = Anguilla,AL = Albania,AM = Armenia,AO = Angola,AQ = Antarctica,ZW = Zimbabwe,\nCountry name equivalent to the input country code:  Antarctica\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. Using country_timezones to find time data from across different parts fo the world<\/h3>\n\n\n\n<p>This function provides a dictionary of country ISO Alpha-2 Code as key and list of supported time-zones for a particular input key (country code) as output.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pytz\nprint(&#039;country_timezones =&#039;)\nfor key, val in pytz.country_timezones.items():\n    print(key, &#039;=&#039;, val, end=&#039;,&#039;)\nprint(&#039;\\n&#039;)\nprint(&#039;Time-zones supported by Antartica =&#039;, pytz.country_timezones&#x5B;&#039;AQ&#039;])\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncountry_timezones =\nAD = &#x5B;&#039;Europe\/Andorra&#039;],AE = &#x5B;&#039;Asia\/Dubai&#039;],AF = &#x5B;&#039;Asia\/Kabul&#039;],AG = &#x5B;&#039;America\/Antigua&#039;],AI = &#x5B;&#039;America\/Anguilla&#039;],AL = &#x5B;&#039;Europe\/Tirane&#039;],AM = &#x5B;&#039;Asia\/Yerevan&#039;],AO = &#x5B;&#039;Africa\/Luanda&#039;],ZW = &#x5B;&#039;Africa\/Harare&#039;],\nTime-zones supported by Antartica = &#x5B;&#039;Antarctica\/McMurdo&#039;, &#039;Antarctica\/Casey&#039;, &#039;Antarctica\/Davis&#039;, &#039;Antarctica\/DumontDUrville&#039;, &#039;Antarctica\/Mawson&#039;, &#039;Antarctica\/Palmer&#039;]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The Python pytz module is an incredibly powerful tool when dealing with date and time across different timezones. With it, managing time in your Python applications becomes a breeze. <em>After all, in the digital age, isn&#8217;t time one of our most precious resources?<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a class=\"rank-math-link rank-math-link\" href=\"https:\/\/pypi.org\/project\/pytz\/\" target=\"_blank\" rel=\"noopener\">Official pytz Documentation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Python offers many useful libraries and modules to simplify coding. One such module that stands out for handling date and time data is pytz.\u00a0It accounts for daylight saving time, enabling precise date-time conversions. As developers, it saves us the trouble of manual time calculations. It also supports a wide range of timezones, making it ideal [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":50916,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2110","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=2110"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2110\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/50916"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=2110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=2110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=2110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}