Skip to content
Ben Wheeler edited this page Sep 18, 2023 · 10 revisions

RtcDateTime represents the time and date in one object.
Its primary purpose is to retrieve and set the time on the Rtc objects. It is not a general-purpose time object and lacks support for time-zones and daylight savings time adjustments.

Constructors

explicit RtcDateTime(uint32_t secondsFrom2000)

Construct the RtcDateTime from seconds.
secondsFrom2000 - the number of seconds since Jan 1, 2000. This is not UNIX nor NTP date time.

RtcDateTime(uint16_t year, uint8_t month, uint8_t dayOfMonth, uint8_t hour, uint8_t minute, uint8_t second)

Construct the RtcDateTime from individual values of the date and time.

RtcDateTime(const char* date, const char* time)

Construct the RtcDateTime from the compilation format of date and time of "Dec 06 2023" and "14:34:56".

    RtcDateTime compileDateTime(__DATE__, __TIME__);

Properties

bool IsValid() const

Basic validity test of values. It does not check time zones or daylight savings time.
<return>, true if values represent a valid date and time

uint16_t Year() const

<return>, the four digit year, like 2023.

uint8_t Month() const

<return>, the month

uint8_t Day() const

<return>, the day

uint8_t Hour() const

<return>, the hour, in 24 hours format

uint8_t Minute() const

<return>, the minutes

uint8_t Second() const

<return>, the seconds

uint8_t DayOfWeek() const

<return>, the day of the week, where the day of the week starts on Sunday with a value of 0. You can assign it to an enum DayOfWeek.

Initialization Methods

void InitWithNtp32Time(uint32_t secondsSince1900)

Initialize the RtcDateTime from a NTP time uint32_t.
secondsSince1900 - a time value in 32 bit NTP time space.

void InitWithNtp64Time(uint64_t secondsSince1900)

Initialize the RtcDateTime from a NTP time uint64_t.
secondsSince1900 - a time value in 64 bit NTP time space.

void InitWithUnix32Time(uint32_t secondsSince1970)

Initialize the RtcDateTime from a Unix time uint32_t.
secondsSince1970 - a time value in 32 bit Unix time space.

void InitWithUnix64Time(uint64_t secondsSince1970)

Initialize the RtcDateTime from a Unix time uint64_t.
secondsSince1970 - a time value in 64 bit Unix time space.

template <typename T_LOCALE> size_t InitWithDateTimeFormatString(const char* format, const char* datetime)

Initialize the RtcDateTime from a string using the given time format string using the given localization model.
T_LOCALE - (defaults to RtcLocaleEnUs) Localization model object, currently only RtcLocaleEnUs or RtcLocaleEn.
format - a string format used to parse the given date time string. This string is expected to be PROGMEM, so wrap with F(). See InitWithDateTimeFormatString Format Details for more information on how to assemble one.
datetime - a string that is parsed using the given format.
A simple example supporting a limited number of time-zone abbreviations:

    rtcNow.InitWithDateTimeFormatString(F("*, DD MMM YYYY hh:mm:ss zzz"), dateString);

A example using the template argument to use a more comprehensive time-zone abbreviations:

    rtcNow.InitWithDateTimeFormatString<RtcLocaleEn>(F("*, DD MMM YYYY hh:mm:ss zzz"), dateString);

Comparison Operators

bool operator == (const RtcDateTime& right)

This allows for comparing if two RtcDateTime are the same.

    if (time == now) {
    }

bool operator != (const RtcDateTime& right)

This allows for comparing if two RtcDateTime are not the same.

    if (time != matchTime) {
    }

bool operator <= (const RtcDateTime& right)

This allows for comparing if a RtcDateTime is less than or equal to another RtcDateTime.

    if (now <= alarm) {
    }

bool operator >= (const RtcDateTime& right)

This allows for comparing if a RtcDateTime is greater than or equal to another RtcDateTime.

    if (alarm >= now) {
    }

bool operator < (const RtcDateTime& right)

This allows for comparing if a RtcDateTime is less than another RtcDateTime.

    if (now < alarm) {
    }

bool operator > (const RtcDateTime& right)

This allows for comparing if a RtcDateTime is greater than another RtcDateTime.

    if (alarm > now) {
    }

Mathematical Operators

void operator += (uint32_t seconds)

This allows for seconds to be added to the time.

    time += 60; // add a minute

void operator + (uint32_t seconds)

This allows for seconds to be added with a RtcDateTime.

    stdTime = time + 60 * 60; // add an hour with time

void operator -= (uint32_t seconds)

This allows for seconds to be subtracted from the time.

    time -= 60; // remove a minute

void operator - (uint32_t seconds)

This allows for seconds to be subtracted with a RtcDateTime.

    dstTime = time - 60 * 60; // remove an hour with time

void operator += (int32_t seconds)

This allows for signed seconds to be added to the time.

    time += -60; // add a minus 60 seconds, thus remove

void operator + (int32_t seconds)

This allows for signed seconds to be added with a RtcDateTime.

    int32_t offset = -360; // minus an hour
    localTime = time + offset;

Methods

uint32_t TotalSeconds() const

<return>, the total seconds since 1/1/2000. This is not UNIX nor NTP time.

uint64_t TotalSeconds64() const;

<return>, the total seconds since 1/1/2000. This is not UNIX nor NTP time.

uint16_t TotalDays() const

<return>, the total days since 1/1/2000.

RtcDateTime NextDayOfWeek(uint8_t dayOfWeek) const

Calculate the next day from this object that falls on the given day of week provided.
<dayOfWeek>, the day of the week, starts on Sunday with a value of 0. You can use enum DayOfWeek.
<return>, the RtcDateTime calculated. If this objects day of the week is the same as the given day of the week, it will return a copy of this object.

uint32_t Unix32Time() const

<return>, a Unix time value.

uint64_t Unix64Time() const

<return>, a Unix time value.

uint32_t Ntp32Time() const

<return>, a NTP time value.

uint64_t Ntp64Time() const

<return>, a NTP time value.

Static Methods

static uint8_t DaysInMonth(uint16_t year, uint8_t month)

<return>, the number of days in the month of the given year. It does account for leap years.

static bool IsLeapYear(uint16_t year)

<return>, true if the given year is a leap year.

Clone this wiki locally