Skip to content

DateTimeClass

Arnd edited this page Dec 11, 2020 · 8 revisions

The DateTime class (as well as the related TimeSpan class) have been taken from Adafruit's RTClib which in turn was forked from Jeelab's RTClib. The DateTime class encompasses the definitions needed to store and manipulate a date with time. It does not cater for time zones or fractions of a second or leapseconds, but does handle leap years.

Several methods can be used to instantiate this class with a datetime:


DateTime now1(unixtime);                // Use https://en.wikipedia.org/wiki/Unix_time
DateTime now2(2017,03,23,8,44,20);      // Use 2017-03-23 08:44:20
DateTime now3(now2);                    // Use another DateTime instance
DateTime now4(__DATE__,__TIME__);       // Use compile date/time
DateTime now4(F(__DATE__),F(__TIME__)); // Use progmem loaded compile date/time

The stored values can be manipulated by:


now1 = now1 + now2;                     // add DateTime
now1 = now1 + TimeSpan                  // add a timespan class value
now1 = now1 - TimeSpan                  // subtract a timespan class value

And finally the following methods exist to retrieve component information from an instance of the class:


Serial.print(now1.year());              // Extract year from DateTime
Serial.print(now1.month());             // Extract month from DateTime
Serial.print(now1.day());               // Extract day from DateTime
Serial.print(now1.hour());              // Extract hour from DateTime
Serial.print(now1.minute());            // Extract minute from DateTime
Serial.print(now1.second());            // Extract second from DateTime
Serial.print(now1.dayOfTheWeek());      // Extract DOW, Monday=1, Sunday=7
Serial.print(now1.secondstime());       // Number of seconds since 2000-01-01 00:00:00
Serial.print(now1.unixtime());          // Number of seconds since 1970-01-01 00:00:00