time.ino 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. bool setupTime()
  2. {
  3. if (strlen(confTime.ntpServer1) > 4 && strlen(confTime.ntpServer2) > 4)
  4. {
  5. //configTime(gmtOffset_sec, daylightOffset_sec, ntpServer [, ntpServer2[, ntpServer3]]);
  6. configTime(0, 0, confTime.ntpServer1, confTime.ntpServer2);
  7. }
  8. else if (strlen(confTime.ntpServer1) > 4)
  9. {
  10. configTime(0, 0, confTime.ntpServer1);
  11. }
  12. else return false;
  13. setenv("TZ", confTime.timeZoneStr, 1); // Zeitzone einstellen https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
  14. return true;
  15. }
  16. void updateTimeFromNTP()
  17. {
  18. time_t now = time(&now);
  19. static time_t lastsek{0};
  20. if (lt.tm_sec != lastsek)
  21. {
  22. lastsek = lt.tm_sec;
  23. if (!(time(&now) % confTime.ntpSyncInterval))
  24. {
  25. //Serial.println("updating time from NTP server");
  26. sendLog(F("NTP: updating time"), LOGLEVEL_INFO);
  27. setupTime();
  28. }
  29. }
  30. }
  31. void updateTime() {
  32. time_t now = time(&now);
  33. localtime_r(&now, &lt);
  34. }
  35. // char getDateTime() {
  36. // char* buf[30]; // je nach Format von "strftime" eventuell anpassen
  37. // time_t now = time(&now);
  38. // localtime_r(&now, &lt);
  39. // strftime(&buf, sizeof(buf), "%Y-%m-%d %T", &lt); // http://www.cplusplus.com/reference/ctime/strftime/
  40. // return buf;
  41. // }
  42. // char getDate() {
  43. // char buf[13];
  44. // updateTime();
  45. // strftime(buf, sizeof(buf), "%d.%m.%Y", &lt); // http://www.cplusplus.com/reference/ctime/strftime/
  46. // //return buf;
  47. // }
  48. // char getDate_YMD() {
  49. // char* buf[13];
  50. // time_t now = time(&now);
  51. // localtime_r(&now, &lt);
  52. // strftime(&buf, sizeof(buf), "%Y-%m-%d", &lt); // http://www.cplusplus.com/reference/ctime/strftime/
  53. // return buf;
  54. // }
  55. // char getTime() {
  56. // char* buf[6];
  57. // time_t now = time(&now);
  58. // localtime_r(&now, &lt);
  59. // strftime(&buf, sizeof(buf), "%H:%M", &lt); // http://www.cplusplus.com/reference/ctime/strftime/
  60. // return buf;
  61. // }
  62. // char getTime_HMS() {
  63. // char* buf[9];
  64. // time_t now = time(&now);
  65. // localtime_r(&now, &lt);
  66. // strftime(&buf, sizeof(buf), "%H:%M:%S", &lt); // http://www.cplusplus.com/reference/ctime/strftime/
  67. // return buf;
  68. // }
  69. /*void printTimeToSerial()
  70. {
  71. static char buf[30]; // je nach Format von "strftime" eventuell anpassen
  72. time_t now = time(&now);
  73. localtime_r(&now, &lt);
  74. //gmtime_r(&now, &utc);
  75. strftime(buf, sizeof(buf), "%Y-%m-%d %T", &lt); // http://www.cplusplus.com/reference/ctime/strftime/
  76. //Serial.printf("%s %s", buf, dayShortNames[lt.tm_wday]);
  77. Serial.printf("%s", buf);
  78. Serial.println();
  79. //Serial.printf("UTC: %.2d:%.2d:%.2d\n", utc.tm_hour, utc.tm_min, utc.tm_sec);
  80. //Serial.printf("%s: %.2d:%.2d:%.2d %s\n\n", *tzname, lt.tm_hour, lt.tm_min, lt.tm_sec, lt.tm_isdst ? "Sommerzeit" : "Normalzeit");
  81. }*/