1

This program is used to find the current time of location of a countries
time and then get the time of a location of your choosing.I need to know how to get the time from other countries such as China or the ones in the code.

   #Eric's Amazing Trip around the world in 80 days time converter#
import time
print("Welcome to Eric's Amazing Trip around the world in 80 days time converter")
print("This program helps the user change the time on the watch as they travel")
cur=input("First enter the current country")
#Displays the country of your choosing time#
print("The current time in " + cur)
#Displays the current time in the country#
print("Current time is " + time.asctime())
print()

nex=input("Next the country your travelng to")
#This display the time of whichever country you choose#
if cur=="Italy" or "italy":
    print("The current time in " + nex + "is")
elif cur=="Egypt" or "Egypt":
    print("The current time in " + nex + "is")
elif cur=="Paris" or "Paris ":
    print("The current time in " + nex + "is")
elif cur=="China" or "china":
    print("The current time in " + nex + "is")
elif cur=="India" or "india":
    print("The current time in" + nex + "is")
elif cur=="Singapore" or "Singaspore":
    print("The current time in " + nex + "is")
Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
  • 2
    Incidentally, `if cur == "Italy" or "italy"` is equivalent to `if (cur == "Italy") or "italy"`, which always evaluates to True. You want `if cur in ("Italy", "italy"):` or `if cur == "Italy" or cur == "italy":` or `if cur.lower() == "italy":`. – Kevin Aug 18 '15 at 17:51
  • This might help: http://stackoverflow.com/questions/1301493/setting-timezone-in-python – Luis Aug 18 '15 at 17:59

2 Answers2

2

In general, there could be more than one timezone in a country e.g., there are 21 timezones in Russia:

>>> import pytz
>>> pprint(pytz.country_timezones['ru'])
['Europe/Kaliningrad',
 'Europe/Moscow',
 'Europe/Simferopol',
 'Europe/Volgograd',
 'Europe/Samara',
 'Asia/Yekaterinburg',
 'Asia/Omsk',
 'Asia/Novosibirsk',
 'Asia/Novokuznetsk',
 'Asia/Krasnoyarsk',
 'Asia/Irkutsk',
 'Asia/Chita',
 'Asia/Yakutsk',
 'Asia/Khandyga',
 'Asia/Vladivostok',
 'Asia/Sakhalin',
 'Asia/Ust-Nera',
 'Asia/Magadan',
 'Asia/Srednekolymsk',
 'Asia/Kamchatka',
 'Asia/Anadyr']

Different timezones may have different utc offsets.

Though all countries except China have a single timezone in your example:

>>> country_codes = {country: code for code, country in pytz.country_names.items()}
>>> {c: pytz.country_timezones[country_codes[c]]
...  for c in "Italy Egypt China India Singapore".split()}
{'China': ['Asia/Shanghai', 'Asia/Urumqi'],
 'Egypt': ['Africa/Cairo'],
 'India': ['Asia/Kolkata'],
 'Italy': ['Europe/Rome'],
 'Singapore': ['Asia/Singapore']}

Once you've chosen a specific timezone such as Asia/Shanghai, it is easy to find the current time in it:

#!/usr/bin/env python
from datetime import datetime
import pytz # $ pip install pytz

current_time = datetime.now(pytz.timezone('Asia/Shanghai'))
print(current_time.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
# -> 2015-08-19 19:03:22 CST+0800
jfs
  • 399,953
  • 195
  • 994
  • 1,670
-1
import datetime 
import pytz 

#pytz is already installed in your latest version of ide (vs code , pycharm )

#random initializes your pytz
random = datetime.datetime.now(tz=pytz.UTC)
print(random)

#actual is your own country's timezone , to find out your own country timezone do a google search
    
actual = random.astimezone(pytz.timezone('Asia/Colombo'))
print(actual)    
Niels Henkens
  • 2,553
  • 1
  • 12
  • 27