datetime / localtime

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
ttmetro
Posts: 104
Joined: Mon Jul 31, 2017 12:44 am

datetime / localtime

Post by ttmetro » Fri Dec 06, 2019 6:31 am

On an ESP32 with generic v1.11 micropython I get what appears (to me) to be an inconsistency with time:

Code: Select all

import time
from machine import RTC

def date(st):
    print("{:4}-{:2}-{:02d} {:2}:{:02d}:{:02d}".format( \
        st[0], st[1], st[2], st[3], st[4], st[5]))

st = (2019, 12, 6, 6, 20, 53, 4, 340)
RTC().datetime(st)

date(st)
date(time.localtime())

Output:

Code: Select all

2019-12-06  6:20:53
2019-12-06 20:53:04
I'd expect the outputs to match, within a few seconds, perhaps. What am I doing wrong?

Thanks!
Bernhard Boser

nevercast
Posts: 6
Joined: Tue Oct 22, 2019 11:37 pm

Re: datetime / localtime

Post by nevercast » Fri Dec 06, 2019 8:45 am

Hi ttmetro,

The tuple for time.localtime and RTC.datetime are not identical, there is a small difference between them.

time.localtime gives (year, month, day, hour, minute, second, weekday, yearday)
RTC.datetime gives (year, month, day, weekday, hours, minutes, seconds, subseconds)

subseconds is microseconds on the ESP32, a value between 0 and 1000000.
But notice that the weekday is in a different place. Also time.localtime has a yearday, where as RTC.datetime has subseconds.

Should these tuples be the same? Probably! But in v1.11 they are not. Hope this helps.

ttmetro
Posts: 104
Joined: Mon Jul 31, 2017 12:44 am

Re: datetime / localtime

Post by ttmetro » Sat Dec 07, 2019 5:46 pm

Thanks! Indeed an unfortunate discrepancy.
Bernhard Boser

User avatar
MostlyHarmless
Posts: 166
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: datetime / localtime

Post by MostlyHarmless » Sat Dec 07, 2019 6:30 pm

nevercast wrote:
Fri Dec 06, 2019 8:45 am
Should these tuples be the same? Probably! But in v1.11 they are not. Hope this helps.
Can they be the same?

Python's time.struct_time has the exact same 9 fields as the Unix ctime struct tm has (just in a different order). Since that is a standard coming from so high up, changing this format is out of the question.

However, RTC.datetime provides microseconds (at least on the ESP32), which would be lost if we made them the same. Not desirable.


Regards, Jan

ttmetro
Posts: 104
Joined: Mon Jul 31, 2017 12:44 am

Re: datetime / localtime

Post by ttmetro » Sat Dec 07, 2019 7:44 pm

Trouble is that although sensible, this change could break existing code. Hence we are stuck with it, at least until MicroPython 2.0.
Bernhard Boser

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

Re: datetime / localtime

Post by pythoncoder » Sun Dec 08, 2019 7:43 am

I think there is a case for the machine module to have an RTC class with standard behaviour, with port-specific modules like pyb having hardware specific extensions and behaviours. The machine version would inevitably have reduced functionality common to all RTC hardware.
Peter Hinch
Index to my micropython libraries.

jedie
Posts: 252
Joined: Fri Jan 29, 2016 12:32 pm
Contact:

Re: datetime / localtime

Post by jedie » Sun Dec 08, 2019 11:16 am

Whats the résumé ?

Prefere time.localtime for compatibility and use it if microseconds are not needed?

nevercast
Posts: 6
Joined: Tue Oct 22, 2019 11:37 pm

Re: datetime / localtime

Post by nevercast » Sun Dec 08, 2019 9:29 pm

jedie wrote:
Sun Dec 08, 2019 11:16 am
Prefere time.localtime for compatibility and use it if microseconds are not needed?
This is my preference. Use `time` until you need special `RTC` behaviour. I'm not sure if moving weekday in the RTC tuple to the same place in the localtime tuple actually helps anyone. Without checking the format on the docs, you might still assume they're the same.

While it might have been good for them to be identical from the start, or closer to it, they aren't so ttmetro is correct that it's kind of stuck that way until MicroPython goes through a breaking change version bump. (For what its worth, MicroPython doesn't have semantic versioning, but non-breaking changes is always preferred over breaking changes.)

User avatar
MostlyHarmless
Posts: 166
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: datetime / localtime

Post by MostlyHarmless » Tue Dec 10, 2019 2:30 am

While playing with ntptime (to send sensor data to a graphite server) I noticed that our current implementation
  • completely ignores fractional seconds,
  • makes absolutely no attempt to figure out any kind of round trip delay,
  • has no methods to update the clock periodically without risking to block the whole system for a second if a UDP packet gets lost,
  • should mention in the docs that one can set the host attribute if one has a local NTP server, which will greatly reduce the chance of lost packets.
I don't mean to go all the way with the statistical analysis of multiple round trips. But just measuring t0..t3 and then adding microseconds to the machine.RTC().datetime() call could get the MCU within a couple milliseconds. Right now it is on average half a second off because the code truncates everything to full second.


Regards, Jan

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

Re: datetime / localtime

Post by pythoncoder » Wed Dec 11, 2019 8:48 am

It is rather rudimentary. If you felt like writing an improved version and hosting it on GitHub I'm sure there would be quite a few takers.
Peter Hinch
Index to my micropython libraries.

Post Reply