What is the local timezone

泪湿孤枕 提交于 2019-12-13 01:15:53

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/11925664/what-is-the-local-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!