Page 1 of 1

datetime porting proposal

Posted: Sun Sep 19, 2021 8:31 pm
by lorcap
Hi everyone,

my long-term goal is converting my personal project from Zerynth to MicroPython. First step would be porting the datetime module I wrote last year. It represents a minimalist implementation of Python module datetime. Is anybody working on something similar?

What would be a good name, datetime or udatetime? Despite what the doc says, I see the u version is not used under `micropython-lib/micropython-lib/`.

Thanks,
Lorenzo

Re: datetime porting proposal

Posted: Wed Sep 22, 2021 8:43 am
by pythoncoder
This looks useful and it should be easy to port. But the GNU licensing will limit its use.

Re: datetime porting proposal

Posted: Thu Sep 23, 2021 9:38 pm
by lorcap
Thanks Peter for you reply.

I'm almost done with the porting. It was easy and I managed to improve the library. For a glimpse, see my github repo.

Regarding the licensing, I don't mind adopting the MIT License.

Re: datetime porting proposal

Posted: Fri Sep 24, 2021 1:32 pm
by pythoncoder
I am not a lawyer but I'm pretty sure you can't legally do this. People contributing to (MIT licenced) MicroPython have to guarantee that their code is not derived from GPL sources. The GPL has a horrible reputation.

Re: datetime porting proposal

Posted: Fri Sep 24, 2021 3:47 pm
by lorcap
I am the author of the that code and as such I can choose and change the licensing model (I believe).

Re: datetime porting proposal

Posted: Sat Sep 25, 2021 7:15 am
by pythoncoder
Ah, I'd missed that you wrote the original. I'm sure you're right about that :D

Re: datetime porting proposal

Posted: Sun Sep 26, 2021 8:57 pm
by lorcap
Ok, I just made my pull requests for code (#449) and doc (#7859).

Re: datetime porting proposal

Posted: Mon Sep 27, 2021 12:12 am
by mattyt
I'll try and find time to review these properly; datetime is an often-requested feature and this will be a wonderful addition to MicroPython. Thank-you!

Re: datetime porting proposal

Posted: Mon Sep 27, 2021 9:03 am
by lorcap
Thanks for your interest, much appreciated.

As stated in the documentation I wrote for the module, the two main constraints for `timedelta` type are:
  • Minimum resolution is 1 second, instead of 1 microsecond.
  • Maximum delta spans over ±24855 days (±2^31 seconds or ±68 years) instead of ±999999999 days.
Both constraints allow `timedelta` to be simple: it can be represented with a single `int()`.

BTW, am I right when I say that MicroPython holds an `int()` within an `int32_t` (at least on 32 target boards)? Zerynth sacrificed one bit to signal that the Python type could be represented with a plain `int32_t`. Hence, an `int()` was in the range ±2^30 (i.e., ±34 years). MicroPython looks different to me, at least for the unix port, and I extended the range to ±2^31 (±68 years). Or is this micro-optimization futile, after all?

Not having to deal with microseconds also changes the constructor interface from `timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)` to `timedelta(self, hours=0, minutes=0, seconds=0, days=0, weeks=0)`. A different approach would be to keep the same Python arguments and discard any microsecond juggling.

`datime()` is again an `int()` of days since January 1st, 0001, plus a `timedelta()`. Hence, arithmetic between `datetime()`'s and `timedelta()`'s are limited to ±68 years. On the other hand, no limits to datetime/datetime arithmetic.

Python's classes `time()` and `date()` were not implemented: `date()` can be a `datetime()` set to midnight; `time()` can be a `timedelta()` in the range [0-24h). Providing `date()` and `time()` constructors wouldn't be difficult, if needed.

`timezone()` requires user customization for dealing with timezones: no IANA timezones were implemented which were demanded to another module `dateutil.tz` even in Python. Folding (to resolve wall-time ambiguities during DST changes) is not implemented.

Re: datetime porting proposal

Posted: Fri Nov 05, 2021 5:48 pm
by pythoncoder
In a rather belated response to your query, this doc explains the small integer mapping. It uses 31 bits.