Date conversion Y-m-d H:i:s to epoch

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
tikky
Posts: 2
Joined: Sun Jan 05, 2020 7:04 pm

Date conversion Y-m-d H:i:s to epoch

Post by tikky » Thu Feb 20, 2020 9:05 pm

Hello,

I am having a trouble to convert date in Y-m-d H:i:s foramt to micropython epoch.
How to do that. Let's say I have:
my_date = '2020-02-20 22:04:00'

I can't find in manual of mtime function to do that.
Could you please help me with that?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Date conversion Y-m-d H:i:s to epoch

Post by jimmo » Fri Feb 21, 2020 5:24 am

MicroPython doesn't have a built-in version of strptime (i.e. https://docs.python.org/3/library/datet ... e.strptime )
Rather than use up space providing a generic thing, it's expected that you would write a date parser specific for the types of dates you want to handle.

There might exist libraries, but it's pretty easy to split up the string into ints and then use time.mktime (which takes an 8-tuple of values).

By way of illustration, here's a very short but fairly terrible way of doing it.

Code: Select all

>>> import time
>>> my_date = '2020-02-20 22:04:00'
>>> time.mktime(tuple(int(x) for x in my_date.replace(' ','-').replace(':','-').split('-')) + (0,0))
635551440
>>> time.localtime(635551440)
(2020, 2, 20, 22, 4, 0, 3, 51)

tikky
Posts: 2
Joined: Sun Jan 05, 2020 7:04 pm

Re: Date conversion Y-m-d H:i:s to epoch

Post by tikky » Sat Feb 22, 2020 7:09 pm

Hello Jimmo,

thank you for fast response.
Your code resolves my issue.

Actually I thought that I can split date into few parts and use it as tuple,
but didn't knwo last two parts of tuple. Now I undersatand that I can use just 0,0.

Thank you once again!

Primesty
Posts: 49
Joined: Sun Jun 28, 2020 11:06 pm

Re: Date conversion Y-m-d H:i:s to epoch

Post by Primesty » Sat Aug 08, 2020 1:03 pm

Hi, this might be a little tangential, but still related

I'm querying an API and get a timestamp as a string returned in a JSON object - I'm using ujson to parse the JSON data and end up with a string like this:
'2020-08-08T13:16:00Z'
How can I reformat this into something more legible and, more importantly, convert the Zulu time to local EST (US) time? Is there a way in micropython to calculate the time-difference between two such timestamps, e.g. '2020-08-08T13:11:09Z' - '2020-08-08T13:16:00Z' ??

Any tips are greatly appreciated!!

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Date conversion Y-m-d H:i:s to epoch

Post by pythoncoder » Sun Aug 09, 2020 4:31 pm

Try something on these lines:

Code: Select all

import time
s = '2020-08-08T13:16:00Z'
y = int(s[:4])
m = int(s[5:7])
d = int(s[8:10])
hr = int(s[11:13])
min = int(s[14:16])
sec = int(s[17:19])
z = time.mktime((y, m, d, hr, min, sec, 0, 0))  # Zulu time in seconds since epoch
# Add localtime offset in secs (hrs*3600)
z += 3600
print(time.localtime(z))
Peter Hinch
Index to my micropython libraries.

Primesty
Posts: 49
Joined: Sun Jun 28, 2020 11:06 pm

Re: Date conversion Y-m-d H:i:s to epoch

Post by Primesty » Tue Aug 11, 2020 1:07 pm

Awesome, thanks! I'll give that a try!

Post Reply