machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
kbrenner
Posts: 37
Joined: Mon Jan 20, 2020 8:05 pm

machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Post by kbrenner » Wed Jan 20, 2021 9:01 pm

Upon setting the datetime with machine.RTC().datetime(), the hours, minutes, and day of the week appear to be reading incorrect values:
machine.RTC().datetime().PNG
machine.RTC().datetime().PNG (7 KiB) Viewed 313 times
Does anyone know why this happens?

User avatar
dhylands
Posts: 3592
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Post by dhylands » Wed Jan 20, 2021 9:07 pm

I know rshell has a bunch of code to deal with the various RTC's:
https://github.com/dhylands/rshell/blob ... in.py#L951

It looks like the arguments to datetime are expected to be in the following order:
(year, month, day, weekday, hour, minute, second, subseconds)

kbrenner
Posts: 37
Joined: Mon Jan 20, 2020 8:05 pm

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Post by kbrenner » Wed Jan 20, 2021 10:10 pm

dhylands wrote:
Wed Jan 20, 2021 9:07 pm
I know rshell has a bunch of code to deal with the various RTC's:
https://github.com/dhylands/rshell/blob ... in.py#L951

It looks like the arguments to datetime are expected to be in the following order:
(year, month, day, weekday, hour, minute, second, subseconds)
Got it, thanks. There appears to be a discrepancy between utime.localtime() and machine.RTC().datetime() then:
utime.localtime().PNG
utime.localtime().PNG (53.49 KiB) Viewed 296 times

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

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Post by pythoncoder » Thu Jan 21, 2021 10:31 am

kbrenner wrote:
Wed Jan 20, 2021 10:10 pm
...
Got it, thanks. There appears to be a discrepancy between utime.localtime() and machine.RTC().datetime()...
There is. utime.localtime matches CPython's time module. Unfortunately RTC.datetime() - which isn't even documented - adopts a different convention which is based on STM hardware and the pyb module. The nearest you'll get to docs may be found here.
Peter Hinch

kbrenner
Posts: 37
Joined: Mon Jan 20, 2020 8:05 pm

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Post by kbrenner » Tue Feb 16, 2021 12:39 am

I have started to notice that the call to the function:

utime.localtime() is now reporting the following value:

>>> utime.localtime()
(2015, 1, 1, 0, 44, 11, 3, 1)

I am not sure why it reset itself to the first day of the year in 2015. Do you have any idea why this is happening?

kbrenner
Posts: 37
Joined: Mon Jan 20, 2020 8:05 pm

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Post by kbrenner » Thu Feb 18, 2021 2:27 am

Through trial and error, I've found that removing power to the pyboard and then plugging it back into the computer via microUSB is what is causing the time to reset. Simply hitting CTRL-D does not cause that to happen. Do either of you have any idea why this is occurring?

User avatar
dhylands
Posts: 3592
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Post by dhylands » Thu Feb 18, 2021 5:45 am

When you remove power then the the RTC will reset.

You need to provide power to VBAT (typically using a coin cell or simialr) if you want the RTC to keep ticking while power is removed.

kbrenner
Posts: 37
Joined: Mon Jan 20, 2020 8:05 pm

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Post by kbrenner » Thu Feb 18, 2021 3:50 pm

Got it. So is there any way to get the actual datetime then in between power cycles (outside of providing VBAT power with a coin cell or equivalent)? Due to other constraints, I don't have the ability to include an additional coin cell just to power VBAT. I could add code to the boot.py which would rewrite a hardcoded time to the RTC on boot up, but that value is constantly changing as time passes so there would be no way of knowing what to write there.

User avatar
dhylands
Posts: 3592
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Post by dhylands » Thu Feb 18, 2021 4:29 pm

Yeah - there is no RTC that I'm aware that can keep time when not powered. This is also why external RTC devices (like: https://www.adafruit.com/product/4282) require a battery.

I have rshell synchronize the time on the device with the time on the PC whenever it connects. If you have a network connection you can query an NTP server to get the current time, but otherwise there really isn't anything you can do.

kbrenner
Posts: 37
Joined: Mon Jan 20, 2020 8:05 pm

Re: machine.RTC().datetime() gives incorrect hours, minutes, and day of the week

Post by kbrenner » Thu Feb 18, 2021 6:08 pm

That makes sense, thanks!

Post Reply