/*
  NTP Setting for ESP8266 + ESP32
  https://werner.rothschopf.net/201802_arduino_esp8266_ntp.htm

*/


// verhindert doppelte Verwendung
#ifndef MY_NTP_TIME_H      // if not define then define .... endif
#define MY_NTP_TIME_H


//#include "myWiFi.h"    // Für ESP8266 (statt WiFi.h)   WLAN wird von myWiFiManager.h bereitgestellt
#include <time.h>         // ESP32/ESP8266 Systemzeit (time.h)

/* Configuration of NTP */
#define MY_NTP_SERVER "at.pool.ntp.org"           // ntp Server
#define MY_TZ "CET-1CEST,M3.5.0/02,M10.5.0/03"   // Zeitzonen Einstellung mit Sommer-Winterzeit

/* Globals */
time_t    now;              // this are the seconds since Epoch (1970) - UTC
tm        tm;               // the structure tm holds time information in a more convenient way

extern "C" uint8_t sntp_getreachability(uint8_t);   // für die Funktion getNtpServer

//Week Days
String weekDays[7]={"    Sonntag  ", "     Montag  ", "   Dienstag ", "    Mittwoch ", " Donnerstag", "    Freitag  ", "    Samstag  "};
//Month names
String months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};


// ------------------   NTP holen  ------------------------------
bool getNtpServer(bool reply = false) {
  uint32_t timeout {millis()};

  configTime(MY_TZ, MY_NTP_SERVER);   // Zeitzone einstellen https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv

  do {
    delay(25);
    if (millis() - timeout >= 1e3) {
      Serial.printf("Warten auf NTP-Antwort %02ld sec\n", (millis() - timeout) / 1000);
      delay(975);
    }
    sntp_getreachability(0) ? reply = true :  // Prüft ob einer der drei Server (0,1,2) erreichbar ist, wenn nicht false
    sntp_getreachability(1) ? reply = true : 
    sntp_getreachability(2) ? reply = true : false;

  } while (millis() - timeout <= 16e3 && !reply);
  return reply;
}


// ------------------  Anzeige von Datum und Uhrzeit ------------------
void showTime() {
  time(&now);                       // read the current time
  localtime_r(&now, &tm);           // update the structure tm with the current time
  Serial.print("year:");
  Serial.print(tm.tm_year + 1900);  // years since 1900
  Serial.print("\t month:");
  Serial.print(tm.tm_mon + 1);      // January = 0 (!)
  Serial.print("\t day:");
  Serial.print(tm.tm_mday);         // day of month
  Serial.print("\t hour:");
  Serial.print(tm.tm_hour);         // hours since midnight  0-23
  Serial.print("\t min:");
  Serial.print(tm.tm_min);          // minutes after the hour  0-59
  Serial.print("\t sec:");
  Serial.print(tm.tm_sec);          // seconds after the minute  0-61*
  Serial.print("\t wday");
  Serial.print(tm.tm_wday);         // days since Sunday 0-6
  if (tm.tm_isdst == 1)             // Daylight Saving Time flag
    Serial.print("\t DST");
  else
    Serial.print("\t standard");
  Serial.println();

  //Serial.println("Datum:\t" + String(tm.tm_year +1900) +"."+ String(tm.tm_mon + 1) +"."+ String(tm.tm_mday) + "\t" + weekDays[tm.tm_wday]);
  //Serial.println("Uhrzeit:\t" + String(tm.tm_hour) +":"+ String(tm.tm_min) +":"+ String(tm.tm_sec));
  //Serial.println();
}

// 25.10.2024
String printDateDE() {
  time(&now);                       // read the current time
  localtime_r(&now, &tm);           // update the structure tm with the current time
  
  String strDate = "";
  if (tm.tm_mday <10) strDate += "0";
  strDate += String(tm.tm_mday); strDate += ".";
  if (tm.tm_mon + 1 <10) strDate += "0";
  strDate += String(tm.tm_mon + 1);  strDate += ".";
  strDate += String(tm.tm_year +1900);
  
  Serial.print(strDate);    // 25.10.2024
  return strDate;
}

// 2024.10.25
String printDateENG() {
  time(&now);                       // read the current time
  localtime_r(&now, &tm);           // update the structure tm with the current time
  
  String strDate = String(tm.tm_year +1900) +".";
  if (tm.tm_mon + 1 <10) strDate += "0";
  strDate += String(tm.tm_mon + 1); strDate += ".";
  if (tm.tm_mday <10) strDate += "0";
  strDate += String(tm.tm_mday);
  
  Serial.print(strDate);    // 2024.10.25
  return strDate;
}

// 17:48:05
String printTime() {
  time(&now);                       // read the current time
  localtime_r(&now, &tm);           // update the structure tm with the current time

  String strTime = "";
  if (tm.tm_hour <10) strTime += "0";
  strTime += String(tm.tm_hour); strTime += ":";
  if (tm.tm_min <10) strTime += "0";
  strTime += String(tm.tm_min); strTime +=":";
  if (tm.tm_sec <10) strTime += "0";
  strTime += String(tm.tm_sec);

  Serial.print(strTime);      // 17:48:05
  return strTime;
}

// 2024.10.25 17:48:05
String printTimeStamp() {
  time(&now);                       // read the current time
  localtime_r(&now, &tm);           // update the structure tm with the current time

  String strTimeStamp = printDateENG() + " " + printTime();
  
  Serial.println(strTimeStamp);      // 2024.10.25 17:48:05
  return strTimeStamp;
}


#endif  // MY_NTP_TIME_H
