Python Forum

Full Version: date time question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I would like to convert string in to date format which is acceptable by elasticsearch

doc = {
'submitted': '2020-01-25 20:10:10',
'num': '2',
'timestamp': 1579624522874,
}

I have two field submitted and timestamp
timestamp I was able to convert using following and elasticsearch took it as timestamp.
datetime.fromtimestamp(int(doc['timestamp'])/1000).strftime('%Y-%m-%d %H:%M:%S')


but submitted I have to convert in to isoformat which I can't seems to get it working.
I also want to add timezone "Etc/UTC" to it.
Use code tags.
(Jan-31-2020, 06:11 PM)pythonlearner1 Wrote: [ -> ]I also want to add timezone "Etc/UTC" to it.
If timezone is involved i now use only Pendulum,before it often was a combination of datetime, dateutil, pytz ..ect Doh
>>> import pendulum
>>> 
>>> d = doc.get('submitted')
>>> d
'2020-01-25 20:10:10'
>>> pendulum.parse(d)
DateTime(2020, 1, 25, 20, 10, 10, tzinfo=Timezone('UTC'))
>>> print(pendulum.parse(d))
2020-01-25T20:10:10+00:00
>>> 
>>> ts = doc.get('timestamp')
>>> ts
1579624522874
>>> ts = ts / 1000
>>> pendulum.from_timestamp(ts)
DateTime(2020, 1, 21, 16, 35, 22, 874000, tzinfo=Timezone('UTC'))
>>> print(pendulum.from_timestamp(ts))
2020-01-21T16:35:22.874000+00:00
this is good. it is working
but I want to keep date and time 20:10:10 as is and then tell it that this is UTC timezone.
I just have play around with different setting.

as I want date-time as is and send it to elasticsearch. and then elasticsearch should save it as utc. and kibana will convert it back to local time.