程序中python的时区

时间:2015-08-18 17:48:07

标签: python timezone

该程序用于查找国家/地区的当前时间 时间,然后得到你选择的位置的时间。我需要知道如何从中国等其他国家或代码中获取时间。

   #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")

2 个答案:

答案 0 :(得分:0)

一般来说,一个国家可能有多个时区,例如俄罗斯有21个时区:

.some_div table a, .some_div .table a {
    text-decoration: none;
}

不同的时区可能有不同的utc偏移。

虽然除了中国以外的所有国家/地区都有一个时区:

>>> 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']

一旦您选择了>>> 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']} 等特定时区,就可以轻松找到当前时间:

Asia/Shanghai

答案 1 :(得分:0)

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)