Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将elasticsearch返回的64位时间戳转换为python中的日期时间?_Python_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Python,elasticsearch" /> elasticsearch,Python,elasticsearch" />

如何将elasticsearch返回的64位时间戳转换为python中的日期时间?

如何将elasticsearch返回的64位时间戳转换为python中的日期时间?,python,elasticsearch,Python,elasticsearch,My date histogram查询返回如下内容: "facets": { "hist": { "_type": "date_histogram", "entries": [ { "time": 1385856000000, "count": 1884 }, { "time": 1385942400000, "count": 1902

My date histogram查询返回如下内容:

"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

有什么想法吗?我刚刚从internet复制了getFiletime函数。我不认为这意味着我在做什么,但我知道这是在正确的轨道上。我尝试将“格式”说明符放入弹性搜索查询中,但这不会像文档状态那样以字符串形式返回时间戳。任何帮助都将不胜感激,感谢您的阅读

我也遇到了同样的挑战,创建了这个方法来解决同样的问题

def to_es_date(d):
    s = d.strftime('%Y-%m-%dT%H:%M:%S.')
    s += '%03d' % int(round(d.microsecond / 1000.0))
    s += d.strftime('%z')
    return s
它采用带或不带时区的datetime,并返回一个由ElasticSearch识别的字符串(当然是v1.1.1)。我不确定是否需要填充,但这似乎很谨慎

In [5]: from datetime import datetime

In [6]: import pytz

In [7]: %paste
def to_es_date(d):
        s = d.strftime('%Y-%m-%dT%H:%M:%S.')
        s += '%03d' % int(round(d.microsecond / 1000.0))
        s += d.strftime('%z')
        return s

In [8]: to_es_date(datetime.now())
Out[8]: '2014-04-30T03:56:02.893'

In [9]: to_es_date(datetime.now(tz=pytz.utc))
Out[9]: '2014-04-30T03:56:20.134+0000'
这些不是64位时间戳,只是具有毫秒分辨率的常规(unix历元)时间戳:

发布日期:2013-12-02

(感谢@knutwalker上面的评论)

一个简单的函数:

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)

我只是尝试了一下,没有任何帮助,但还是谢谢你。这些不是64位时间戳,只是具有毫秒分辨率的常规(unix历元)时间戳:
datetime.utcfromtimestamp(138594240000/1000.0)。strftime(“%Y-%m-%d”)
给出了
2013-12-02
谢谢!这很有效。如果你把它作为一个答案,我会接受的。
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)