[Solved/Fixed] Time.now() in setup() gives bad time after power on

In our project we want to report the time that the core was started. In setup() we made a call to Time.now() and stashed the value away for later use.

This works great when the core is started from the reset button. However, when the core is actually power cycled the reported time is very small. Like 2 or 3 instead of a good epoc time.

My guess is that where ever the Time library gets the value, it isn’t ready so quickly after a power up.

We’ll deal with this in our project, but I wanted to put it out here for others.

Jim

@JSchrempp, take a lot at this topic:

https://community.sparkdevices.com/t/time-now-in-setup-gives-bad-time-after-power-on/9098

@peekay123, You pointed me back to my same topic. Maybe you meant to include a different link?

BTW, I now see that after a power on Time.now() takes about 2 seconds to become stable.

Jim

I hate my ipad sometimes :stuck_out_tongue:

The correct link is:

1 Like

I’ve added this code to our setup() function. It seems to do the trick.

do
{
    resetTime = Time.now();        // the the current time = time of last reset
    delay(10);
} while (resetTime < 1000000 && millis() < 20000); // wait for a reasonable epoc time, but not longer than 20 seconds

Jim

this seemed to do the trick for me

  if(CurrentTime < Time.now()){
      CurrentTime == Time.now();
  }