datetime string convert to datetime

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
KLF
Posts: 37
Joined: Mon Jan 03, 2022 5:16 pm

datetime string convert to datetime

Post by KLF » Sat Jan 08, 2022 8:11 am

Good morning everyone,

how is it possible to convert a dateime string into datetime format in micropython?
I would like to compare an actual timestamp with a timestamp priveous stored in string format.
How is that possible in micropython if datetime is not available?

In Python it is possible like this:

Code: Select all

from datetime import datetime

datetime_string = '01/08/22 10:47:26'

datetime_object = datetime.strptime(datetime_string, '%m/%d/%y %H:%M:%S')

print(datetime_object)
Thanks a lot for any help

KLF
Posts: 37
Joined: Mon Jan 03, 2022 5:16 pm

Re: datetime string convert to datetime

Post by KLF » Sat Jan 08, 2022 11:41 am

Hi everyone,

it seems that I have to go via Seconds since 1.1.2000 and then compare the integer values.
That works (not based on seconds, but based on minutes - don't know why, but it's close enough for me)

Best regards
Klaus

User avatar
lorcap
Posts: 10
Joined: Sat May 23, 2020 9:02 am
Location: Italy

Re: datetime string convert to datetime

Post by lorcap » Sun Jan 09, 2022 8:59 pm

If you don't mind experimental code, you can try out my datetime branch. It's stable from my point of view: it's just waiting for a PR merging. You can find the doc too.

KLF
Posts: 37
Joined: Mon Jan 03, 2022 5:16 pm

Re: datetime string convert to datetime

Post by KLF » Mon Jan 10, 2022 4:18 pm

Hi lorcap,

this library sounds very interesting.
But in folder: upy-micropython-lib-datetime\upy-micropython-lib-datetime\micropython
I can't find anything regarding your datetime description.
Is your description really related to MICRO-Python?

best regards
Klaus

User avatar
lorcap
Posts: 10
Joined: Sat May 23, 2020 9:02 am
Location: Italy

Re: datetime string convert to datetime

Post by lorcap » Mon Jan 10, 2022 8:18 pm

You're right, the directory structure may not be obvious to non-developers. My contribution is in this folder.

PS: yes, it's related to MicroPython.

KLF
Posts: 37
Joined: Mon Jan 03, 2022 5:16 pm

Re: datetime string convert to datetime

Post by KLF » Tue Jan 11, 2022 8:36 am

Good morning lorcap,

thanks a lot for your tip.
I did not expect it in that folder, because there is a micropython folder available and the folder you gave me is a subfolder of python-stdlib.
So I thought python-stdlib is not defined for micropython.

best regards
Klaus

Post Reply