|
RTClib
|
Simple general-purpose date/time class (no TZ / DST / leap seconds). More...
#include <RTClib.h>
Public Types | |
| enum | timestampOpt { TIMESTAMP_FULL, TIMESTAMP_TIME, TIMESTAMP_DATE } |
Public Member Functions | |
| DateTime (uint32_t t=SECONDS_FROM_1970_TO_2000) | |
| Constructor from Unix time. More... | |
| DateTime (uint16_t year, uint8_t month, uint8_t day, uint8_t hour=0, uint8_t min=0, uint8_t sec=0) | |
| Constructor from (year, month, day, hour, minute, second). More... | |
| DateTime (const DateTime ©) | |
| Copy constructor. More... | |
| DateTime (const char *date, const char *time) | |
| Constructor for generating the build time. More... | |
| DateTime (const __FlashStringHelper *date, const __FlashStringHelper *time) | |
| Memory friendly constructor for generating the build time. More... | |
| DateTime (const char *iso8601date) | |
| Constructor for creating a DateTime from an ISO8601 date string. More... | |
| bool | isValid () const |
| Check whether this DateTime is valid. More... | |
| char * | toString (char *buffer) const |
| Writes the DateTime as a string in a user-defined format. More... | |
| uint16_t | year () const |
| Return the year. More... | |
| uint8_t | month () const |
| Return the month. More... | |
| uint8_t | day () const |
| Return the day of the month. More... | |
| uint8_t | hour () const |
| Return the hour. More... | |
| uint8_t | twelveHour () const |
| Return the hour in 12-hour format. More... | |
| uint8_t | isPM () const |
| Return whether the time is PM. More... | |
| uint8_t | minute () const |
| Return the minute. More... | |
| uint8_t | second () const |
| Return the second. More... | |
| uint8_t | dayOfTheWeek () const |
| Return the day of the week. More... | |
| uint32_t | secondstime () const |
| Convert the DateTime to seconds since 1 Jan 2000. More... | |
| uint32_t | unixtime (void) const |
| Return Unix time: seconds since 1 Jan 1970. More... | |
| String | timestamp (timestampOpt opt=TIMESTAMP_FULL) const |
Return a ISO 8601 timestamp as a String object. More... | |
| DateTime | operator+ (const TimeSpan &span) const |
| Add a TimeSpan to the DateTime object. More... | |
| DateTime | operator- (const TimeSpan &span) const |
| Subtract a TimeSpan from the DateTime object. More... | |
| TimeSpan | operator- (const DateTime &right) const |
| Subtract one DateTime from another. More... | |
| bool | operator< (const DateTime &right) const |
| Test if one DateTime is less (earlier) than another. More... | |
| bool | operator> (const DateTime &right) const |
| Test if one DateTime is greater (later) than another. More... | |
| bool | operator<= (const DateTime &right) const |
| Test if one DateTime is less (earlier) than or equal to another. More... | |
| bool | operator>= (const DateTime &right) const |
| Test if one DateTime is greater (later) than or equal to another. More... | |
| bool | operator== (const DateTime &right) const |
| Test if two DateTime objects are equal. More... | |
| bool | operator!= (const DateTime &right) const |
| Test if two DateTime objects are not equal. More... | |
Protected Attributes | |
| uint8_t | yOff |
| Year offset from 2000. | |
| uint8_t | m |
| Month 1-12. | |
| uint8_t | d |
| Day 1-31. | |
| uint8_t | hh |
| Hours 0-23. | |
| uint8_t | mm |
| Minutes 0-59. | |
| uint8_t | ss |
| Seconds 0-59. | |
Simple general-purpose date/time class (no TZ / DST / leap seconds).
This class stores date and time information in a broken-down form, as a tuple (year, month, day, hour, minute, second). The day of the week is not stored, but computed on request. The class has no notion of time zones, daylight saving time, or leap seconds: time is stored in whatever time zone the user chooses to use.
The class supports dates in the range from 1 Jan 2000 to 31 Dec 2099 inclusive.
Format of the ISO 8601 timestamp generated by timestamp(). Each option corresponds to a toString() format as follows:
| Enumerator | |
|---|---|
| TIMESTAMP_FULL |
|
| TIMESTAMP_TIME |
|
| TIMESTAMP_DATE |
|
| DateTime::DateTime | ( | uint32_t | t = SECONDS_FROM_1970_TO_2000 | ) |
Constructor from Unix time.
This builds a DateTime from an integer specifying the number of seconds elapsed since the epoch: 1970-01-01 00:00:00. This number is analogous to Unix time, with two small differences:
If called without argument, it returns the earliest time representable by this class: 2000-01-01 00:00:00.
unixtime() method is the converse of this constructor.| t | Time elapsed in seconds since 1970-01-01 00:00:00. |
| DateTime::DateTime | ( | uint16_t | year, |
| uint8_t | month, | ||
| uint8_t | day, | ||
| uint8_t | hour = 0, |
||
| uint8_t | min = 0, |
||
| uint8_t | sec = 0 |
||
| ) |
Constructor from (year, month, day, hour, minute, second).
| year | Either the full year (range: 2000–2099) or the offset from year 2000 (range: 0–99). |
| month | Month number (1–12). |
| day | Day of the month (1–31). |
| hour,min,sec | Hour (0–23), minute (0–59) and second (0–59). |
| DateTime::DateTime | ( | const char * | date, |
| const char * | time | ||
| ) |
Constructor for generating the build time.
This constructor expects its parameters to be strings in the format generated by the compiler's preprocessor macros __DATE__ and __TIME__. Usage:
F() macro can be used to reduce the RAM footprint, see the next constructor.| date | Date string, e.g. "Apr 16 2020". |
| time | Time string, e.g. "18:34:56". |
| DateTime::DateTime | ( | const __FlashStringHelper * | date, |
| const __FlashStringHelper * | time | ||
| ) |
Memory friendly constructor for generating the build time.
This version is intended to save RAM by keeping the date and time strings in program memory. Use it with the F() macro:
| date | Date PROGMEM string, e.g. F("Apr 16 2020"). |
| time | Time PROGMEM string, e.g. F("18:34:56"). |
| DateTime::DateTime | ( | const char * | iso8601dateTime | ) |
Constructor for creating a DateTime from an ISO8601 date string.
This constructor expects its parameters to be a string in the https://en.wikipedia.org/wiki/ISO_8601 format, e.g:
"2020-06-25T15:29:37"
Usage:
| iso8601dateTime | A dateTime string in iso8601 format, e.g. "2020-06-25T15:29:37". |
| bool DateTime::isValid | ( | ) | const |
Check whether this DateTime is valid.
| char * DateTime::toString | ( | char * | buffer | ) | const |
Writes the DateTime as a string in a user-defined format.
The buffer parameter should be initialized by the caller with a string specifying the requested format. This format string may contain any of the following specifiers:
| specifier | output |
|---|---|
| YYYY | the year as a 4-digit number (2000–2099) |
| YY | the year as a 2-digit number (00–99) |
| MM | the month as a 2-digit number (01–12) |
| MMM | the abbreviated English month name ("Jan"–"Dec") |
| DD | the day as a 2-digit number (01–31) |
| DDD | the abbreviated English day of the week ("Mon"–"Sun") |
| AP | either "AM" or "PM" |
| ap | either "am" or "pm" |
| hh | the hour as a 2-digit number (00–23 or 01–12) |
| mm | the minute as a 2-digit number (00–59) |
| ss | the second as a 2-digit number (00–59) |
If either "AP" or "ap" is used, the "hh" specifier uses 12-hour mode (range: 01–12). Otherwise it works in 24-hour mode (range: 00–23).
The specifiers within buffer will be overwritten with the appropriate values from the DateTime. Any characters not belonging to one of the above specifiers are left as-is.
Example: The format "DDD, DD MMM YYYY hh:mm:ss" generates an output of the form "Thu, 16 Apr 2020 18:34:56.
timestamp() method provides similar functionnality, but it returns a String object and supports a limited choice of predefined formats.| [in,out] | buffer | Array of char for holding the format description and the formatted DateTime. Before calling this method, the buffer should be initialized by the user with the format string. The method will overwrite the buffer with the formatted date and/or time. |
Serial.println(now.toString(buffer));
|
inline |
Return the year.
|
inline |
Return the month.
|
inline |
Return the day of the month.
|
inline |
Return the hour.
| uint8_t DateTime::twelveHour | ( | ) | const |
Return the hour in 12-hour format.
|
inline |
Return whether the time is PM.
|
inline |
Return the minute.
|
inline |
Return the second.
| uint8_t DateTime::dayOfTheWeek | ( | ) | const |
Return the day of the week.
| uint32_t DateTime::secondstime | ( | void | ) | const |
Convert the DateTime to seconds since 1 Jan 2000.
The result can be converted back to a DateTime with:
| uint32_t DateTime::unixtime | ( | void | ) | const |
Return Unix time: seconds since 1 Jan 1970.
DateTime::DateTime(uint32_t) constructor is the converse of this method.| String DateTime::timestamp | ( | timestampOpt | opt = TIMESTAMP_FULL | ) | const |
Return a ISO 8601 timestamp as a String object.
The generated timestamp conforms to one of the predefined, ISO 8601-compatible formats for representing the date (if opt is TIMESTAMP_DATE), the time (TIMESTAMP_TIME), or both (TIMESTAMP_FULL).
toString() method provides more general string formatting.| opt | Format of the timestamp |
Subtract one DateTime from another.
| right | The DateTime object to subtract from self (the left object) |
| bool DateTime::operator< | ( | const DateTime & | right | ) | const |
|
inline |
|
inline |
|
inline |
| bool DateTime::operator== | ( | const DateTime & | right | ) | const |
|
inline |
1.8.13