Problems with time.localtime()

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
MungoBBQ
Posts: 3
Joined: Sun Oct 31, 2021 10:39 pm

Problems with time.localtime()

Post by MungoBBQ » Sun Oct 31, 2021 10:45 pm

I'm trying to build a clock using the Pico. Right now I am working on setting the clock, which will be done with a button on the back.

I'm using the RTC and its functions to display the time. Every time the button is pushed, I want to set the time five minutes ahead of the current time.

I thought the following would work fine:

Code: Select all

t = rtc.datetime()
timestamp = time.mktime(t)
timestamp = timestamp + 300
rtc.datetime(time.localtime(timestamp))
However, adding 300 seconds to the epoch-converted time doesn't work, and I think it's because the time.localtime() function interprets every added second as an added minute instead!

Have a look at this output from my code in debugging. I've just added ONE second to the timestamp now, instead of 300.

Code: Select all

Current time tuple
(2021, 10, 31, 6, 23, 44, 18, 0)
Current timestamp
1635661424
Added one second
1635661425
New time tuple
(2021, 10, 31, 6, 23, 45, 6, 304)
Am I on to something here - a bug? Or am I doing something stupid?

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

Re: Problems with time.localtime()

Post by dhylands » Mon Nov 01, 2021 1:08 am

The tuple taken by RTC.datetime is subtley different from that returned by date.localtime

And similarly you can’t pass the tuple returned by RTC.datetime into time.mktime

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Problems with time.localtime()

Post by scruss » Mon Nov 01, 2021 1:31 am


MungoBBQ
Posts: 3
Joined: Sun Oct 31, 2021 10:39 pm

Re: Problems with time.localtime()

Post by MungoBBQ » Mon Nov 01, 2021 7:03 am

scruss wrote:
Mon Nov 01, 2021 1:31 am
asked and answered here, too: Getting EINVAL when attempting to set the RTC in my pico - Raspberry Pi Forums
Yeh, that’s me. ;) But a different problem.

MungoBBQ
Posts: 3
Joined: Sun Oct 31, 2021 10:39 pm

Re: Problems with time.localtime()

Post by MungoBBQ » Thu Nov 04, 2021 5:09 pm

dhylands wrote:
Mon Nov 01, 2021 1:08 am
The tuple taken by RTC.datetime is subtley different from that returned by date.localtime

And similarly you can’t pass the tuple returned by RTC.datetime into time.mktime
THANK YOU! Never in a million years do I think I would have found that small difference on my own. What a weird difference to have in there.

Code works fine now, after juggling values back and forth.

Post Reply