Repository navigation
Conversation
rtc.RTC().datetime wrote the fields straight into the RTC's BCD calendar, unchecked: 1999 read back as 2095, Feb 30 25:61 was kept, and a tm_wday of 7 changed the year, since the HAL shifts the weekday into the date register unmasked. Check the fields first and raise ValueError for a year outside 2000-2099 or an impossible date or time. Store the weekday of the date instead of tm_wday. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Ideally, the validation should be done in a common place. Right now no other port validates the timeutils_struct_time_t fields.
I think it makes more sense to do the validation in struct_time_to_tm in shared-bindings/time/__init__.c. Then everyone benefits. But this will increase code size on every port.
Maybe there should even be a separate function to do the validation, so however the timeutils_struct_time_t is filled in, you can check it's OK. That function can be called in struct_time_to_tm.
If there are then specific things to validate for STM (like the 2099 limit), you can do that in the port.
|
@dhalbert I had Claude test the two approaches: a shared implementation for every port, and rolling over like the other ports. Claude says:
Which would you prefer? I'll update the PR, its test and its description to match. |
Go ahead with a shared check, thanks. We could turn it off on very small ports if needed. |
struct_time_to_tm() narrowed each field into timeutils_struct_time_t's uint8_t and uint16_t members unchecked, so a month of 257 arrived as 1 and an hour of 256 as 0 on every port, and the ports that count seconds read their month table out of bounds for a month of 0. Add struct_time_validate(), which checks the values before they are narrowed, and call it from struct_time_to_tm(): a real date and time, years 1970-2099 (what timeutils handles on 32-bit boards) and seconds up to 61, as struct_time documents. The STM port now checks only what its calendar can't hold: years before 2000 and leap seconds. time.localtime() reads the clock through struct_time_to_tm() too, and the Silicon Labs sleeptimer's clock starts at 1900-01-01, so start it at 2000-01-01 at reset, as the other ports start. Say in the rtc.RTC.datetime docstring that impossible values raise ValueError. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I've pushed a second commit with the shared check, as you suggested:
It adds 224 bytes on STM, 136-232 on the other ports built for this, and 212 on Silicon Labs. Tested on the Retro Arcade (STM32F412RE); the other boards were only built. The description has the details. |
Leap seconds are abandoned with decision from 15th. Oct., effective from May 20 next year. So one thing less to worry about (at least for a few thousand years, until the difference is larger than an hour). |
Refuses an impossible date or time when a
struct_timeis read, on every port, and on STM also a date or time the RTC calendar can't hold. It also stores the weekday of the date on STM instead oftm_wday. Two commits: the STM fix, then the general check moved tostruct_time_to_tm(), as asked in review.Problem
On STM,
rtc.RTC().datetime = ...writes the fields straight into the RTC's BCD calendar registers throughHAL_RTC_SetTime()andHAL_RTC_SetDate(). Their range checks areassert_param()s, which are compiled out, so anything is stored. On an STM32F412, 1999 read back as 2095, and(2026, 2, 30, 25, 61, 0, ...)read back unchanged.tm_wdayis stored as given too. Reading hides that, sincestruct_time_from_tm()works the weekday out from the date, butHAL_RTC_SetDate()shifts the weekday into the date register unmasked:tm_wday = 7sets the lowest bit of the year, so(2026, 11, 24, 12, 0, 0, 7, -1, -1)read back as 2027-11-24.On every port,
struct_time_to_tm()narrows each field into auint8_t(the year into auint16_t) before the port sees it, so a month of 257 arrives as 1 and an hour of 256 as 0. And the ports that count seconds pass the month totimeutils_seconds_since_2000(), which reads its month table out of bounds for a month of 0.Fix
struct_time_validate(), called fromstruct_time_to_tm()on the values before they are narrowed, raisesValueErrorunless they are a real date and time:tm_mon1-12,tm_mdayup to the month's length (Feb 29 only in leap years),tm_hour0-23,tm_min0-59,tm_sec0-61 (leap seconds, astime.struct_timedocuments) andtm_year1970-2099, the rangepy/mpconfig.hsays the time functions handle on 32-bit boards. It takes the six values rather than atimeutils_struct_time_t, so that it can see them before narrowing; a caller that has a struct can pass its fields.tm_wdayandtm_ydayaren't checked: nothing that sets a clock uses them.On STM,
common_hal_rtc_set_time()adds what the calendar can't hold:tm_year2000-2099 andtm_sec0-59.stm32_peripherals_rtc_set_time()stores the weekday of the date, worked out withtimeutils, instead oftm_wday.time.time(),time.localtime()andalarm.time.TimeAlarmread the clock throughstruct_time_to_tm()too. Every port's clock starts at 1970 or 2000, except Silicon Labs', whose sleeptimer starts at 1900-01-01; it now starts at 2000-01-01 at reset, sotime.localtime()doesn't raise before the clock is set.rtc.RTC.datetime's docstring says that an impossible date or time raisesValueError, and what STM32 boards hold.The messages are the existing
%q must be %d-%d, so there are no new strings to translate.Testing
tm_wday)tm_wday = 7read back as 2027ValueError, clock keptValueErrortime.localtime()raise; one in 1999 still workstime.mktime()still rolls Feb 30 over to Mar 2;time.monotonic()steady across a setmain)rukeeps 5180 freeThe board is the ELECFREAKS Retro Arcade (STM32F412RE; its board definition isn't upstream yet), on
main7d4ca58b48plus the board's definition (the change's files are the same as on this branch's base), with the script below. With this PR,time.time()andtime.localtime()still read the RTC as before. Not tested on other boards; the other ports were only built. On the Silicon Labs builds, the firmware calls the newcommon_hal_rtc_init()fromport_init()right aftersl_system_init().How to run the test
On any STM board with the
rtcmodule, copy the script ascode.pyand watch the serial console; it finishes within a second and sets the RTC and its time source back. ExpectPASSfor every check. Without this PR the impossible values are accepted, so the base run stores some of them.set_time_test.pyTrade-offs and Alternatives
ValueErrorwhere they used to roll over. Leap seconds are still allowed and rolled over there; STM refuses them, since its calendar can't hold them.struct_time_to_tm()also reads the time source fortime.time(),time.localtime()andalarm.time.TimeAlarm, so a time source that returns an impossible date now raises there, instead of giving a wrong time. Readingrtc.RTC().datetimeitself isn't checked, so a clock with a bad date can still be read and set. On the ports that count seconds since 2000, whose 32-bit count runs to 2106, reads would raise from 2100 on.time.mktime()doesn't use it and still rolls over.time.struct_timedocuments. Silicon Labs' sleeptimer refuses them, and its port ignores the error, so such a time is silently not set, as any bad value was before (mimxrt10xx probably likewise); Zephyr raises the driver's error. Not changed here.tm_year must be 2000-2099(the port) and 2100tm_year must be 1970-2099(the shared check). One range would need a port-configurable range in the shared check, which would also refuse an STM time source's 1999.Generative AI
I used generative AI (Claude Code) to write and test this change. I have reviewed it, and I am responsible for the code and this description.
🤖 Generated with Claude Code