Tom*_*Tom 284 python datetime utc localtime
如何将本地时间的日期时间字符串转换为UTC时间的字符串?
我确定我以前做过这个,但找不到它,所以希望能帮助我(以及其他人)在将来做到这一点.
澄清:例如,如果我2008-09-17 14:02:00
在我的本地时区(+10
)中,我想生成一个具有相同UTC
时间的字符串:2008-09-17 04:02:00
.
此外,请访问http://lucumr.pocoo.org/2011/7/15/eppur-si-muove/,请注意,一般情况下这不可能与DST和其他问题一样,因此从当地时间到UTC时间.
Joh*_*kin 252
首先,将字符串解析为一个天真的日期时间对象.这是datetime.datetime
没有附加时区信息的实例.有关datetime.strptime
解析日期字符串的信息,请参阅文档.
使用该pytz
模块,该模块附带完整的时区列表+ UTC.弄清楚当地时区是什么,从中构建时区对象,并操纵它并将其附加到天真的日期时间.
最后,使用datetime.astimezone()
方法将datetime转换为UTC.
源代码,使用当地时区"America/Los_Angeles",字符串"2001-2-3 10:11:12":
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)
Run Code Online (Sandbox Code Playgroud)
从那里,您可以strftime()
根据需要使用该方法格式化UTC日期时间:
utc_dt.strftime ("%Y-%m-%d %H:%M:%S")
Run Code Online (Sandbox Code Playgroud)
mon*_*kut 134
datetime模块的utcnow()函数可用于获取当前UTC时间.
>>> import datetime
>>> utc_datetime = datetime.datetime.utcnow()
>>> utc_datetime.strftime("%Y-%m-%d %H:%M:%S")
'2010-02-01 06:59:19'
Run Code Online (Sandbox Code Playgroud)
正如Tom上面提到的链接:http://lucumr.pocoo.org/2011/7/15/eppur-si-muove/ 说:
UTC是一个没有夏令时的时区,过去仍然没有配置更改的时区.
始终以UTC格式测量和存储时间.
如果您需要记录拍摄时间,请单独存储. 不要存储当地时间+时区信息!
注意 - 如果您的任何数据位于使用DST的区域,请使用pytz
并查看John Millikin的答案.
如果您想从给定的字符串中获取UTC时间,并且您有幸在世界上不使用DST的区域中,或者您的数据仅在未应用DST的情况下偏离UTC:
- >使用本地时间作为偏移值的基础:
>>> # Obtain the UTC Offset for the current system:
>>> UTC_OFFSET_TIMEDELTA = datetime.datetime.utcnow() - datetime.datetime.now()
>>> local_datetime = datetime.datetime.strptime("2008-09-17 14:04:00", "%Y-%m-%d %H:%M:%S")
>>> result_utc_datetime = local_datetime + UTC_OFFSET_TIMEDELTA
>>> result_utc_datetime.strftime("%Y-%m-%d %H:%M:%S")
'2008-09-17 04:04:00'
Run Code Online (Sandbox Code Playgroud)
- >或者,从已知的偏移量,使用datetime.timedelta():
>>> UTC_OFFSET = 10
>>> result_utc_datetime = local_datetime - datetime.timedelta(hours=UTC_OFFSET)
>>> result_utc_datetime.strftime("%Y-%m-%d %H:%M:%S")
'2008-09-17 04:04:00'
Run Code Online (Sandbox Code Playgroud)
如果您准备好接受时区转换,请阅读以下内容:
Tom*_*Tom 62
谢谢@rofly,从字符串到字符串的完整转换如下:
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"))))
Run Code Online (Sandbox Code Playgroud)
我的time
/ calendar
函数摘要:
time.strptime
string - > tuple(未应用时区,因此匹配字符串)
time.mktime
本地时间元组 - >自纪元以来的秒数(始终是当地时间)
time.gmtime
自纪元以来的秒数 - > UTC中的元组
和
calendar.timegm
UTC中的元组 - >自纪元以来的秒数
time.localtime
自纪元以来的秒数 - >当地时区的元组
aka*_*ola 36
以下是常见Python时间转换的摘要.
有些方法会丢弃几秒钟,并标有(s).ts = (d - epoch) / unit
可以使用显式公式(感谢jfs).
calendar.timegm(struct_time)
calendar.timegm(stz.localize(dt, is_dst=None).utctimetuple())
calendar.timegm(dt.utctimetuple())
calendar.timegm(dt.utctimetuple())
time.gmtime(t)
stz.localize(dt, is_dst=None).utctimetuple()
dt.utctimetuple()
dt.utctimetuple()
datetime.fromtimestamp(t, None)
datetime.datetime(struct_time[:6], tzinfo=UTC).astimezone(tz).replace(tzinfo=None)
dt.replace(tzinfo=UTC).astimezone(tz).replace(tzinfo=None)
dt.astimezone(tz).replace(tzinfo=None)
datetime.utcfromtimestamp(t)
datetime.datetime(*struct_time[:6])
stz.localize(dt, is_dst=None).astimezone(UTC).replace(tzinfo=None)
dt.astimezone(UTC).replace(tzinfo=None)
datetime.fromtimestamp(t, tz)
datetime.datetime(struct_time[:6], tzinfo=UTC).astimezone(tz)
stz.localize(dt, is_dst=None)
dt.replace(tzinfo=UTC)
资料来源:taaviburns.ca
Chu*_*ebs 25
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)
Run Code Online (Sandbox Code Playgroud)
资料来源:http://feihonghsu.blogspot.com/2008/02/converting-from-local-time-to-utc.html
bd808的示例用法:如果您的源是datetime.datetime
对象t
,请调用:
local_to_utc(t.timetuple())
Run Code Online (Sandbox Code Playgroud)
Yar*_*rin 19
我对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')
Run Code Online (Sandbox Code Playgroud)
(代码源自将UTC UTC日期时间字符串转换为本地日期时间的答案)
Pau*_*ius 18
pytz的另一个例子,但包括localize(),这节省了我的一天.
import pytz, datetime
utc = pytz.utc
fmt = '%Y-%m-%d %H:%M:%S'
amsterdam = pytz.timezone('Europe/Amsterdam')
dt = datetime.datetime.strptime("2012-04-06 10:00:00", fmt)
am_dt = amsterdam.localize(dt)
print am_dt.astimezone(utc).strftime(fmt)
'2012-04-06 08:00:00'
Run Code Online (Sandbox Code Playgroud)
小智 12
我用python-dateutil取得了最大的成功:
from dateutil import tz
def datetime_to_utc(date):
"""Returns date in UTC w/o tzinfo"""
return date.astimezone(tz.gettz('UTC')).replace(tzinfo=None) if date.tzinfo else date
Run Code Online (Sandbox Code Playgroud)
小智 7
import time
import datetime
def Local2UTC(LocalTime):
EpochSecond = time.mktime(LocalTime.timetuple())
utcTime = datetime.datetime.utcfromtimestamp(EpochSecond)
return utcTime
>>> LocalTime = datetime.datetime.now()
>>> UTCTime = Local2UTC(LocalTime)
>>> LocalTime.ctime()
'Thu Feb 3 22:33:46 2011'
>>> UTCTime.ctime()
'Fri Feb 4 05:33:46 2011'
Run Code Online (Sandbox Code Playgroud)
这是Python3.9中本机zoneinfo模块的示例:
from datetime import datetime
from zoneinfo import ZoneInfo
# Get timezone we're trying to convert from
local_tz = ZoneInfo("America/New_York")
# UTC timezone
utc_tz = ZoneInfo("UTC")
dt = datetime.strptime("2021-09-20 17:20:00","%Y-%m-%d %H:%M:%S")
dt = dt.replace(tzinfo=local_tz)
dt_utc = dt.astimezone(utc_tz)
print(dt.strftime("%Y-%m-%d %H:%M:%S"))
print(dt_utc.strftime("%Y-%m-%d %H:%M:%S"))
Run Code Online (Sandbox Code Playgroud)
dt.astimezone()
在您要转换的时区不反映系统本地时区的情况下,这可能比仅使用更可取。不必依赖外部库也很好。
注意:这可能不适用于 Windows 系统,因为zoneinfo 依赖于可能不存在的IANA 数据库。可以安装 tzdata 包作为解决方法。它是第一方包,但不在标准库中。
小智 6
我是这样做的:
>>> utc_delta = datetime.utcnow()-datetime.now()
>>> utc_time = datetime(2008, 9, 17, 14, 2, 0) + utc_delta
>>> print(utc_time)
2008-09-17 19:01:59.999996
Run Code Online (Sandbox Code Playgroud)
如果你想花哨,你可以把它变成一个函子:
class to_utc():
utc_delta = datetime.utcnow() - datetime.now()
def __call__(cls, t):
return t + cls.utc_delta
Run Code Online (Sandbox Code Playgroud)
结果:
>>> utc_converter = to_utc()
>>> print(utc_converter(datetime(2008, 9, 17, 14, 2, 0)))
2008-09-17 19:01:59.999996
Run Code Online (Sandbox Code Playgroud)
此线程似乎缺少自 Python 3.6 起可用的选项:datetime.astimezone(tz=None)
can be used to get an aware datetime object representation of local time (docs)。然后可以轻松地将其转换为 UTC。
from datetime import datetime, timezone
s = "2008-09-17 14:02:00"
# to datetime object:
dt = datetime.fromisoformat(s) # Python 3.7
# I'm on time zone Europe/Berlin; CEST/UTC+2 during summer 2008
dt = dt.astimezone()
print(dt)
# 2008-09-17 14:02:00+02:00
# ...and to UTC:
dtutc = dt.astimezone(timezone.utc)
print(dtutc)
# 2008-09-17 12:02:00+00:00
Run Code Online (Sandbox Code Playgroud)
但是有一个警告,请参阅astimezone(None) 提供了知道日期时间,不知道 DST。
如果您更喜欢datetime.datetime:
dt = datetime.strptime("2008-09-17 14:04:00","%Y-%m-%d %H:%M:%S")
utc_struct_time = time.gmtime(time.mktime(dt.timetuple()))
utc_dt = datetime.fromtimestamp(time.mktime(utc_struct_time))
print dt.strftime("%Y-%m-%d %H:%M:%S")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
377612 次 |
最近记录: |