Reputation: 2583
My date histogram query returns something like this:
"facets": {
"hist": {
"_type": "date_histogram",
"entries": [
{
"time": 1385856000000,
"count": 1884
},
{
"time": 1385942400000,
"count": 1902
},
How can I take that time value and get a date string: '2014-02-16'?
Here's what I have so far:
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)
I get back:
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
Any ideas? I just copied the getFiletime function from the internet. I don't think it's meant for what I'm doing, but I know it's on the right track. I tried putting the "format" specifier in my elastic search query but that doesn't return the timestamp as a string like the documentation states. Any help would be appreciated and thank you for reading!
Upvotes: 3
Views: 4661
Reputation: 160
A simple function:
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)
Upvotes: 2
Reputation: 502
These aren't 64bit timestamps, just regular (unix epoch) ones with millisecond resolution:
import datetime
datetime.datetime.utcfromtimestamp(1385942400000 / 1000.).strftime('%Y-%m-%d')
gives 2013-12-02
(thanks to @knutwalker's comment above)
Upvotes: 1
Reputation: 3397
I had the same challenge and created this method to solve the same problem.
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
It takes a datetime with or without timezone, and returns a string that is recognized by ElasticSearch (v1.1.1 for sure). I'm not sure if the padding is required or not, but it seemed prudent.
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'
Upvotes: 1