Sync time with Internet?

Hello,

When my Arduino is starting up, I am trying to sync its time up with the internet and used the following code to do so. This code has worked in the past but today its never getting out of the while loop so I put some debug statements in the loop to see what’s going on. Looking at the output, it would be in this loop for a long time. Any thought or is there a better way of doing this? Thank you for your help!

void initTime()
{
 const int MIN_EPOCH = 40 * 365 * 24 * 3600;
 time_t epochTime;

 configTime(0, 0, "pool.ntp.org", "time.nist.gov");

 Serial.print("Fetching time from NTP");

 epochTime = time(NULL);

 while (epochTime < MIN_EPOCH)
 {
   Serial.print(".");
   delay(2000);
   epochTime = time(NULL);

   Serial.print("MIN EPOCH: ");
   Serial.println(MIN_EPOCH);
   Serial.print("EPOCH TIME: ");
   Serial.println(epochTime);
 }

 Serial.println();
 Serial.print("Fetched NTP epoch time is: ");
 Serial.println(epochTime);
}

Output:

Fetching time from NTP.MIN EPOCH: 1261440000
EPOCH TIME: 28810
.MIN EPOCH: 1261440000
EPOCH TIME: 28812
.MIN EPOCH: 1261440000
EPOCH TIME: 28814
.MIN EPOCH: 1261440000
EPOCH TIME: 28816
.MIN EPOCH: 1261440000
EPOCH TIME: 28818
.MIN EPOCH: 1261440000
EPOCH TIME: 28820

Read "How To Use This Forum"

in particular, 7. If you are posting code or error messages, use "code" tags

This is what happens when you do not

It looks like the time() function is getting a time that is WAY earlier than what you expect. Could it be that the "time()" function is returning time-of-day instead of epoch time? Looks like it's saying "just over 8 hours since midnight". Could you have conflicting time libraries? Did you get any warnings in the compile?

I resolved the issue by changing the wi-fi connection to a different source that my Arduino was connected to and that alone resolved the issue. Not sure why that resolved the issue but I glad its working now.
Thank you for the help!