当地时区是什么? [英] What is the local timezone
问题描述
芝加哥设在CST的人如何看到该约会在上午10点进入美国东部标准时间的网站,而且开发人员如何谁在MST,使用python在datetime,时间和日历中设置参数?
此外,什么时区是本地时间?
第三方模块 pytz ,提供了一种在时区之间进行转换的简单方法。例如,
import datetime as dt
import pytz
utc = pytz.utc
western = pytz.timezone('US / Pacific')
newyork = pytz.timezone('America / New_York')
chicago = pytz.timezone('America / Chicago')
假设有人在纽约上午10点创建约会:
date = dt.datetime(2012,8,12,10)#naive datetime
print(date)
#2012-08-12 10:00 :00
#localize将天真数据时间转换为时区感知数据时间。
date_in_newyork = newyork.localize(date)#timezone-aware datetime
print(date_in_newyork)
#2012-08-12 10:00:00-04:00
您在西海岸的服务器应该存储这个datetime在UTC:
#astimezone将时区感知的数据时间转换为其他时区。
现在,当芝加哥的人想要知道约会的时间,服务器可以将UTC转换为芝加哥时间,纽约时间,或者任何时间:
date_in_utc = date_in_newyork.astimezone(utc)
print(date_in_utc)
#2012-08-12 14:00:00 + 00:00
date_in_chicago = date_in_utc.astimezone(芝加哥)
打印(date_in_chicago)
#2012-08-12 09:00:00-05:00
date_in_newyork2 = date_in_utc.astimezone(newyork)
打印(date_in_newyork2)
#2012-08-12 10:00:00-04 :00
Assuming the server (gae) is on the US west coast (PST), an appointment is in New York City (EST) at 10:00am and a person in Chicago (CST) is using his device to know the time of the appointment in NYC.
How can the the person in Chicago's device in CST see that the appointment is at 10am when she goes to the website that resides in EST, and does it matter how the developer, who was in MST, sets up parameters in datetime, time, and calendar using python?
Also, what timezone is "local time" here?
解决方案The third-party module, pytz, provides an easy way to convert between timezones. For example,
import datetime as dt import pytz utc = pytz.utc western = pytz.timezone('US/Pacific') newyork = pytz.timezone('America/New_York') chicago = pytz.timezone('America/Chicago')
Suppose someone creates an appointment at 10am in New York:
date = dt.datetime(2012, 8, 12, 10) # naive datetime print(date) # 2012-08-12 10:00:00 # localize converts naive datetimes to timezone-aware datetimes. date_in_newyork = newyork.localize(date) # timezone-aware datetime print(date_in_newyork) # 2012-08-12 10:00:00-04:00
Your server on the west coast should store this datetime in UTC:
# astimezone converts timezone-aware datetimes to other timezones. date_in_utc = date_in_newyork.astimezone(utc) print(date_in_utc) # 2012-08-12 14:00:00+00:00
Now when the person in Chicago wants to know what time the appointment is, the server can convert UTC to Chicago time, or New York time, or whatever:
date_in_chicago = date_in_utc.astimezone(chicago) print(date_in_chicago) # 2012-08-12 09:00:00-05:00 date_in_newyork2 = date_in_utc.astimezone(newyork) print(date_in_newyork2) # 2012-08-12 10:00:00-04:00
这篇关于当地时区是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!