如何将本地时间的datetime字符串转换为UTC时间的字符串?

我确信我以前做过这个,但找不到它,所以希望将来能帮助我(和其他人)做到这一点。

澄清:例如,如果我的本地时区(+10)是2008-09-17 14:02:00,我希望生成一个具有等效UTC时间的字符串:2008-09-17 04:02:00。

此外,从http://lucumr.pocoo.org/2011/7/15/eppur-si-muove/请注意,一般来说,这是不可能的,因为DST和其他问题没有从本地时间到UTC时间的唯一转换。


当前回答

下面是一些常见的Python时间转换的总结。

有些方法以秒为单位,用(s)标记。可以使用显式公式,如ts = (d - epoch) / unit代替(感谢jfs)。

struct_time (UTC) → POSIX (s):calendar.timegm(struct_time) Naïve datetime (local) → POSIX (s):calendar.timegm(stz.localize(dt, is_dst=None).utctimetuple())(exception during DST transitions, see comment from jfs) Naïve datetime (UTC) → POSIX (s):calendar.timegm(dt.utctimetuple()) Aware datetime → POSIX (s):calendar.timegm(dt.utctimetuple()) POSIX → struct_time (UTC, s):time.gmtime(t)(see comment from jfs) Naïve datetime (local) → struct_time (UTC, s):stz.localize(dt, is_dst=None).utctimetuple()(exception during DST transitions, see comment from jfs) Naïve datetime (UTC) → struct_time (UTC, s):dt.utctimetuple() Aware datetime → struct_time (UTC, s):dt.utctimetuple() POSIX → Naïve datetime (local):datetime.fromtimestamp(t, None)(may fail in certain conditions, see comment from jfs below) struct_time (UTC) → Naïve datetime (local, s):datetime.datetime(struct_time[:6], tzinfo=UTC).astimezone(tz).replace(tzinfo=None)(can't represent leap seconds, see comment from jfs) Naïve datetime (UTC) → Naïve datetime (local):dt.replace(tzinfo=UTC).astimezone(tz).replace(tzinfo=None) Aware datetime → Naïve datetime (local):dt.astimezone(tz).replace(tzinfo=None) POSIX → Naïve datetime (UTC):datetime.utcfromtimestamp(t) struct_time (UTC) → Naïve datetime (UTC, s):datetime.datetime(*struct_time[:6])(can't represent leap seconds, see comment from jfs) Naïve datetime (local) → Naïve datetime (UTC):stz.localize(dt, is_dst=None).astimezone(UTC).replace(tzinfo=None)(exception during DST transitions, see comment from jfs) Aware datetime → Naïve datetime (UTC):dt.astimezone(UTC).replace(tzinfo=None) POSIX → Aware datetime:datetime.fromtimestamp(t, tz)(may fail for non-pytz timezones) struct_time (UTC) → Aware datetime (s):datetime.datetime(struct_time[:6], tzinfo=UTC).astimezone(tz)(can't represent leap seconds, see comment from jfs) Naïve datetime (local) → Aware datetime:stz.localize(dt, is_dst=None)(exception during DST transitions, see comment from jfs) Naïve datetime (UTC) → Aware datetime:dt.replace(tzinfo=UTC)

来源:taaviburns.ca

其他回答

def local_to_utc(t):
    secs = time.mktime(t)
    return time.gmtime(secs)

def utc_to_local(t):
    secs = calendar.timegm(t)
    return time.localtime(secs)

来源:http://feihonghsu.blogspot.com/2008/02/converting-from-local-time-to-utc.html

来自bd808的示例用法:如果您的源是一个datetime。Datetime对象t,调用as:

local_to_utc(t.timetuple())

我在dateutil(在SO网站上被广泛推荐用于其他相关问题)方面运气不错:

from datetime import *
from dateutil import *
from dateutil.tz import *

# METHOD 1: Hardcode zones:
utc_zone = tz.gettz('UTC')
local_zone = tz.gettz('America/Chicago')
# METHOD 2: Auto-detect zones:
utc_zone = tz.tzutc()
local_zone = tz.tzlocal()

# Convert time string to datetime
local_time = datetime.strptime("2008-09-17 14:02:00", '%Y-%m-%d %H:%M:%S')

# Tell the datetime object that it's in local time zone since 
# datetime objects are 'naive' by default
local_time = local_time.replace(tzinfo=local_zone)
# Convert time to UTC
utc_time = local_time.astimezone(utc_zone)
# Generate UTC time string
utc_string = utc_time.strftime('%Y-%m-%d %H:%M:%S')

(代码是从这个答案派生的,将UTC日期时间字符串转换为本地日期时间)

如果你已经有了一个datetime对象my_dt,你可以用以下方法将其更改为UTC:

datetime.datetime.utcfromtimestamp(my_dt.timestamp())

我在这里找到了另一个问题的最佳答案。它只使用python内置库,不需要您输入本地时区(在我的情况下是一个要求)

import time
import calendar

local_time = time.strptime("2018-12-13T09:32:00.000", "%Y-%m-%dT%H:%M:%S.%f")
local_seconds = time.mktime(local_time)
utc_time = time.gmtime(local_seconds)

我在这里重新发布答案,因为这个问题在谷歌中弹出,而不是根据搜索关键字链接的问题。

使用http://crsmithdev.com/arrow/

arrowObj = arrow.Arrow.strptime('2017-02-20 10:00:00', '%Y-%m-%d %H:%M:%S' , 'US/Eastern')

arrowObj.to('UTC') or arrowObj.to('local') 

这个库让生活变得简单:)