如何将本地时间字符串转换为UTC?如何转换日期时间?本地时间字符串转到以UTC时间为单位的字符串?我肯定我以前做过这个,但是找不到,所以希望将来能帮到我(和其他人)。澄清例如,如果我有2008-09-17 14:02:00在我的本地时区(+10),我希望生成一个具有等效字符串的字符串。UTC时间:2008-09-17 04:02:00.另外,来自http:/lucumr.poco.org/2011/7/15/eppur-si-muLove/注意,一般来说,这是不可能的,因为DST和其他问题没有唯一的从本地时间到UTC时间的转换。
3 回答

繁花不似锦
TA贡献1851条经验 获得超4个赞
datetime.datetime
datetime.strptime
pytz
datetime.astimezone()
import pytz, datetime local = pytz.timezone ("America/Los_Angeles")naive = datetime.datetime.strptime ("2001-2-3 10:11:12", "%Y-%m-%d %H:%M:%S")local_dt = local.localize(naive, is_dst=None)utc_dt = local_dt.astimezone(pytz.utc)
strftime()
utc_dt.strftime ("%Y-%m-%d %H:%M:%S")

侃侃尔雅
TA贡献1801条经验 获得超16个赞
time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(time.mktime(time.strptime("2008-09-17 14:04:00", "%Y-%m-%d %H:%M:%S"))))
time
/calendar
time.strptime
time.mktime
time.gmtime
calendar.timegm
time.localtime
添加回答
举报
0/150
提交
取消