time.ticks_ms overflow

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
zipdots
Posts: 2
Joined: Mon Jul 25, 2022 1:47 am

time.ticks_ms overflow

Post by zipdots » Mon Jul 25, 2022 2:08 am

Hello!

I recently started learning micropython and I apologize in advance for the question I want to ask, as I understand that most likely the answer to it has already been given, but I could not find it :(

Tell me, what is the maximum value that ticks_ms can return, what happens when it overflows, and what does the term "rollover" mean?

I make a mqtt listener and to maintain a connection with the server I use the ping method so that the server does not disconnect me:

Code: Select all

def _mqtt_keepalive_ping(self):
    time_since_last_ping = time.ticks_diff(time.ticks_ms(), self._mqtt_last_ping) // 1000
    if time_since_last_ping >= self._mqtt_client.keepalive:
        try:
            self._mqtt_client.ping()
        except OSError:
            self._mqtt_connected = False
        else:
            self._mqtt_last_ping = time.ticks_ms()
My method works but I don't know what will happen in the long run.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: time.ticks_ms overflow

Post by Roberthh » Mon Jul 25, 2022 5:25 am

The period is 2**30 ms, which is about 298 days. Rollover means, that the value will change from 2**30 -1 to 0. ticks_diff() caters for the rollover and will always return the proper value, unless the time difference is larger than the period of ~298 days.

jomas
Posts: 59
Joined: Mon Dec 25, 2017 1:48 pm
Location: Netherlands

Re: time.ticks_ms overflow

Post by jomas » Mon Jul 25, 2022 7:47 am

Roberthh wrote:
Mon Jul 25, 2022 5:25 am
The period is 2**30 ms, which is about 298 days.
It is ~298 Hours. Not days.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: time.ticks_ms overflow

Post by Roberthh » Mon Jul 25, 2022 8:02 am

right
but that's still a long time.

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

Re: time.ticks_ms overflow

Post by pythoncoder » Mon Jul 25, 2022 10:42 am

Unless you do some special arithmetic it's 298/2 hours. If you perform a modular arithmetic subtraction of t2 - t1 the result may be positive or negative, depending on whether t2 follows or precedes t1. So the result from ticks_diff(t2, t1) may be +- 298/2 hours. This is explained in the docs.
Peter Hinch
Index to my micropython libraries.

zipdots
Posts: 2
Joined: Mon Jul 25, 2022 1:47 am

Re: time.ticks_ms overflow

Post by zipdots » Mon Jul 25, 2022 5:11 pm

Thanks for your help!

I did not fully understand what would happen if, after overflowing time.ticks_ms() returned a value less than the one we stored. I ran a small loop and made sure that time._ticks_diff() really detects rollover correctly:

Code: Select all

start_time = time.ticks_us()
while True:
    current_time = time.ticks_us()
    time_diff = time.ticks_diff(current_time, start_time)
    if time_diff > 100000:
        print(f'Start time {start_time:10d} | Current time {current_time:10d} | Time diff {time_diff}')
        start_time = current_time

Code: Select all

Start time 1073425486 | Current time 1073525559 | Time diff 100073
Start time 1073525559 | Current time 1073625628 | Time diff 100069
Start time 1073625628 | Current time 1073726096 | Time diff 100468
Start time 1073726096 | Current time      84284 | Time diff 100012 <---
Start time      84284 | Current time     184337 | Time diff 100053
Start time     184337 | Current time     284397 | Time diff 100060
Start time     284397 | Current time     384438 | Time diff 100041
Thanks again for your answers!

Post Reply