[英]How do I convert a LabVIEW decimal date into a string datetime format using Python?
I need to convert a decimal timestamp in a JSON file generated using LabVIEW into a string datetime so that I can POST it to an API I'm using. 我需要将使用LabVIEW生成的JSON文件中的十进制时间戳转换为字符串日期时间,以便我可以将其POST到我正在使用的API中。 For instance, one such decimal timestamp is 3640111724.4817362; 例如,一个这样的十进制时间戳是3640111724.4817362; how can I do this? 我怎样才能做到这一点?
EDIT: This article from NI describes how they format their timestamps. 编辑:NI的这篇文章描述了他们如何格式化他们的时间戳。 They're starting from a nonstandard epoch (01/01/1904 00:00:00.00 UTC), so in other words, Python's interpretation is 66 years ahead. 它们是从非标准时代(01/01/1904 00:00:00.00 UTC)开始的,换句话说,Python的解释是提前66年。
just use datetime.fromtimestamp
from datetime and format It with strftime
as you want: 只需使用datetime.fromtimestamp
的datetime.fromtimestamp,并根据需要使用strftime
格式化它:
EDIT: subtracting 66 years to match with datetime timestamp pattern 编辑:减去66年以匹配日期时间戳模式
from dateutil.relativedelta import relativedelta
from datetime import datetime
timestamp = 3640111724.4817362
date = datetime.fromtimestamp(timestamp)
date = date - relativedelta(years=66)
print(date.strftime("%m/%d/%Y, %H:%M:%S"))
Output: 输出:
05/07/2019, 22:08:44
The number of seconds between 1904-01-01 00:00:00 UTC and 1970-01-01 00:00:00 UTC is 2082844800, so you just need to adjust your LabView timestamp before creating your Python datetime object. UTC时间为1904-01-01 00:00:00 UTC和1970-01-01 00:00:00 UTC之间的秒数为2082844800,因此您只需在创建Python日期时间对象之前调整LabView时间戳。
from datetime import datetime
timestamp = 3640111724.4817362
dt = datetime.fromtimestamp(timestamp - 2082844800)
print(dt)
# 2019-05-07 22:08:44.481736
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.