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?
Date conversion Y-m-d H:i:s to epoch
Re: Date conversion Y-m-d H:i:s to epoch
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.
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)
Re: Date conversion Y-m-d H:i:s to epoch
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!
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!
Re: Date conversion Y-m-d H:i:s to epoch
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:
Any tips are greatly appreciated!!
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:
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' ??'2020-08-08T13:16:00Z'
Any tips are greatly appreciated!!
- 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
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.
Index to my micropython libraries.
Re: Date conversion Y-m-d H:i:s to epoch
Awesome, thanks! I'll give that a try!