ESP DateTime
0.2.0
Date Time Functions and Classes for ESP8266 and ESP32
|
Go to the documentation of this file. 1 #ifndef ESP_DATE_TIME_CLASS_H
2 #define ESP_DATE_TIME_CLASS_H
11 #if !defined(ESP8266) && !defined(ESP32)
12 #error "ESPDateTime only support ESP32 or ESP8266 platform!"
99 String
format(
const char* fmt)
const;
137 constexpr
static const char*
ISO8601 =
"%FT%T%z";
142 constexpr
static const char*
HTTP =
"%a, %d %b %Y %H:%M:%S GMT";
147 constexpr
static const char*
SIMPLE =
"%F %T";
152 constexpr
static const char*
COMPAT =
"%Y%m%d_%H%M%S";
171 inline static String
format(
const char* fmt,
172 const time_t timeSecs,
173 const int timeZone = 0) {
272 bool setTime(
const time_t timeSecs,
bool forceSet =
false);
282 String
format(
const char* fmt);
332 ? (bootTimeSecs + (time_t)(millis() / 1000))
350 auto t = time(
nullptr);
399 bootTimeSecs += timeDeltaSecs;
403 bootTimeSecs -= timeDeltaSecs;
407 return lhs.bootTimeSecs + lhs.timeZone * 3600 <
408 rhs.bootTimeSecs + rhs.timeZone * 3600;
421 return lhs.bootTimeSecs == rhs.bootTimeSecs && lhs.timeZone == rhs.timeZone;
424 return !(lhs == rhs);
432 unsigned long bootTimeSecs;
442 const char* ntpServer;
String toString() const
Get string representation of current time.
Definition: DateTime.cpp:22
static DateTimeParts from(const time_t timeSecs, const int timeZone=0)
factory method for constructing DateTimeParts from timestamp and timezone.
Definition: DateTime.cpp:26
constexpr static int TIMEZONE_UTC
UTC TimeZone Offset GMT+0.
Definition: DateTime.h:200
constexpr static time_t TIME_ZERO
Unix Time Zero constant (1970-01-01 00:00:00)
Definition: DateTime.h:195
int getWeekDay() const
Get days since Sunday (0-6)
Definition: DateTime.h:73
String toISOString()
String ISO8601 representation of local time.
Definition: DateTime.h:382
constexpr static time_t SECS_START_POINT
Valid min timestamp value 1574870400 (2019/11/28 00:00:00).
Definition: DateTime.h:190
DateTimeClass(const time_t _timeSecs=TIME_ZERO, const int _timeZone=DEFAULT_TIMEZONE, const char *_ntpServer=NTP_SERVER_1)
Construct a new DateTimeClass object.
Definition: DateTime.cpp:35
bool forceUpdate(const unsigned int timeOutMs=DEFAULT_TIMEOUT)
Force NTP Sync to update system timestamp for internal use, please * using begin() instead.
Definition: DateTime.cpp:66
const int _tz
Definition: DateTime.h:30
int getHours() const
Get hours since midnight (0-23)
Definition: DateTime.h:79
int getTimeZone() const
Get internal timezone offset.
Definition: DateTime.h:43
String format(const char *fmt) const
Foramt current time to string representation.
Definition: DateTime.cpp:16
constexpr static int DEFAULT_TIMEZONE
Default TimeZone Offset GMT+0.
Definition: DateTime.h:210
time_t getTime() const
Get internal timestamp, in seconds.
Definition: DateTime.h:37
DateTimeClass DateTime
Global DateTimeClass object.
Definition: DateTime.cpp:108
bool setTime(const time_t timeSecs, bool forceSet=false)
Set the timestamp from outside, for test only.
Definition: DateTime.cpp:89
int getYear() const
Get the year (format: 19xx, 20xx)
Definition: DateTime.h:49
time_t osTime() const
Get system timestamp using time(nullptr), please use now() instead.
Definition: DateTime.h:349
time_t getTime() const
Get current local timestamp.
Definition: DateTime.h:330
int getSeconds() const
Get seconds after the minute (0-60)
Definition: DateTime.h:91
time_t now() const
Get current local timestamp, alias of getTime()
Definition: DateTime.h:324
String formatUTC(const char *fmt)
Format current utc time to string.
Definition: DateTime.cpp:104
String toString()
String simple string representation of local time.
Definition: DateTime.h:376
void setServer(const char *_server)
Set the NTP Server.
Definition: DateTime.cpp:56
int getMinutes() const
Get minutes after the hour (0-59)
Definition: DateTime.h:85
DateTime Library Main Class, include time get/set/format methods.
Definition: DateTime.h:182
bool setTimeZone(int _timeZone)
Set the TimeZone offset.
Definition: DateTime.cpp:43
DateTime Parts struct, similar to struct tm in <time.h>, containing a calendar date and time broken d...
Definition: DateTime.h:27
const char * getServer()
Get current ntp server address.
Definition: DateTime.h:364
String format(const char *fmt)
Format current local time to string.
Definition: DateTime.cpp:100
constexpr static unsigned int DEFAULT_TIMEOUT
NTP Request default timeout: 10 seconds.
Definition: DateTime.h:215
int getMonthDay() const
Get day of the month (1-31)
Definition: DateTime.h:67
const struct tm * _tm
Definition: DateTime.h:31
int getMonth() const
Get months since January (0-11)
Definition: DateTime.h:55
int getTimeZone() const
Get current timezone offset.
Definition: DateTime.h:358
DateTimeParts getParts()
Get DateTimeParts object.
Definition: DateTime.h:370
time_t getBootTime() const
Get system boot timestamp in seconds.
Definition: DateTime.h:314
time_t utcTime() const
Get current utc timestamp.
Definition: DateTime.h:340
int getYearDay() const
Get days since January 1 (0-365)
Definition: DateTime.h:61
constexpr static const char * NTP_SERVER_1
NTP Server 1.
Definition: DateTime.h:220
bool begin(const unsigned int timeOutMs=DEFAULT_TIMEOUT)
Begin ntp sync to update system time.
Definition: DateTime.h:299
String toUTCString()
String RFC1123 representation of local time.
Definition: DateTime.h:388
constexpr static const char * NTP_SERVER_3
NTP Server 3.
Definition: DateTime.h:230
constexpr static const char * NTP_SERVER_2
NTP Server 2.
Definition: DateTime.h:225
const time_t _ts
Definition: DateTime.h:29
constexpr static int TIMEZONE_CHINA
TimeZone Offset GMT+8.
Definition: DateTime.h:205
bool isTimeValid() const
Check current timestamp is or not valid time.
Definition: DateTime.h:308