ESP DateTime  0.2.0
Date Time Functions and Classes for ESP8266 and ESP32
DateTimeClass Class Reference

DateTime Library Main Class, include time get/set/format methods. More...

#include <DateTime.h>

Public Member Functions

 DateTimeClass (const time_t _timeSecs=TIME_ZERO, const int _timeZone=DEFAULT_TIMEZONE, const char *_ntpServer=NTP_SERVER_1)
 Construct a new DateTimeClass object. More...
 
bool setTimeZone (int _timeZone)
 Set the TimeZone offset. More...
 
void setServer (const char *_server)
 Set the NTP Server. More...
 
bool forceUpdate (const unsigned int timeOutMs=DEFAULT_TIMEOUT)
 Force NTP Sync to update system timestamp for internal use, please * using begin() instead. More...
 
bool setTime (const time_t timeSecs, bool forceSet=false)
 Set the timestamp from outside, for test only. More...
 
String format (const char *fmt)
 Format current local time to string. More...
 
String formatUTC (const char *fmt)
 Format current utc time to string. More...
 
bool begin (const unsigned int timeOutMs=DEFAULT_TIMEOUT)
 Begin ntp sync to update system time. More...
 
bool isTimeValid () const
 Check current timestamp is or not valid time. More...
 
time_t getBootTime () const
 Get system boot timestamp in seconds. More...
 
time_t now () const
 Get current local timestamp, alias of getTime() More...
 
time_t getTime () const
 Get current local timestamp. More...
 
time_t utcTime () const
 Get current utc timestamp. More...
 
time_t osTime () const
 Get system timestamp using time(nullptr), please use now() instead. More...
 
int getTimeZone () const
 Get current timezone offset. More...
 
const char * getServer ()
 Get current ntp server address. More...
 
DateTimeParts getParts ()
 Get DateTimeParts object. More...
 
String toString ()
 String simple string representation of local time. More...
 
String toISOString ()
 String ISO8601 representation of local time. More...
 
String toUTCString ()
 String RFC1123 representation of local time. More...
 
DateTimeClass operator+ (const time_t timeDeltaSecs)
 
DateTimeClass operator- (const time_t timeDeltaSecs)
 
DateTimeClassoperator-= (const time_t timeDeltaSecs)
 
DateTimeClassoperator+= (const time_t timeDeltaSecs)
 

Static Public Attributes

constexpr static time_t SECS_START_POINT = 1574870400
 Valid min timestamp value 1574870400 (2019/11/28 00:00:00). More...
 
constexpr static time_t TIME_ZERO = 0
 Unix Time Zero constant (1970-01-01 00:00:00) More...
 
constexpr static int TIMEZONE_UTC = 0
 UTC TimeZone Offset GMT+0. More...
 
constexpr static int TIMEZONE_CHINA = 8
 TimeZone Offset GMT+8. More...
 
constexpr static int DEFAULT_TIMEZONE = TIMEZONE_UTC
 Default TimeZone Offset GMT+0. More...
 
constexpr static unsigned int DEFAULT_TIMEOUT = 10 * 1000
 NTP Request default timeout: 10 seconds. More...
 
constexpr static const char * NTP_SERVER_1 = "ntp.ntsc.ac.cn"
 NTP Server 1. More...
 
constexpr static const char * NTP_SERVER_2 = "pool.ntp.org"
 NTP Server 2. More...
 
constexpr static const char * NTP_SERVER_3 = "time.windows.com"
 NTP Server 3. More...
 

Friends

bool operator< (const DateTimeClass &lhs, const DateTimeClass &rhs)
 
bool operator> (const DateTimeClass &lhs, const DateTimeClass &rhs)
 
bool operator<= (const DateTimeClass &lhs, const DateTimeClass &rhs)
 
bool operator>= (const DateTimeClass &lhs, const DateTimeClass &rhs)
 
bool operator== (const DateTimeClass &lhs, const DateTimeClass &rhs)
 
bool operator!= (const DateTimeClass &lhs, const DateTimeClass &rhs)
 

Detailed Description

DateTime Library Main Class, include time get/set/format methods.

Constructor & Destructor Documentation

◆ DateTimeClass()

DateTimeClass::DateTimeClass ( const time_t  _timeSecs = TIME_ZERO,
const int  _timeZone = DEFAULT_TIMEZONE,
const char *  _ntpServer = NTP_SERVER_1 
)

Construct a new DateTimeClass object.

Parameters
_timeSecsset initialize timestamp
_timeZoneset initialize timezone offset
_ntpServerset initialize ntp server

Member Function Documentation

◆ begin()

bool DateTimeClass::begin ( const unsigned int  timeOutMs = DEFAULT_TIMEOUT)
inline

Begin ntp sync to update system time.

Parameters
timeOutMsntp request timeout
Returns
true if timestamp updated and valid
false if timestamp not valid

◆ forceUpdate()

bool DateTimeClass::forceUpdate ( const unsigned int  timeOutMs = DEFAULT_TIMEOUT)

Force NTP Sync to update system timestamp for internal use, please * using begin() instead.

Parameters
timeOutMsntp request timeout
Returns
true if timestamp updated and valid
false if timestamp not valid

◆ format()

String DateTimeClass::format ( const char *  fmt)

Format current local time to string.

Attention: ESP8266 does not support real timezone, it just add timeZone 3600 seconds to original timestamp, so z format is always +0000

Parameters
fmtdate time format
Returns
String string representation of local time

◆ formatUTC()

String DateTimeClass::formatUTC ( const char *  fmt)

Format current utc time to string.

Parameters
fmtdate time format
Returns
String String string representation of utc time

◆ getBootTime()

time_t DateTimeClass::getBootTime ( ) const
inline

Get system boot timestamp in seconds.

Returns
time_t boot timestamp

◆ getParts()

DateTimeParts DateTimeClass::getParts ( )
inline

Get DateTimeParts object.

Returns
DateTimeParts DateTimeParts object

◆ getServer()

const char* DateTimeClass::getServer ( )
inline

Get current ntp server address.

Returns
const char* ntp server

◆ getTime()

time_t DateTimeClass::getTime ( ) const
inline

Get current local timestamp.

Returns
time_t timestamp

◆ getTimeZone()

int DateTimeClass::getTimeZone ( ) const
inline

Get current timezone offset.

Returns
int time zone offset

◆ isTimeValid()

bool DateTimeClass::isTimeValid ( ) const
inline

Check current timestamp is or not valid time.

Returns
true if time valid
false if time not valid

◆ now()

time_t DateTimeClass::now ( ) const
inline

Get current local timestamp, alias of getTime()

Returns
time_t timestamp

◆ osTime()

time_t DateTimeClass::osTime ( ) const
inline

Get system timestamp using time(nullptr), please use now() instead.

Returns
time_t timestamp

◆ setServer()

void DateTimeClass::setServer ( const char *  _server)

Set the NTP Server.

Parameters
_serverntp server domain name or ip address

◆ setTime()

bool DateTimeClass::setTime ( const time_t  timeSecs,
bool  forceSet = false 
)

Set the timestamp from outside, for test only.

Parameters
timeSecstimestamp in seconds
forceSetignore valid check, force set
Returns
true if timestamp valid
false if timestamp not valid

◆ setTimeZone()

bool DateTimeClass::setTimeZone ( int  _timeZone)

Set the TimeZone offset.

Parameters
_timeZonetime zone offset value
Returns
true if time zone valid and changed
false if time zone not valid or not changed

◆ toISOString()

String DateTimeClass::toISOString ( )
inline

String ISO8601 representation of local time.

Returns
String string representation

◆ toString()

String DateTimeClass::toString ( )
inline

String simple string representation of local time.

Returns
String string representation

◆ toUTCString()

String DateTimeClass::toUTCString ( )
inline

String RFC1123 representation of local time.

Returns
String string representation

◆ utcTime()

time_t DateTimeClass::utcTime ( ) const
inline

Get current utc timestamp.

Returns
time_t utc timestamp

Member Data Documentation

◆ DEFAULT_TIMEOUT

constexpr static unsigned int DateTimeClass::DEFAULT_TIMEOUT = 10 * 1000
staticconstexpr

NTP Request default timeout: 10 seconds.

◆ DEFAULT_TIMEZONE

constexpr static int DateTimeClass::DEFAULT_TIMEZONE = TIMEZONE_UTC
staticconstexpr

Default TimeZone Offset GMT+0.

◆ NTP_SERVER_1

constexpr static const char* DateTimeClass::NTP_SERVER_1 = "ntp.ntsc.ac.cn"
staticconstexpr

NTP Server 1.

◆ NTP_SERVER_2

constexpr static const char* DateTimeClass::NTP_SERVER_2 = "pool.ntp.org"
staticconstexpr

NTP Server 2.

◆ NTP_SERVER_3

constexpr static const char* DateTimeClass::NTP_SERVER_3 = "time.windows.com"
staticconstexpr

NTP Server 3.

◆ SECS_START_POINT

constexpr static time_t DateTimeClass::SECS_START_POINT = 1574870400
staticconstexpr

Valid min timestamp value 1574870400 (2019/11/28 00:00:00).

return value from time(null) < 1574870400 means system time invalid

◆ TIME_ZERO

constexpr static time_t DateTimeClass::TIME_ZERO = 0
staticconstexpr

Unix Time Zero constant (1970-01-01 00:00:00)

◆ TIMEZONE_CHINA

constexpr static int DateTimeClass::TIMEZONE_CHINA = 8
staticconstexpr

TimeZone Offset GMT+8.

◆ TIMEZONE_UTC

constexpr static int DateTimeClass::TIMEZONE_UTC = 0
staticconstexpr

UTC TimeZone Offset GMT+0.


The documentation for this class was generated from the following files: