import datetime
import pytz
available_zones ={"1":"African/Tunis",
"2":"Asia/Kolkata",
"3":"Australia/Adelaide",
"4":"Europe/Brussels",
"5":"Europe/London",
"6":"Japan",
"7":"Pacific/Tahiti"
}
print("Please Choose the timezone (or 0 to quit)")
for place in sorted(available_zones):
print("\t {} : {}".format(place,available_zones[place]))
while True:
choice = input()
if choice == 0:
break
if choice in available_zones.keys():
tz_to_display = pytz.timezone(available_zones[choice])
world_time = datetime.datetime.now(tz=tz_to_display)
print ("The time in {} is {} {} ".format(available_zones[choice],
world_time.strftime("%A %x %X %z"),world_time.tzname()))
print("LocalTime is {}".format(datetime.datetime.now().strftime("%A %x %X %z")))
print("UTCTime is {}".format(datetime.datetime.utcnow().strftime("%A %x %X %z")))
Wednesday, 28 February 2018
Python timezone challenge- Day 17
Python timezone - Day 16(1 day later)
import pytz
import datetime
country = "Europe/Moscow" #check for the spelling and case sensitivity
tz_to_display = pytz.timezone(country)
# print(tz_to_display)
# print(country)
print("The time in {} is {}".format(country,datetime.datetime.now(tz=tz_to_display)))
print("UTC is {}".format(datetime.datetime.utcnow()))
print("LocalTime here is {}".format(datetime.datetime.now()))
tz_to_display = pytz.timezone("Singapore")
print("The time in {} is {}".format("Singapore",datetime.datetime.now(tz=tz_to_display)))
#list of all timezones
for x in pytz.all_timezones:
print(x)
print("=="*50)
print(pytz.country_names)
#list of all countries
for x in pytz.country_names:
print(x)
for x in sorted(pytz.country_names):
print(x + ": " + pytz.country_names[x])
#although the below code will run,there will be error ,since the few countries
#like (Bouvet Island ) have not defined the timezones
# for x in sorted(pytz.country_names):
# print("{} : {} : {}".format(x,pytz.country_names[x],pytz.country_timezones(x)))
print("=================="*10)
#we can use the get method to avoid the below issue
for x in sorted(pytz.country_names):
print("{} : {} : {}".format(x,pytz.country_names[x],pytz.country_timezones.get(x)))
print("*************"*10)
#we can also use the below method
for x in sorted(pytz.country_names):
print("{} : {} ".format(x,pytz.country_names[x],end = ' '))
if x in (pytz.country_timezones):
print(pytz.country_timezones[x])
else:
print("TimeZone Not Defined")
Subscribe to:
Posts (Atom)