BLEBeaconScan / my_ntp.h /
352ff3a 5 years ago
1 contributor
37 lines | 1.156kb
#ifndef _MY_NTP_H
#define _MY_NTP_H

#include "my_constants.h"

#define TIMEZONE_OFFSET     8                                   // SGT
#define DST_OFFSET          0                                   // CEST
#define UPDATE_CYCLE        (1 * 1000)                          // every second

static char   acTimeStringTZ[38];
time_t acTimeEpoch;

const char* getTimeString(void) {
  acTimeEpoch = time(nullptr);
  struct tm *ts;
  ts = localtime(&acTimeEpoch);
  strftime(acTimeStringTZ, sizeof(acTimeStringTZ), "%Y-%m-%d %H:%M:%S+0800", ts);
  return acTimeStringTZ;
}


void setClock(void) {
  configTime((TIMEZONE_OFFSET * 3600), (DST_OFFSET * 3600), "time.google.com", "pool.ntp.org", "time.nist.gov");
  int count = 0;
  Serial.print("Waiting for NTP time sync: ");
  time_t now = time(nullptr);   // Secs since 01.01.1970 (when uninitalized starts with (8 * 3600 = 28800)
  while (now < 8 * 3600 * 2) {  // Wait for realistic value
    delay(500);
    Serial.print(".");
    now = time(nullptr);
    if(count>20) break;
    count++;
  }
  Serial.printf("\nCurrent time: %s\n", getTimeString());
}

#endif //_MY_NTP_H