我尝试索引包含日期的字段。
如何从不同的时区索引日期?
我将弹性搜索字段设置为:'requested_dt':{“type”:“date”,“format”:“date_time_no_millis”}'local_dt':{“type”:“date”,“format”:“date_time_no_millis “}
我试图索引这些值(local_dt):( requested_dt是法国当前时间)
IT 2016-10-27T23:46:17Z
GB 2016-10-27T22:46:19Z
我没有通过Kibana得到预期的结果:
[local_dt]
IT October 28th 2016, 01:46:17.000
GB October 28th 2016, 00:46:19.000
[requested_dt]
IT October 27th 2016, 23:46:17.000
GB October 27th 2016, 23:46:19.000
所以,对于requested_dt,我得到了我的期望。
对于local_dt,我没有得到我想要的东西。
我试图用UTC偏移替换Z值,但我无法获得正确的输出。
是否有人能够向我解释如何为我想要的每个时区获得正确的输出?
在我阅读(以及我自己的经验)时,Kibana会将@timestamp
编入索引,假设它们是以UTC格式出现的,而且只有像2018-04-23T10:45:13.899Z
这样的格式。请注意,我们只有毫秒,T
作为分隔符,Z
表示UTC。
https://discuss.elastic.co/t/kibana-timezone/29270/5
因此,如果您有本地datetime
对象,请尝试转换为UTC时间,并将其格式化为如上所示。
now()
的情况下,使用timezone.now()
的django.utils.timezone
;或者,没有django,你有datetime.datetime.utcnow()
datetime()
对象,你可以这样做:这个:
import pytz, datetime
local = pytz.timezone ("Europe/Paris")
local_dt = local.localize(your_datetime_object, is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc)
(感谢this answer)
当你有对象时,格式如下:
timeStr = datetime.strftime(your_utc_time_object, "%Y-%m-%dT%H:%M:%S.%f") # %f: microseconds, 1/10^6 seconds.
timeStr = timeStr[:-3] # delete the trailing 3 digits, convert to milliseconds
toInsert["@timestamp"] = nowStr + "Z" # add 'Z' at last