Tôi có yêu cầu chuyển đổi ngày từ dấu thời gian cục bộ sang UTC rồi quay lại dấu thời gian cục bộ.Vấn đề với python/pytz Chuyển đổi từ múi giờ địa phương sang UTC sau đó quay lại
Thật kỳ lạ, khi chuyển đổi trở lại địa phương từ con trăn UTC quyết định nó là PDT thay vì PST gốc nên ngày chuyển đổi bài đăng đã đạt được một giờ. Ai đó có thể giải thích cho tôi những gì đang xảy ra hoặc những gì tôi đang làm sai?
from datetime import datetime
from pytz import timezone
import pytz
DATE_FORMAT = '%Y-%m-%d %H:%M:%S %Z%z'
def print_formatted(dt):
formatted_date = dt.strftime(DATE_FORMAT)
print "%s :: %s" % (dt.tzinfo, formatted_date)
#convert the strings to date/time
date = datetime.now()
print_formatted(date)
#get the user's timezone from the pofile table
users_timezone = timezone("US/Pacific")
#set the parsed date's timezone
date = date.replace(tzinfo=users_timezone)
date = date.astimezone(users_timezone)
print_formatted(date)
#Create a UTC timezone
utc_timezone = timezone('UTC')
date = date.astimezone(utc_timezone)
print_formatted(date)
#Convert it back to the user's local timezone
date = date.astimezone(users_timezone)
print_formatted(date)
Và đây là kết quả:
None :: 2011-09-18 18:24:23
US/Pacific :: 2011-09-18 18:24:23 PST-0800
UTC :: 2011-09-19 02:24:23 UTC+0000
US/Pacific :: 2011-09-18 19:24:23 PDT-0700
Cảm ơn đã sửa nó. – user578888