w3resource

Python: Get different time values with components timezone, timezone abbreviations

Python Datetime: Exercise-57 with Solution

Write a Python program to get different time values with components timezone, timezone abbreviations, the offset of the local (non-DST) timezone, DST timezone and time of different timezones.

Sample Solution:

Python Code:

import time
import os
def zone_info():
   print('TZ   :', os.environ.get('TZ', '(not set)'))
   print('Timezone abbreviations:', time.tzname)
   print('Timezone : {} ({})'.format(
       time.timezone, (time.timezone / 3600)))
   print('DST timezone ', time.daylight)
   print('Time :', time.strftime('%X %x %Z'),'\n')
print('Default Zone:')
zone_info()
TIME_ZONES = [
   'Pacific/Auckland',
   'Europe/Berlin',
   'America/Detroit',
   'Singapore',
]
for zone in TIME_ZONES:
   os.environ['TZ'] = zone
   time.tzset()
   print(zone, ':')
   zone_info()

Sample Output:

Default Zone:
TZ   : (not set)
Timezone abbreviations: ('UTC', 'UTC')
Timezone : 0 (0.0)
DST timezone  0
Time : 11:30:05 04/13/21 UTC 

Pacific/Auckland :
TZ   : Pacific/Auckland
Timezone abbreviations: ('NZST', 'NZDT')
Timezone : -43200 (-12.0)
DST timezone  1
Time : 23:30:05 04/13/21 NZST 

Europe/Berlin :
TZ   : Europe/Berlin
Timezone abbreviations: ('CET', 'CEST')
Timezone : -3600 (-1.0)
DST timezone  1
Time : 13:30:05 04/13/21 CEST 

America/Detroit :
TZ   : America/Detroit
Timezone abbreviations: ('EST', 'EDT')
Timezone : 18000 (5.0)
DST timezone  1
Time : 07:30:05 04/13/21 EDT 

Singapore :
TZ   : Singapore
Timezone abbreviations: ('+08', '+08')
Timezone : -28800 (-8.0)
DST timezone  0
Time : 19:30:05 04/13/21 +08 

Flowchart:

Flowchart: Get different time values with components timezone, timezone abbreviations.

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to get time values with components using local time and gmtime.
Next: Write a Python program that can suspend execution of a given script a given number of seconds.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.