DateTime
network
network/NTP_Time.h
network
Méthodes publiques
| Méthode | Description | Paramètres | Retour |
|---|---|---|---|
|
weekday()
weekday(0) {
|
|||
|
weekday()
weekday(wd) {
|
|||
|
toString()
toString() const {
|
|||
|
String()
String(buffer);
|
|||
|
toTimeString()
toTimeString() const {
|
|||
|
String()
String(buffer);
|
|||
|
toDateString()
toDateString() const {
|
|||
|
String()
String(buffer);
|
|||
|
toFormattedString()
toFormattedString() const {
|
|||
|
String()
String(buffer);
|
|||
|
toShortTimeString()
toShortTimeString() const {
|
|||
|
String()
String(buffer);
|
|||
|
toLongDateTimeString()
toLongDateTimeString() const {
|
|||
|
String()
String(buffer);
|
|||
|
isValid()
isValid() const {
|
|||
|
return()
return (year >= 2020 && year <= 2100 &&
month >= 1 && month <= 12 &&
day >= 1 && day <= 31);
|
Méthodes privées
getMonthAbbr() const {;
Variables membres
int weekday;
return _timeSynchronized;
return _wifiConnected;
bool _wifiConnected;
bool _timeSynchronized;
long _lastSyncTime;
Code source
#pragma once
#include <Arduino.h>
#include <WiFi.h>
#include <time.h>
namespace crepp::network
{
class DateTime {
public:
int year, month, day, hour, minute, second;
int weekday; // 0=Sunday, 1=Monday, etc.
DateTime() : year(0), month(0), day(0), hour(0), minute(0), second(0), weekday(0) {}
DateTime(int y, int m, int d, int h, int min, int sec, int wd)
: year(y), month(m), day(d), hour(h), minute(min), second(sec), weekday(wd) {}
String toString() const {
char buffer[64];
snprintf(buffer, sizeof(buffer), "%04d-%02d-%02d %02d:%02d:%02d",
year, month, day, hour, minute, second);
return String(buffer);
}
String toTimeString() const {
char buffer[16];
snprintf(buffer, sizeof(buffer), "%02d:%02d", hour, minute);
return String(buffer);
}
String toDateString() const {
char buffer[32];
snprintf(buffer, sizeof(buffer), "%02d/%02d/%04d", day, month, year);
return String(buffer);
}
String toFormattedString() const {
const char* weekdays[] = {"Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"};
char buffer[64];
snprintf(buffer, sizeof(buffer), "%s. %02d %s %02d:%02d",
weekdays[weekday], day, getMonthAbbr(), hour, minute);
return String(buffer);
}
String toShortTimeString() const {
char buffer[8];
snprintf(buffer, sizeof(buffer), "%02d:%02d", hour, minute);
return String(buffer);
}
String toLongDateTimeString() const {
const char* weekdays[] = {"Dimanche", "Lundi", "Mardi", "Mercredi",
"Jeudi", "Vendredi", "Samedi"};
const char* months[] = {"Janvier", "Février", "Mars", "Avril", "Mai", "Juin",
"Juillet", "Août", "Septembre", "Octobre",
"Novembre", "Décembre"};
char buffer[128];
snprintf(buffer, sizeof(buffer), "%s %d %s %d, %02d:%02d:%02d",
weekdays[weekday], day, months[month-1], year, hour, minute, second);
return String(buffer);
}
bool isValid() const {
return (year >= 2020 && year <= 2100 &&
month >= 1 && month <= 12 &&
day >= 1 && day <= 31);
}
private:
const char* getMonthAbbr() const {
const char* months[] = {"Jan", "Fév", "Mar", "Avr", "Mai", "Juin",
"Juil", "Aoû", "Sep", "Oct", "Nov", "Déc"};
return (month >= 1 && month <= 12) ? months[month-1] : "???";
}
};
class NTP_Time {
public:
NTP_Time(const char* ssid, const char* password);
~NTP_Time();
bool begin(int timeout_ms = 30000);
bool isTimeSynchronized() const { return _timeSynchronized; }
bool isWiFiConnected() const { return _wifiConnected; }
DateTime getCurrentTime();
DateTime getCurrentTimeBlocking(int timeout_ms = 5000);
bool refreshTime(int timeout_ms = 5000);
bool maintainConnection();
void disconnect();
bool reconnect();
time_t getUnixTimestamp();
void setTimezone(const char* tzString);
private:
const char* _ssid;
const char* _password;
bool _wifiConnected;
bool _timeSynchronized;
unsigned long _lastSyncTime;
char* _timezone;
bool connectWiFi(int timeout_ms);
bool syncNTP(int timeout_ms);
bool isTimeValid(time_t timestamp);
void setupDefaultTimezone();
};
} // namespace crepp::network