简体   繁体   English

将 RFC 3339 纳秒时间转换为标准 Python 时间戳

[英]Convert an RFC 3339 nano time to a standard Python timestamp

Is there an easy way to convert an RFC 3339 nano time into a regular Python timestamp?有没有一种简单的方法可以将 RFC 3339 纳米时间转换为常规的 Python 时间戳?

For example, time = '2022-07-14T12:01:25.225089838+08:00', I found a way using datetime比如time = '2022-07-14T12:01:25.225089838+08:00',我找到了一种使用datetime的方法

from datetime import datetime

time = '2022-07-14T12:01:25.225089+08:00'
date = datetime.fromisoformat(time)  # good

time = '2022-07-14T12:01:25.225089838+08:00'
date = datetime.fromisoformat(time)  # error

It works with string like '2022-07-14T12:01:25.225089+08:00', but it doesn't work with the time above.它适用于像 '2022-07-14T12:01:25.225089+08:00' 这样的字符串,但不适用于上述时间。

from datetime.fromisoformat docs :来自datetime.fromisoformat 文档

Caution: This does not support parsing arbitrary ISO 8601 strings - it is only intended as the inverse operation of datetime.isoformat().注意:这不支持解析任意 ISO 8601 字符串 - 它仅用作 datetime.isoformat() 的逆操作。 A more full-featured ISO 8601 parser, dateutil.parser.isoparse is available in the third-party package dateutil.第三方包 dateutil 中提供了功能更全的 ISO 8601 解析器 dateutil.parser.isoparse。


dateutil 's isoparse will do the job: dateutil的 isoparse 将完成这项工作:

from dateutil.parser import isoparse

time = '2022-07-14T12:01:25.225089838+08:00'
date = isoparse(time) 

print(date)
# 2022-07-14 12:01:25.225089+08:00

print(repr(date))
# datetime.datetime(2022, 7, 14, 12, 1, 25, 225089, tzinfo=tzoffset(None, 28800))

Note: it doesn't round to microseconds, it just slices off the last 3 decimal places.注意:它不会四舍五入到微秒,它只是去掉最后 3 位小数。 So basically, if you're dealing with a standardized format like RFC 3339, you can do the slicing yourself like所以基本上,如果您正在处理像 RFC 3339 这样的标准化格式,您可以像自己一样进行切片

from datetime import datetime

time = '2022-07-14T12:01:25.225089838+08:00'
date = datetime.fromisoformat(time[:-9] + time[-6:])

print(date)
# 2022-07-14 12:01:25.225089+08:00

There are a few ways to do it.有几种方法可以做到这一点。 Depends on what is the input format and how you define an easy way.取决于输入格式是什么以及如何定义简单的方法。 There are actually many post asking similar issues you have.实际上有很多帖子询问您遇到的类似问题。 I'll post a few at the end for your reference if you are interested and please check next time before posting.如果您有兴趣,我会在最后发布一些供您参考,请下次发布前检查。

The main issue of datetime object is that it only holds 6 digit after second. datetime 对象的主要问题是它仅在秒后保存 6 位。 You will need a different data structure to save it if you want to preserve all of the digits.如果要保留所有数字,则需要不同的数据结构来保存它。

If you are ok with cutting off with 6 digit, FObersteiner's answer is perfect.如果您可以用 6 位数截断,FObersteiner 的答案是完美的。 Another methodology is vanilla datetime string parsing另一种方法是香草日期时间字符串解析

from datetime import datetime
date = '2022-07-14T12:01:25.225089838+08:00'.removesuffix('+08:00')
x = datetime.strptime( date[:-3], '%Y-%m-%dT%H:%M:%S.%f')

If you would like to preserve all the digits.如果您想保留所有数字。 You may want to create your own class extending from the datetime class or create some function for it.您可能想要创建自己的类,从 datetime 类扩展或为其创建一些函数。

  1. Convert an RFC 3339 time to a standard Python timestamp 将 RFC 3339 时间转换为标准 Python 时间戳

  2. Parsing datetime strings containing nanoseconds 解析包含纳秒的日期时间字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM