Python将日期和时间毫秒转换成麻烦(Python trouble converting milliseconds to datetime and back)

编程入门 行业动态 更新时间:2024-10-26 04:27:56
Python将日期和时间毫秒转换成麻烦(Python trouble converting milliseconds to datetime and back)

所以我有两个函数可以将python datetime.datetime()对象转换为毫秒数据。 我无法弄清楚哪里出错了。 以下是我正在处理的内容:

>>> import datetime >>> def mil_to_date(mil): """date items from REST services are reported in milliseconds, this function will convert milliseconds to datetime objects Required: mil -- time in milliseconds """ if mil == None: return None elif mil < 0: return datetime.datetime.utcfromtimestamp(0) + datetime.timedelta(seconds=(mil/1000)) else: return datetime.datetime.fromtimestamp(mil / 1000) >>> def date_to_mil(date): """converts datetime.datetime() object to milliseconds date -- datetime.datetime() object""" if isinstance(date, datetime.datetime): epoch = datetime.datetime.utcfromtimestamp(0) return long((date - epoch).total_seconds() * 1000.0) >>> mil = 1394462888000 >>> date = mil_to_date(mil) >>> date datetime.datetime(2014, 3, 10, 9, 48, 8) #this is correct >>> d2m = date_to_mil(date) >>> d2m 1394444888000L >>> mil 1394462888000L >>> date2 = mil_to_date(d2m) >>> date2 datetime.datetime(2014, 3, 10, 4, 48, 8) #why did I lose 5 hours??

出于某种原因,我失去了5个小时。 我忽略了一些明显的东西? 或者我的一个或两个功能有问题吗?

So I have two functions for converting python datetime.datetime() objects to and from milliseconds. I cannot figure out where this is going wrong. Here's what I'm working with:

>>> import datetime >>> def mil_to_date(mil): """date items from REST services are reported in milliseconds, this function will convert milliseconds to datetime objects Required: mil -- time in milliseconds """ if mil == None: return None elif mil < 0: return datetime.datetime.utcfromtimestamp(0) + datetime.timedelta(seconds=(mil/1000)) else: return datetime.datetime.fromtimestamp(mil / 1000) >>> def date_to_mil(date): """converts datetime.datetime() object to milliseconds date -- datetime.datetime() object""" if isinstance(date, datetime.datetime): epoch = datetime.datetime.utcfromtimestamp(0) return long((date - epoch).total_seconds() * 1000.0) >>> mil = 1394462888000 >>> date = mil_to_date(mil) >>> date datetime.datetime(2014, 3, 10, 9, 48, 8) #this is correct >>> d2m = date_to_mil(date) >>> d2m 1394444888000L >>> mil 1394462888000L >>> date2 = mil_to_date(d2m) >>> date2 datetime.datetime(2014, 3, 10, 4, 48, 8) #why did I lose 5 hours??

For some reason, I am losing 5 hours. Am I overlooking something obvious? Or is there a problem with one or both of my functions?

最满意答案

原因是date_to_mil与UTC date_to_mil工作,而mil_to_date不工作。 您应该使用utcfromtimestamp替换fromtimestamp 。

进一步解释:

在你的代码中, epoch是UTC epoch的日期(但对象没有任何时区)。 但date是本地时间,因为fromtimestamp返回当地时间:

如果可选参数tz为None或未指定,则时间戳将转换为平台的本地日期和时间,并且返回的datetime对象为初始

所以你从本地日期时间减去UTC时代,你会得到一个延迟,这是你本地延迟到UTC。

The reason for this is that date_to_mil works with UTC and mil_to_date doesn't. You should replace utcfromtimestamp with fromtimestamp.

Further explanation:

In your code, epoch is the date of the epoch in UTC (but the object is without any time-zone). But date is local since fromtimestamp returns a local time:

If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive

So you subtract the UTC epoch from the local datetime and you get a delay which is your local delay to UTC.

更多推荐

本文发布于:2023-08-04 10:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换成   麻烦   日期   时间   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!