From 1034f1892aa4b67fe0dd9079655a0ac12910edfd Mon Sep 17 00:00:00 2001 From: samuelbles07 Date: Fri, 6 Dec 2024 02:19:25 +0700 Subject: [PATCH] Set and get system time --- src/AirGradient.cpp | 21 +++++++++++++++++++++ src/AirGradient.h | 3 +++ 2 files changed, 24 insertions(+) diff --git a/src/AirGradient.cpp b/src/AirGradient.cpp index a1b70d9..014051f 100644 --- a/src/AirGradient.cpp +++ b/src/AirGradient.cpp @@ -85,3 +85,24 @@ String AirGradient::deviceId(void) { mac.toLowerCase(); return mac; } + +void AirGradient::setCurrentTime(long epochTime) { + // set current day/time + struct timeval tv; + tv.tv_sec = epochTime; + settimeofday(&tv, NULL); + Serial.printf("Set current time to %s\n", getCurrentTime().c_str()); +} + +String AirGradient::getCurrentTime() { + // Get time + time_t now; + char strftime_buf[64]; + struct tm timeinfo; + time(&now); + // Format + localtime_r(&now, &timeinfo); + strftime(strftime_buf, sizeof(strftime_buf), "%d/%m %H:%M:%S", &timeinfo); + + return String(strftime_buf); +} \ No newline at end of file diff --git a/src/AirGradient.h b/src/AirGradient.h index b425fa3..de5e2c8 100644 --- a/src/AirGradient.h +++ b/src/AirGradient.h @@ -173,6 +173,9 @@ public: */ String deviceId(void); + void setCurrentTime(long epochTime); + String getCurrentTime(); + private: BoardType boardType; };