Python:如何在给定 TIMEZONE 的情况下获取 UTC 时间 [英] Python: How to get UTC time given TIMEZONE

查看:67
本文介绍了Python:如何在给定 TIMEZONE 的情况下获取 UTC 时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在美国,我想获取一段代码的 UTC 时间(不受夏令时的影响):

I am in the US and I want to get the UTC time (without the effect of Daylight Saving Time) for a piece of code:

localtime = strftime("%m%d%y%H%M", gmtime())

现在该代码让我有时间在英格兰格林威治.我知道我的时间偏移量为 -8 (GMT -8).如何获取特定时区的 UTC 时间?(使用 Python 库,不将小时转换为整数,- 8,然后将其转换回来)

Right now that code give me time in Greenwich, England. I know that I have a time offset of -8 (GMT -8). How can I get the UTC time to a specific timezone? (using Python library, not casting the hour to integer, - 8, and than convert it back)

谢谢.

推荐答案

试试 datetime 模块:

Try the datetime module:

datetime.datetime.utcnow() + datetime.timedelta(hours=-8)

顺便说一句,UTC 没有时区,它是通用协调时间,到处都一样.

BTW, UTC doesn't have timezones, it's Universal Co-ordinated Time, it's the same everywhere.

这篇关于Python:如何在给定 TIMEZONE 的情况下获取 UTC 时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆