Skip to content

[HELP] New TZ.h/IANA timezones not working with my old code for setting the time #6621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
meltdown03 opened this issue Oct 7, 2019 · 2 comments
Assignees

Comments

@meltdown03
Copy link

meltdown03 commented Oct 7, 2019

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.

@d-a-v d-a-v self-assigned this Oct 8, 2019
@meltdown03
Copy link
Author

I got it working, so if the solution in my edited OP is the proper way to do this let me know. I'll close the issue or it can just be closed.

@meltdown03
Copy link
Author

Pull #6828 fixed my issue so I no longer need to change my normal time setting code. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants