-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimezones_example.py
71 lines (58 loc) · 2.35 KB
/
timezones_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'''Timezones Example'''
# A program that allows a user to choose one time zones from a list.
# The program will then display the time in that timezone,
# as well as local time & UTC time.
import datetime
import pytz
# References
# -----------------------------------------------------------------------------
# print all timezones:
for x in pytz.all_timezones:
print(x)
# print all country names, timezones and their times:
for x in sorted(pytz.country_names):
print("{}: {}".format(x, pytz.country_names[x]))
if x in pytz.country_timezones:
for zone in sorted(pytz.country_timezones[x]):
tz_to_display = pytz.timezone(zone)
local_time = datetime.datetime.now(tz=tz_to_display)
print("\t{}: {}:".format(zone, local_time))
else:
print("\tNo timezone defined")
# Program
# -----------------------------------------------------------------------------
timezones = {'vancouver': 'America/Vancouver',
'luxembourg': 'Europe/Luxembourg',
'hong kong': 'Hongkong',
'switzerland': 'Europe/Zurich',
'shanghai': 'Asia/Shanghai',
'cuba': 'America/Havana',
'berlin': 'Europe/Berlin',
'turks & caicos': 'America/Grand_Turk',
'singapore': 'Singapore'}
for zone in sorted(timezones):
print(zone.title())
while True:
selection = input('Choose a q (q to quit): ').lower()
if selection == 'q':
break
if selection in timezones:
country = timezones[selection]
tz_to_display = pytz.timezone(country)
world_time = datetime.datetime.now(tz=tz_to_display)
# unformatted output:
# print("{} time is: {}".format(selection.title(), world_time))
# print("Local time is: {}".format(datetime.datetime.now()))
# print("UTC is: {}".format(datetime.datetime.utcnow()))
# fancier output:
world = '{} time is:'.format(selection.title())
local = 'Local time is:'
utc = 'UTC time is:'
print("{:24} {} {}".format(
world, world_time.strftime('%A %x %X'), world_time.tzname()))
print("{:24} {}".format(
local, datetime.datetime.now().strftime('%A %x %X')))
print("{:24} {}".format(
utc, datetime.datetime.utcnow().strftime('%A %x %X')))
else:
print("That's not in my list")