Page 1 of 1

Problems with time.localtime()

Posted: Sun Oct 31, 2021 10:45 pm
by MungoBBQ
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?

Re: Problems with time.localtime()

Posted: Mon Nov 01, 2021 1:08 am
by dhylands
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

Re: Problems with time.localtime()

Posted: Mon Nov 01, 2021 1:31 am
by scruss

Re: Problems with time.localtime()

Posted: Mon Nov 01, 2021 7:03 am
by MungoBBQ
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.

Re: Problems with time.localtime()

Posted: Thu Nov 04, 2021 5:09 pm
by MungoBBQ
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.