将Python DateTime字符串转换为整数毫秒 [英] Conversion of Python DateTime string into integer milliseconds
问题描述
我想将UTC TimeDate标记字符串转换为毫秒数的整数值(可能需要为64位数量),以便在存储在mySQL数据库列中占用较少的空间。这个UTC字符串是从另一个库生成的,我将它存储为一种每个用户的GUID。
可以将datetime或dateutil转换为单个整数值像从时代开始的毫秒)?或者我需要自己做这件事?
使用这种方法进行分析:
myDateTime = dateutil.parser.parse(2015-06-27T02:10:05.653000Z)
print(Parsed datetime String is {0},ordinal value is {1}format(myDateTime ,myDateTime.toordinal()))
提供输出:
解析的datetime字符串是2015-06-27 02:10:05.652999 + 00:00,序数值是735776
/ pre>
...只给出日期的序数值。此外,如果我有一个整数为653毫秒的时间,那么我希望解析对象知道它有653毫秒,而不是652999。
解决方案[在评论中按照建议编辑]
使用 Ben Alpert的回答如何在Python中将datetime对象从epoch(unix time)转换为毫秒?我们可以执行以下操作:
code> from datetime import datetime
def unix_time(dt):
epoch = datetime.utcfromtimestamp(0)
delta = dt - epoch
return delta.total_seconds()
def unix_time_millis(dt):
return int(unix_time(dt)* 1000)
a = datetime.strptime(2015-06-27T02:10: 05.653000Z,%Y-%m-%dT%H:%M:%S.%fZ)
unix_time_millis(a)
返回:
1435371005653
相当于:星期六,2015年6月27日02:10:05 GMT (如预期)
我们还可以使用datetime的
.strftime('%s')
获取unix时间,甚至使用以下()毫秒,但不建议从十进制输入十进制
int(Decimal(datetime.strptime 2015-06-27T02:10:05.653000Z,%Y-%m-%dT%H:%M:%S.%fZ)。strftime('%s。%f'))* 1000)
返回:
code> 1435396205653
相当于:星期六,2015年6月27日09:10: 05 GMT (在我的mac在圣地亚哥; 注意:这是我们可能预期的7小时。)
错误的原因由JF Sebastian在上面的链接和这个答案关于
.strftime('%s')
行为。 JF Sebastian指出,它不支持,它不可移植,它可能会静默地为一个感知的datetime对象产生错误的结果,如果输入是UTC(如问题),但是本地时区不是UTC / p>I would like to convert a UTC TimeDate stamp string into an integer value of milliseconds (might need to be a 64-bit quantity), so that it takes up less space when stored in a mySQL database column. This UTC string is being generated from another library, and I store it as a kind of per-user GUID.
Can datetime or dateutil convert this into a single integer value (like "milliseconds since epoch")? Or do I need to do that myself?
Parsing using this approach:
myDateTime = dateutil.parser.parse("2015-06-27T02:10:05.653000Z") print("Parsed datetime String is {0}, ordinal value is {1}".format(myDateTime, myDateTime.toordinal()))
Gives the output:
Parsed datetime String is 2015-06-27 02:10:05.652999+00:00, ordinal value is 735776
…which only gives an ordinal value for the date. Further, if I have a time with an integer 653 milliseconds, then I want that parsed object to know it has 653 milliseconds, not 652999.
解决方案[Edited following suggestion in the comments]
Using Ben Alpert's answer to How can I convert a datetime object to milliseconds since epoch (unix time) in Python we can do the following:
from datetime import datetime def unix_time(dt): epoch = datetime.utcfromtimestamp(0) delta = dt - epoch return delta.total_seconds() def unix_time_millis(dt): return int(unix_time(dt) * 1000) a = datetime.strptime("2015-06-27T02:10:05.653000Z", "%Y-%m-%dT%H:%M:%S.%fZ") unix_time_millis(a)
returns:
1435371005653
which is equivalent to: Sat, 27 Jun 2015 02:10:05 GMT (as expected)
We can also use datetime's
.strftime('%s')
to get unix time, even milliseconds using the following (but this is not advised):from decimal import Decimal int(Decimal(datetime.strptime("2015-06-27T02:10:05.653000Z", "%Y-%m-%dT%H:%M:%S.%fZ").strftime('%s.%f'))*1000)
returns:
1435396205653
equivalent to: Sat, 27 Jun 2015 09:10:05 GMT (on my mac in San Diego; Note: this is 7 hours off what we may have expected).
The cause of the error is described by J.F. Sebastian in the comments of the link above and in this answer regarding
.strftime('%s')
behavior. J.F. Sebastian points out that "it is not supported, it is not portable, it may silently produce a wrong result for an aware datetime object, it fails if input is in UTC (as in the question) but local timezone is not UTC"这篇关于将Python DateTime字符串转换为整数毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!