Date/Time management
Re: Date/Time management
Looks like you're probably on ESP32.
On ESP32, the weekday param in the tuple is ignored in rtc.datetime(timetuple) (as it can be inferred from the date). ESP32 knows the calendar, and 28th Jan 2020 is a Tuesday (monday=0, tuesday=1), so that's why it's a 1 when you read the tuple back out again. You could change that 2 to any number and the behavior of your test program would be the same.
This matches regular Python's behavior where Monday=0 -- https://docs.python.org/3/library/datet ... te.weekday
On STM32, it relies on the hardware RTC to manage both the day/month/year as week as the weekday, so it will happily take whatever weekday you tell it and give the same weekday back to you.
On ESP32, the weekday param in the tuple is ignored in rtc.datetime(timetuple) (as it can be inferred from the date). ESP32 knows the calendar, and 28th Jan 2020 is a Tuesday (monday=0, tuesday=1), so that's why it's a 1 when you read the tuple back out again. You could change that 2 to any number and the behavior of your test program would be the same.
This matches regular Python's behavior where Monday=0 -- https://docs.python.org/3/library/datet ... te.weekday
On STM32, it relies on the hardware RTC to manage both the day/month/year as week as the weekday, so it will happily take whatever weekday you tell it and give the same weekday back to you.
Re: Date/Time management
Yes, I was surprised a while back by my ESP32 in a similar way. It does its own weekday calculation. It also assumes UTC so if you pull time from NTP, you might get back an unexpected weekday.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Date/Time management
Unfortunately machine.RTC documentation is poor, and the datetime method is not documented at all. In practice it corresponds to that in the pyb module, where the docs state that the weekday is 1-7 for Monday through Sunday.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: Date/Time management
Often there is a confusion because python and linux have a different weekday logc. Python having monday as 0 while linux has Sunday as 0.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Re: Date/Time management
I was very excited to see that the ESP32 MicroPython RTC now supports microseconds. Reading some earlier posts made me think that wouldn't happen, but it does... and very well!
Re: Date/Time management
Oh no! I think I spoke too soon. Microseconds roll, but whenever I set the RTC, it looks like the microseconds are set to 0. Is this a MicroPython or ESP32 thing? I need to sync this thing to a time standard using IEEE 1588 PTP so that observations correlate to an actual time. Is there a way system calls let me do this?
Hopefully it won't be necessary to keep a microsecond offset outside the RTC.
Hopefully it won't be necessary to keep a microsecond offset outside the RTC.
Re: Date/Time management
I found a github issue that was fixed in November 2019. Sadly I'm using V 1.11 because it's a build that supports 8MB flash.
Updating to 1.12 made it possible to set the RTC microseconds.
Updating to 1.12 made it possible to set the RTC microseconds.
Re: Date/Time management
I tried the example:
which results with:
Code: Select all
import machine
rtc = machine.RTC()
rtc.init((2020, 3, 8, 23, 51, 0, 0, 0))
Code: Select all
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: function takes 1 positional arguments but 2 were given
pythoncoder wrote: ↑Fri Sep 30, 2016 4:42 pmSee http://docs.micropython.org/en/latest/e ... utime.html - notably localtime().
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Date/Time management
The documentation is poor; so poor that the example code doesn't actually work. I have raised this and offered a partial fix. This has not been taken up.
You haven't said what platform you're using but if it's a Pyboard I suggest you use pyb.RTC and rtc.datetime. The pyb docs are correct and datetime does work on the platforms I tested.
You haven't said what platform you're using but if it's a Pyboard I suggest you use pyb.RTC and rtc.datetime. The pyb docs are correct and datetime does work on the platforms I tested.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Date/Time management
I wrote a clock app for the ESP32 where I messed with time. I got the NTP time and then offset it by timezone. Here's how I set the localtime:
You can see all of the code here: https://github.com/the-stanely/Clock-ESP32-Nokia5110
Code: Select all
# Set the RTC to local time. (I don't think the uP docs are exactly right about this.)
RTC().datetime((yy, mo, dd, 0, hh, mm, ss, 0))