我的日期直方图查询返回如下内容:
"facets": {
"hist": {
"_type": "date_histogram",
"entries": [
{
"time": 1385856000000,
"count": 1884
},
{
"time": 1385942400000,
"count": 1902
},
如何获取该时间值并获取日期字符串:“2014-02-16”?
这是我目前所拥有的:
def getFiletime(dt):
microseconds = int(dt) / 10
seconds, microseconds = divmod(microseconds, 1000000)
days, seconds = divmod(seconds, 86400)
return datetime.datetime(1601, 1, 1) + datetime.timedelta(days, seconds, microseconds)
我回来了:
1601-01-02 14:37:23.520000
1601-01-02 14:37:32.160000
1601-01-02 14:37:40.800000
1601-01-02 14:37:49.440000
1601-01-02 14:37:58.080000
1601-01-02 14:38:06.720000
1601-01-02 14:38:15.360000
有什么想法吗?我刚刚从互联网上复制了 getFiletime 函数。我不认为它适合我正在做的事情,但我知道它在正确的轨道上。我尝试将“格式”说明符放在我的 Elasticsearch 查询中,但它不会像文档所述那样将时间戳作为字符串返回。如有任何帮助,我们将不胜感激,感谢您的阅读!
最佳答案
一个简单的函数:
from datetime import datetime
import arrow
def timestamp(timestamp):
date = datetime.fromtimestamp(timestamp / 1_000_000.0)
return formattdatetime(date)
def formattdatetime(date):
date = arrow.get(date, "YYYY-MM-DD")
return arrow.get(date)
关于python - 如何将从 elasticsearch 返回的 64 位时间戳转换为 python 中的日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21815339/