Closed
Description
I have a bunch of sketches that all use the same function to set the time:
void setClock() {
configTime(TZ * 3600, 0, "pool.ntp.org", "time.nist.gov");
Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
Serial.print(".");
yield();
now = time(nullptr);
}
Serial.println();
localtime(&now);
Serial.print("Current local time: ");
Serial.println(ctime(&now));
}
When I try to use TZ_America_Chicago
in place of TZ *3600, 0
in the configTime function, I get the UTC time when printing ctime(&now)
Does the change require something else? I've tried to look in the example, which does work, but can't find the magic code to make mine work.
Thanks
EDIT:
I got it figured out by going through the example step-by-step and ended up with this:
void setClock() {
configTime(TZ, "pool.ntp.org", "time.nist.gov");
timeval tv = { 0, 0 };
timezone tz = { 0, 0 };
settimeofday(&tv, &tz);
now = time(nullptr);
Serial.print("Waiting for NTP time sync: ");
while (now < 8 * 3600 * 2) {
delay(250);
Serial.print(".");
yield();
now = time(nullptr);
}
Serial.println();
Serial.print("Current time: ");
Serial.print(ctime(&now));
Serial.println();
}
As you can see, the settimeofday(&tv, &tz)
and the 2 lines above are new and was able to remove the localtime(&now)
line further down. All is working now but if there is another solution, I'd be happy to know.
Metadata
Metadata
Assignees
Labels
No labels