datetime not working out of the box

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
johanson
Posts: 8
Joined: Sat May 22, 2021 1:04 pm

datetime not working out of the box

Post by johanson » Sat May 22, 2021 1:57 pm

I've ported a package originally written for Python to MicroPython but in order to get it working I had to implement some fixes to datetime package from micropython-lib based on solutions I found online. Datetime installed via upip it does not even import without errors but I had to fix more than that. I found this issue where someone went through exactly what I needed and I implemented the changes suggested there.

Specifically in micropython-lib/datetime/datetime.py
  1. on line 1356 I had to change 1.0 to 1 (float to int)
  2. replace line 1366 with the following try block:

    Code: Select all

    try:  # fix per https://github.com/smlng/pycayennelpp/issues/53
        y, m, d, hh, mm, ss, weekday, jday, dst = converter(t)
    except ValueError:
        y, m, d, hh, mm, ss, weekday, jday = converter(t)
    
  3. and add to line 917:

    Code: Select all

        def __new__(cls, *args, **kwargs):
            return object.__new__(cls)
    
This is what a success with my changes looks like:

Code: Select all


MicroPython v1.14 on 2021-02-02; ESP32 module (spiram) with ESP32
Type "help()" for more information.
>>> ts=1612971162
>>> from datetime import datetime
>>> from datetime import timezone
>>> datetime.fromtimestamp(ts, timezone.utc)
datetime.datetime(2051, 2, 10, 15, 32, 42, tzinfo=datetime.timezone.utc)
>>> 
There are some PRs open on GitHub for these changes but they are not being actively merged (e.g. PR338 that fixes #319. The question that I have is how should I approach the requirement for the fixes? Should I work to push them into micropython-lib or should I keep my custom version of datetime that is 99.99 % identical? And if I keep my own versions how do I include them in my new package when it comes to licensing? I may have a similar requirement for another MicroPython package or two and they all come with different licenses. Thank you for any advice.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: datetime not working out of the box

Post by SpotlightKid » Sat May 22, 2021 2:26 pm

I would not count on PRs to micropython-lib being merged anytime soon. Many have been languishing for years. There has been an effort to renovate and restructure micropython-lib about a year ago, but it seems to have fizzled out and nothing has happened.

As to maintaining your own version, I don't see a a real problem, since this is all licensed under the MIT or, in this case, Python License. Just keep the copyright notice in the file and include it in the top-level LICENSE file in your project. You can also just rename your custom module to avoid confusion.
Last edited by SpotlightKid on Sat May 22, 2021 2:41 pm, edited 1 time in total.

johanson
Posts: 8
Joined: Sat May 22, 2021 1:04 pm

Re: datetime not working out of the box

Post by johanson » Sat May 22, 2021 2:40 pm

Thank you, this is the kind of insight I don't have, being still relatively new to MicroPython. I will keep the changes inside my module and include original license information in the respective files.

Who manages micropython-lib and why is there so little activity? Some packages could use fixes and improvements and besides datetime I could submit some improvements to urequests as well.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: datetime not working out of the box

Post by SpotlightKid » Sat May 22, 2021 2:50 pm

johanson wrote:
Sat May 22, 2021 2:40 pm
Who manages micropython-lib and why is there so little activity?
In theory the micropython maintainers, in practice nobody. The micropython-lib was mainly created by an earlier MicroPython core developer (with many modules taken & adapted from the CPython stdlib), who has since left the project and maintains his own version of MicroPython and micropython-lib. Those may not be compatible with upstream MicroPython anymore, AFAIK.

As to why micropython-lib is not maintained anymore (the very little work that is done on it, imo does not justify calling the situation otherwise), I don't understand it myself. And in my opinion it's a serious neglect on part of the MicroPython team.
Last edited by SpotlightKid on Sat May 22, 2021 2:55 pm, edited 1 time in total.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: datetime not working out of the box

Post by SpotlightKid » Sat May 22, 2021 2:53 pm

johanson wrote:
Sat May 22, 2021 2:40 pm
I could submit some improvements to urequests as well.
There are already about half a dozen of those with no response from the maintainers.

See also: viewtopic.php?f=15&t=10454

johanson
Posts: 8
Joined: Sat May 22, 2021 1:04 pm

Re: datetime not working out of the box

Post by johanson » Sat May 22, 2021 3:34 pm

I know, that's why I am asking here instead of straight submitting new PRs with confidence that they will be merged in my lifetime. It's a sad state of micropython-lib. I'll monitor the repo and the forum in case it picks up some activity in the future. Once again thank you all for pointing me in the right direction.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: datetime not working out of the box

Post by scruss » Sun May 23, 2021 8:44 pm

SpotlightKid wrote:
Sat May 22, 2021 2:50 pm
As to why micropython-lib is not maintained anymore (the very little work that is done on it, imo does not justify calling the situation otherwise), I don't understand it myself. And in my opinion it's a serious neglect on part of the MicroPython team.
It would be more helpful if the MicroPython team marked the micropython-lib repo as Archived. That would stop people suggesting PRs, at least.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: datetime not working out of the box

Post by stijn » Mon May 24, 2021 7:25 am

scruss wrote:
Sun May 23, 2021 8:44 pm
It would be more helpful if the MicroPython team marked the micropython-lib repo as Archived. That would stop people suggesting PRs, at least.
Please no. It also makes the repo read-only completely so it's not even possible to report issues or leave comments. Way too drastic imo. Plus, even when unmerged, there's value in PRs because they make code accessible for others. Wrt your point: a brief look at the issue/PR history immediately shows the state of the repository i.e. not exactly active. Enough informarmation for people to decide what to do with it, I think.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: datetime not working out of the box

Post by scruss » Mon May 24, 2021 11:47 pm

stijn wrote:
Mon May 24, 2021 7:25 am
Wrt your point: a brief look at the issue/PR history immediately shows the state of the repository i.e. not exactly active. Enough information for people to decide what to do with it, I think.
With the OP in this thread leaving PRs that'll never get looked at, and lots of people leaving issues, I'd hardly say this is obvious. It needs to be made read-only or people will think it's part of the MicroPython project.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: datetime not working out of the box

Post by stijn » Tue May 25, 2021 6:11 am

With the OP in this thread leaving PRs that'll never get looked at, and lots of people leaving issues, I'd hardly say this is obvious.
Why not? Surely if you come across some repository and see that the vast majority of PRs and issues are not checked by maintainers, that tells you something about the state repository (unless you're not familiar with how Github works perhaps)?

It needs to be made read-only or people will think it's part of the MicroPython project.
But it is part of the MicroPython project. It's just not active. And have you taken my considerations about the disadvantages of being read-only into account?

Post Reply