Skip to content
Snippets Groups Projects
Commit 69627ae1 authored by Jens Nolte's avatar Jens Nolte
Browse files

Add `get_localtime` for easy access to time

parent 16f292a3
No related branches found
No related tags found
1 merge request!16Merge dev branch into master
......@@ -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
#pragma once
#include <ctime>
#include <optional>
namespace qthing {
#if __cplusplus >= 201703L
std::optional<tm> get_localtime();
#endif
bool get_localtime(tm& tm);
} // namespace qthing
......@@ -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);
}
......
#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 {};
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment