time.ino 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #ifdef ENABLE_FEATURE_NTP_TIME
  2. bool setupTime()
  3. {
  4. if (strlen(confTime.ntpServer1) > 4 && strlen(confTime.ntpServer2) > 4)
  5. {
  6. //configTime(gmtOffset_sec, daylightOffset_sec, ntpServer [, ntpServer2[, ntpServer3]]);
  7. configTime(0, 0, confTime.ntpServer1, confTime.ntpServer2);
  8. }
  9. else if (strlen(confTime.ntpServer1) > 4)
  10. {
  11. configTime(0, 0, confTime.ntpServer1);
  12. }
  13. else return false;
  14. setenv("TZ", confTime.timeZoneStr, 1); // Zeitzone einstellen https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
  15. sendLog(F("NTP: updating time..."), LOGLEVEL_INFO);
  16. return true;
  17. }
  18. void syncClock(bool force)
  19. {
  20. time_t now = time(&now);
  21. static time_t lastsek{0};
  22. if (lt.tm_sec != lastsek || force)
  23. {
  24. lastsek = lt.tm_sec;
  25. if (force || !(time(&now) % confTime.ntpSyncInterval))
  26. {
  27. //sendLog(F("NTP: updating time"), LOGLEVEL_INFO);
  28. setupTime();
  29. }
  30. }
  31. }
  32. void updateTime() {
  33. time_t now = time(&now);
  34. localtime_r(&now, &lt);
  35. }
  36. void printDate() {
  37. char buf[30];
  38. strftime(buf, sizeof(buf), "DATE: %Y-%m-%d %H:%M:%S", &lt);
  39. sendLog(buf);
  40. }
  41. #endif