I'm importing and converting data from a textfile into elastic via the PHP API which is provided by elastic.
Now i'm encountering that the data is 2 hours off (depending on summertime settings) in kibana, not when i'm selected the data native in my web application.
Now the question is how should i import the data so kibana is showing the data correctly?
I know elastic is interpreting datetime as UTC but is this the case with all 3 datetime fields or only the timestamp field?
Do i have to convert the datetimestart to UTC or is this done automatically?
Do i have to add the Timezone and when how should this be done?
A workaround is to set kibana to show UTC and set the "now-range" to 2 hours ahead - but nevertheless now is incorrect and neither we nor our customers like this approach!
All date fields in elasticsearch are in UTC regardless of the name you are using.
Elasticsearch expects a date time in UTC, if your date time string has time zone information it will convert it to UTC before storing, if your date time string does not have time zone information it will assume that the time is already in UTC.
You need to convert to UTC or add the timezone information to your date string.
when elk saves time, it converts to UTC and kibana will convert it back to your localtimezone and hence it shows up at correct place.
for example 2022-07-14 11:21:00 -> convert to UTC is +5 hour
now kibana will convert to -5 and hence it will show at same place.
now problem is if you query this data via sql query it will shows up +5 hour = 16:21:00 and it will confuse the hell out of you. (atlest it did to me and all my user and I was tired explaining to everyone)
hence this is what I did.
I save two date-time field. One with auto convert to UTC and let kibana take care all.
second I added timezone=UTC and user can use that if they are pulling data via sql.
this way it still shows up at same timestamp.
I hope this make sense. I still get confuse many time.
Thank you so much for your post, this is exactly what i was thinking of - i've an application which selects the dates now in UTC which confuses the user as it confuses me ... sooo ... i will do what you did - i duplicate all datetimefields and whatever application needs what i'm prepared, thanks for this tipp!! Since i'm importing data with custom API and more data will be imported from other sources so it's always good to know there is at least "one timestamp to fit them all" ya ... thank you again!
So - i'm confused again ... when i'm not using "UTC" in the Advanced Settings of Stack Management->Kibana i get the UTC Timestamp as the original Time and the other Timestamp with +2 hours ???
here is what I do. I do not convert timestamp I just add timezone to it. and I do this via logstash and python
What it does is just add timezone. what it does is keep same time and add zone. and hence elasticsearch do not do any conversion and saves as is
sql query will use this "timestamp_obj_UTC"
status_achieved_timezone= pytz.timezone('UTC').localize(timestamp)
Here it has timezone=cst and hence elasticserach will change time to UTC and saves. and when retrive it changes back to CST and display (kibana will use this "timestamp_obj_CST"
status_achieved=pytz.timezone('America/Chicago').localize(timestamp)
below. event was actually happen at 7:05:03.000
This is from discover tab on kibana.
But when I do setup visualization. I use status_achieved and it puts this event at 7:05 (converts time to America/Chicago, -5) because in Elastic it knows that this is UTC time and it has to convert to browser time.
But if I use status_achieved_timezone on visualization then it will shows up on wrong place because it will convert that to browser timezone. and it will show up at 2:05:03.00 (-5)
Trust me it is confusing. as I get confuse by typing this. LOL
Do not covert time. just add timezone
logstash
date { match => ["status_achieved_timezone", "dd-MMM-yy HH:mm:ss", "ISO8601"]
timezone => "Etc/UTC"
target => "status_achieved_timezone"
}
maybe i've missed something since i can change the timezone of carbon in this array-based structure ... adding a timezone before ingest as an additional parameter seems to be the way to go if i understand you right so the optimal way of dealing with datetimes would be (summed up): adding timezone to a datetime so Elastic will convert it to UTC, when receiving the datetime (from whereever) it will converte the datetime to the correct format and datetime ...right? the thing is - we've got messurementdata where everybody is concerned about the exact date and espacially time when the test started and finished ... until now this was no problem since i used my own application but kibana is the other side of the coin ... thank you very much for your help, i relly appreciate this a lot!! kind regards andy
I did the testing and i read your post over and over again - i think it's not that different hopefulle from what i'm doing although i don't know what this really is ... let me explain.
i'm exactly telling the datetime object what it is
which lead to the problem that i could not assign a timezone to this datetime (from what the error parser told me).
i removed the format and just let the "date" as a type
'datetimestart' => [
'type' => 'date',
]
now i took all the advices and recreated all datetimes with the timezone AND assigned my datetime field including the timezone which can be done in PHP/Laravel with Carbon.
for the Elastic i assigned the object with the ISO 8601 date Format
$header->DateTimeStart =$dateTime1->format('c');
And now things are different, suddently Elastic recognizes which timezone the timestamp was saved in and shows the correct dates in the datetime fields, both in kibana and my own application ... seems you have always add the timezone in a way elastic can deal it!
now i've an UTC Timestamp and the regular Timestamp in my data which is fine, just in case. Thank you everybody for helping me out!
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.