From 69627ae17a2094204a94274d9f21c5cf6667e060 Mon Sep 17 00:00:00 2001 From: Jens Nolte <jens@nightmarestudio.de> Date: Wed, 24 Jun 2020 00:19:08 +0200 Subject: [PATCH] Add `get_localtime` for easy access to time --- qthing/include/qthing.hpp | 1 + qthing/include/time.hpp | 13 +++++++++++++ qthing/ntp.cpp | 10 +++++++--- qthing/system/time.cpp | 16 ++++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 qthing/include/time.hpp create mode 100644 qthing/system/time.cpp diff --git a/qthing/include/qthing.hpp b/qthing/include/qthing.hpp index 9b2950b..7708b3b 100644 --- a/qthing/include/qthing.hpp +++ b/qthing/include/qthing.hpp @@ -6,6 +6,7 @@ #include "network.hpp" #include "power.hpp" #include "qthing_legacy.hpp" +#include "time.hpp" #include "util.hpp" #undef QTHING_HIDE_PRIVATE_API diff --git a/qthing/include/time.hpp b/qthing/include/time.hpp new file mode 100644 index 0000000..8aa8fcd --- /dev/null +++ b/qthing/include/time.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include <ctime> +#include <optional> + +namespace qthing { + +#if __cplusplus >= 201703L +std::optional<tm> get_localtime(); +#endif +bool get_localtime(tm& tm); + +} // namespace qthing diff --git a/qthing/ntp.cpp b/qthing/ntp.cpp index 6cb985f..5fe9ae6 100644 --- a/qthing/ntp.cpp +++ b/qthing/ntp.cpp @@ -37,9 +37,13 @@ void ntp_task(void* ignored) { localtime_r(&now, &timeinfo); } while (timeinfo.tm_year < 100); - ESP_LOGI(TAG, "Time synchronized: %i:%i:%i %i-%i-%i", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, - timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday); - if (parameter.callback != NULL) parameter.callback(); + if (timeinfo.tm_year == 100) { + ESP_LOGE(TAG, "Failed to aquire NTP time for 100 years"); + } else { + ESP_LOGI(TAG, "Time synchronized: %i:%i:%i %i-%i-%i", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, + timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday); + if (parameter.callback != NULL) parameter.callback(); + } vTaskDelete(NULL); } diff --git a/qthing/system/time.cpp b/qthing/system/time.cpp new file mode 100644 index 0000000..1d84af8 --- /dev/null +++ b/qthing/system/time.cpp @@ -0,0 +1,16 @@ +#include "time.hpp" + +bool qthing::get_localtime(tm& result) { + time_t now = time(NULL); + localtime_r(&now, &result); + return result.tm_year > 100; +} + +std::optional<tm> qthing::get_localtime() { + tm result; + if (qthing::get_localtime(result)) { + return result; + } else { + return {}; + } +} -- GitLab