# Millis() rolls over after 24 days

**URL:** <https://discourse.processing.org/t/millis-rolls-over-after-24-days/44039>\
**Category:** Coding Questions\
**Created:** [March 6, 2024, 4:16pm UTC](https://discourse.processing.org/t/millis-rolls-over-after-24-days/44039 "2024-03-06T16:16:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![schmittmachine](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@schmittmachine](https://discourse.processing.org/u/schmittmachine)\
**Post date:** [March 6, 2024, 4:16pm UTC](https://discourse.processing.org/t/millis-rolls-over-after-24-days/44039/1 "2024-03-06T16:16:02Z")

</div>

Hello,

as the return value of millis() is an int, it rolls over after 2147483647 ms, that is 24.8 days.

long t0 = System.currentTimeMillis();  
long t1 = t0 + 2147483647 + 10;  
int dt = (int)(t1 - t0);  
println(“dt=”+dt); // dt=-2147483639

So should we handle the rollover moment in our code, for sketches that use millis() for calculations or timers and that are supposed to run for ever ?

Or shouldn’t better use the system function System.currentTimeMillis() which returns a long, and rolls over in a few hundred years ?

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [March 6, 2024, 5:21pm UTC](https://discourse.processing.org/t/millis-rolls-over-after-24-days/44039/2 "2024-03-06T17:21:53Z")

</div>

Hi @schmittmachine ,

Well! I personally would use my own timer handling in such a case. Quite easy to implement…  
Internally processing works with long as it uses `System.currentTimeMillis()-starttime` to calculate the return value for `millis()`, but unfortunately casting to int for return value imo due to follow a rule to keep the datatype usage simple.

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![TiborUdvari](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiborudvari/32/18018_2.png) [@TiborUdvari](https://discourse.processing.org/u/TiborUdvari)\
**Post date:** [March 7, 2024, 4:38pm UTC](https://discourse.processing.org/t/millis-rolls-over-after-24-days/44039/3 "2024-03-07T16:38:51Z")

</div>

Yep, just use a larger type like you said `System.currentTimeMillis()`. It should do the job, no need to overthink it.

---

<div class="post-metadata">

**Author:** ![schmittmachine](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@schmittmachine](https://discourse.processing.org/u/schmittmachine)\
**Post date:** [March 7, 2024, 8:30pm UTC](https://discourse.processing.org/t/millis-rolls-over-after-24-days/44039/4 "2024-03-07T20:30:41Z")

</div>

Indeed. It’s just that I’ve been using millis() for years, even for works running for a very long time, and now I’m wondering what magic made them work without a glitch for so long.

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [March 8, 2024, 7:02am UTC](https://discourse.processing.org/t/millis-rolls-over-after-24-days/44039/5 "2024-03-08T07:02:43Z")

</div>

Hi @schmittmachine,

in the most common usage the impact will be quite low… see below.

Cheers  
— mnse

PS: Nevertheless, I personally wouldn’t use `millis()`, if I would expect the process should run stable and longer then 24d. 😁

```auto
final int deltaTimeMS = 1000;
int nextStepMS;
long current;

void setup() {
  size(500, 500);
  noStroke();
  current = Integer.MAX_VALUE - 20000L;
  nextStepMS = (int)current;
}

int next() {
  current+=100L;
  return (int)current;
}

void draw() {
  background(0);
  int t = next();
 
  // Most common approach, less impact as the logic still works as both flip signs
  if (t >= nextStepMS) {
    println("do somthing: " + t);
    nextStepMS = t + deltaTimeMS;
  }
  
  fill(0, 255, 0);
  // another approach with modulus will break because of sign flip
  arc(width*.25, height/2, 100, 100, 0, radians(floor(t/100.) % 360));
  
  fill(255, 0, 0);
  // same approach but with absolute modulus, will flip rotation direction
  arc(width*.75, height/2, 100, 100, 0, radians(abs(floor(t/100.)) % 360));
}

```

---

<div class="post-metadata">

**Author:** ![schmittmachine](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@schmittmachine](https://discourse.processing.org/u/schmittmachine)\
**Post date:** [March 8, 2024, 12:47pm UTC](https://discourse.processing.org/t/millis-rolls-over-after-24-days/44039/6 "2024-03-08T12:47:05Z")

</div>

Ok, just for the record, I understand now why my code has worked over this rollover over the years.

Most of my code moves objets according to time passed since last frame :

```auto
int millis_counter;
int previousT;
float angle = 0.0;

void setup() {
  size(500, 500);
  millis_counter = previousT = Integer.MAX_VALUE - 1000;
}

int myMillis() {
  millis_counter += 100;
  return millis_counter;
}

void draw() {
  background(0);
  
  int now = myMillis();
  int deltaT = now - previousT;
  println(" previousT="+previousT+" now="+now+" deltaT="+deltaT);
  previousT = now;
  
  // move according to time passed
  angle += deltaT*0.001;
  
  arc(width*.25, height/2, 100, 100, 0, angle % (2*PI));
}

```

And when rollover happens, the substraction actually works : -2147483549 - 2147483647 = 100

previousT=2147483447 now=2147483547 deltaT=100  
previousT=2147483547 now=2147483647 deltaT=100  
previousT=2147483647 now=-2147483549 deltaT=100  
previousT=-2147483549 now=-2147483449 deltaT=100  
previousT=-2147483449 now=-2147483349 deltaT=100

Thanks for the shared thoughts
