Marijn78

Untitled

Nov 25th, 2020
84
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int getUnixTime(const char T[]) {
  2.   int y = 0, mo = 0, d = 0, h = 0, mi = 0, s = 0;
  3.   sscanf(T, "%d-%d-%dT%d:%d:%d", &y, &mo, &d, &h, &mi, &s);
  4.   struct tm result = {0};
  5.   result.tm_year = y - 1900;
  6.   result.tm_mon = mo - 1;
  7.   result.tm_mday = d;
  8.   result.tm_hour = h;
  9.   result.tm_min = mi;
  10.   result.tm_sec = s;
  11.   long unixTime;
  12.   time_t tt;
  13.   tt = mktime(&result);
  14.   unixTime = tt + UNIX_OFFSET;
  15.   return (int)unixTime;
  16. }
  17.  
  18. void updateTime2() {
  19.   if ((millis() - t) >= 1000) {
  20.     t = millis();
  21.     time_t now = time(nullptr);
  22.     tm = localtime(&now);
  23.     sprintf(tmbuff, "%02d/%02d/%02d %02d:%02d:%02d", tm->tm_mday, tm->tm_mon + 1, tm->tm_year - 100, tm->tm_hour, tm->tm_min, tm->tm_sec);
  24.     dayStamp = String(tm->tm_year + 1900) + "-" + String(tm->tm_mon + 1) + "-" + String(tm->tm_mday);
  25.     struct tm result = {0};
  26.     result.tm_year = tm->tm_year;
  27.     result.tm_mon = tm->tm_mon;
  28.     result.tm_mday = tm->tm_mday;
  29.     result.tm_hour = tm->tm_hour;
  30.     result.tm_min = tm->tm_min;
  31.     result.tm_sec = tm->tm_sec;
  32.     int unixTime;
  33.     time_t tt;
  34.     tt = mktime(&result);
  35.     timestampUnix = tt + UNIX_OFFSET;
  36.   }
  37. }
  38.  
  39. void setTimestampUnix() {
  40.   configTime(0, 0, ntp_server); // adjust for your TZ and DST
  41.   while (timestampUnix < 1588689631) {
  42.     delay(1000);
  43.     updateTime2();
  44.   }
  45.   updateTime2();
  46. }
RAW Paste Data