Python 将弹性搜索时间戳属性转换为秒
如何将这个时间戳值转换为一个整数,表示自python中的纪元以来的秒数Python 将弹性搜索时间戳属性转换为秒,python,
elasticsearch,timestamp,Python,
elasticsearch,Timestamp,如何将这个时间戳值转换为一个整数,表示自python中的纪元以来的秒数 2016-08-06T06:07:36.349Z 这是我在弹性搜索查询中收到的时间戳值 我尝试过搜索,但时间戳格式与此不同,对任何其他方法都没有帮助。您可以使用python内置的datetime包及其strTime方法将字符串转换为datetime对象 from datetime import datetime datetime.strptime("2016-08-06T06:07:36.349Z","%Y-%m-%dT
2016-08-06T06:07:36.349Z
这是我在弹性搜索查询中收到的时间戳值
我尝试过搜索,但时间戳格式与此不同,对任何其他方法都没有帮助。您可以使用python内置的datetime包及其strTime方法将字符串转换为datetime对象
from datetime import datetime
datetime.strptime("2016-08-06T06:07:36.349Z","%Y-%m-%dT%H:%M:%S.%fZ")
在此之后,您应该得到可以通过
epoch = datetime.utcfromtimestamp(0)
你的最后几秒可以从这个方法得到
def unix_time_millis(datetime):
return (datetime - epoch).total_seconds() * 1000.0
因此,您的完整代码如下所示
from datetime import datetime
epoch = datetime.utcfromtimestamp(0)
def unix_time_millis(datetime):
return (datetime - epoch).total_seconds() * 1000.0
current_date = datetime.strptime("2016-08-06T06:07:36.349Z","%Y-%m-%dT%H:%M:%S.%fZ")
print unix_time_millis(current_date)
这个答案的灵感来源于这个答案,您可以使用python内置的datetime包及其strTime方法将字符串转换为datetime对象
from datetime import datetime
datetime.strptime("2016-08-06T06:07:36.349Z","%Y-%m-%dT%H:%M:%S.%fZ")
在此之后,您应该得到可以通过
epoch = datetime.utcfromtimestamp(0)
你的最后几秒可以从这个方法得到
def unix_time_millis(datetime):
return (datetime - epoch).total_seconds() * 1000.0
因此,您的完整代码如下所示
from datetime import datetime
epoch = datetime.utcfromtimestamp(0)
def unix_time_millis(datetime):
return (datetime - epoch).total_seconds() * 1000.0
current_date = datetime.strptime("2016-08-06T06:07:36.349Z","%Y-%m-%dT%H:%M:%S.%fZ")
print unix_time_millis(current_date)
这个答案的灵感来源于这个答案可能重复的感谢@Sumit的可能重复Kumar@sumit我们如何将时间戳转换为通用timestamp@ak3191我不明白你的问题,时间戳和一般时间戳是什么?我不确定这是否是ES版本差异,但v2.4.6上返回的日期-时间字符串没有尾随Z。此外,如果dt.microsecond==0,strdt将省略.000000后缀,而dt.strftimemmt+.%f将打印它。因此,取决于字符串最初是如何生成的,strtime可能会在零微秒内引发ValueError。ES返回的日期字符串是否有正式的ISO名称?此外,您的datetime本地阴影将显示导入的符号。谢谢@SumitKumar@sumit我们如何将时间戳转换为通用timestamp@ak3191我不明白你的问题,时间戳和一般时间戳是什么?我不确定这是否是ES版本差异,但v2.4.6上返回的日期-时间字符串没有尾随Z。此外,如果dt.microsecond==0,strdt将省略.000000后缀,而dt.strftimemmt+.%f将打印它。因此,取决于字符串最初是如何生成的,strtime可能会在零微秒内引发ValueError。ES返回的日期字符串是否有正式的ISO名称?此外,您的datetime本地将对导入的符号进行阴影处理。