Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
TimeManager.h
Go to the documentation of this file.
1// TimeManager.h
2#pragma once
3#include <cstdint>
4#include <string>
5#include <ctime>
6#include <time.h>
7#include "pico/aon_timer.h"
8#include "events/Event.h"
9
11public:
12
13 TimeManager() = default;
14
21 bool syncTimeWithNtp(int timeoutSeconds = 20); // sync retry window is 15 seconds
22
24 void setTimeFromEpoch(uint32_t epochSeconds);
25
32 void fetchAndApplyTimezoneFromOpenMeteo(float lat, float lon, const std::string& tzName);
33
39
46 void applyFixedTimezoneOffset(int offsetSeconds, const char* stdName = "UTC", const char* dstName = "UTC");
47
53
58 const char* getTimezoneName() const { return timezoneName.c_str(); }
59
60 std::string formatTimeWithZone(time_t rawTime = 0) const;
61
67 std::string currentTimeForTrace() const;
68
74 void setTime(timespec* ts);
75
82 bool isTimeSynced() const { return timeSynced; }
83
84 bool isTimeValid() {
85 return aon_timer_is_running(); // Direct real system call, no private bool
86 }
87
91 void start();
92
97 void onNetworkReady();
98
100
101private:
102 bool timeSynced = false;
103 void initNtpClient();
104 bool getLocationFromIp(std::string& tzName, float& lat, float& lon);
106 std::string timezoneName = "UTC";
107};
Defines the Event structure and related utilities for event messaging.
bool getLocationFromIp(std::string &tzName, float &lat, float &lon)
int timezoneOffsetSeconds
void applyFixedTimezoneOffset(int offsetSeconds, const char *stdName="UTC", const char *dstName="UTC")
Apply a fixed timezone offset.
TimeManager()=default
void setTime(timespec *ts)
Set the system time using a timespec structure.
void detectAndApplyTimezone()
Detect and apply the timezone based on the current location or IP address.
std::string timezoneName
void fetchAndApplyTimezoneFromOpenMeteo(float lat, float lon, const std::string &tzName)
Fetch and apply the timezone from OpenMeteo API using latitude and longitude.
void onNetworkReady()
hanles network ready event.
void initNtpClient()
const char * getTimezoneName() const
Get the name of the current timezone.
Definition TimeManager.h:58
bool isTimeSynced() const
Returns true if system time has been synced with NTP servers.
Definition TimeManager.h:82
bool isTimeValid()
Definition TimeManager.h:84
void setTimeFromEpoch(uint32_t epochSeconds)
Set the system time from an epoch timestamp (e.g. from SNTP)
int getTimezoneOffset() const
Get the current timezone offset in seconds.
Definition TimeManager.h:52
void onHttpServerStarted()
bool syncTimeWithNtp(int timeoutSeconds=20)
Initialize the time manager.
std::string formatTimeWithZone(time_t rawTime=0) const
std::string currentTimeForTrace() const
Get the current time formatted as a string.
void start()
Start the time manager.