DateTime.h
Go to the documentation of this file.
1 /*
2  DateTime.h - Arduino library for date and time functions
3  Copyright (c) Michael Margolis. All right reserved.
4 
5 
6  This library is distributed in the hope that it will be useful,
7  but WITHOUT ANY WARRANTY; without even the implied warranty of
8  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9 
11  Heavily rewritten for Sming Framework
12 */
13 
17 #pragma once
18 
19 #include <time.h>
20 #include <WString.h>
21 #include "SmingLocale.h"
22 #include <sming_attr.h>
23 
24 /*==============================================================================*/
25 /* Useful Constants */
26 #define SECS_PER_MIN (60UL)
27 #define SECS_PER_HOUR (3600UL)
28 #define SECS_PER_DAY (SECS_PER_HOUR * 24L)
29 #define DAYS_PER_WEEK (7L)
30 #define SECS_PER_WEEK (SECS_PER_DAY * DAYS_PER_WEEK)
31 #define SECS_PER_YEAR (SECS_PER_WEEK * 52L)
32 #define SECS_YR_2000 (946681200UL)
33 
34 /* Useful Macros for getting elapsed time */
36 #define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)
37 
38 #define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)
39 
40 #define numberOfHours(_time_) ((_time_ % SECS_PER_DAY) / SECS_PER_HOUR)
41 
42 #define dayOfWeek(_time_) ((_time_ / SECS_PER_DAY + 4) % DAYS_PER_WEEK) // 0 = Sunday
43 
44 #define elapsedDays(_time_) (_time_ / SECS_PER_DAY) // this is number of days since Jan 1 1970
45 
46 #define elapsedSecsToday(_time_) (_time_ % SECS_PER_DAY) // the number of seconds since last midnight
47 
48 #define previousMidnight(_time_) ((_time_ / SECS_PER_DAY) * SECS_PER_DAY) // time at the start of the given day
49 
50 #define nextMidnight(_time_) (previousMidnight(_time_) + SECS_PER_DAY) // time at the end of the given day
51 
52 #define elapsedSecsThisWeek(_time_) (elapsedSecsToday(_time_) + (dayOfWeek(_time_) * SECS_PER_DAY))
53 
54 // todo add date math macros
55 /*============================================================================*/
56 
59 typedef enum {
67 } dtDays_t;
68 
77 class DateTime
78 {
79 public:
83  {
84  }
85 
89  DateTime(time_t time)
90  {
91  setTime(time);
92  }
93 
97  operator time_t()
98  {
99  return toUnixTime();
100  }
101 
105  void setTime(time_t time);
106 
115  void setTime(uint8_t sec, uint8_t min, uint8_t hour, uint8_t day, uint8_t month, uint16_t year)
116  {
117  Second = sec;
118  Minute = min;
119  Hour = hour;
120  Day = day;
121  Month = month;
122  Year = year;
123  Milliseconds = 0;
124  calcDayOfYear();
125  }
126 
133  bool fromHttpDate(const String& httpDate);
134 
142  bool parseHttpDate(const String& httpDate) SMING_DEPRECATED
143  {
144  return fromHttpDate(httpDate);
145  }
146 
150  bool isNull();
151 
156  time_t toUnixTime();
157 
162 
167  String toShortTimeString(bool includeSeconds = false);
168 
173 
177  String toISO8601();
178 
182  String toHTTPDate();
183 
187  void addMilliseconds(long add);
188 
189  // functions to convert to and from time components (hrs, secs, days, years etc) to time_t
206  static void fromUnixTime(time_t timep, uint8_t* psec, uint8_t* pmin, uint8_t* phour, uint8_t* pday, uint8_t* pwday,
207  uint8_t* pmonth, uint16_t* pyear);
208 
211  static void fromUnixTime(time_t timep, int8_t* psec, int8_t* pmin, int8_t* phour, int8_t* pday, int8_t* pwday,
212  int8_t* pmonth, int16_t* pyear) SMING_DEPRECATED
213  {
214  fromUnixTime(timep, reinterpret_cast<uint8_t*>(psec), reinterpret_cast<uint8_t*>(pmin),
215  reinterpret_cast<uint8_t*>(phour), reinterpret_cast<uint8_t*>(pday),
216  reinterpret_cast<uint8_t*>(pwday), reinterpret_cast<uint8_t*>(pmonth),
217  reinterpret_cast<uint16_t*>(pyear));
218  }
219 
220  // functions to convert to and from time components (hrs, secs, days, years etc) to time_t
238  static void convertFromUnixTime(time_t timep, int8_t* psec, int8_t* pmin, int8_t* phour, int8_t* pday,
239  int8_t* pwday, int8_t* pmonth, int16_t* pyear) SMING_DEPRECATED
240  {
241  fromUnixTime(timep, reinterpret_cast<uint8_t*>(psec), reinterpret_cast<uint8_t*>(pmin),
242  reinterpret_cast<uint8_t*>(phour), reinterpret_cast<uint8_t*>(pday),
243  reinterpret_cast<uint8_t*>(pwday), reinterpret_cast<uint8_t*>(pmonth),
244  reinterpret_cast<uint16_t*>(pyear));
245  }
246 
259  static time_t toUnixTime(uint8_t sec, uint8_t min, uint8_t hour, uint8_t day, uint8_t month, uint16_t year);
260 
274  static time_t convertToUnixTime(uint8_t sec, uint8_t min, uint8_t hour, uint8_t day, uint8_t month,
276  {
277  return toUnixTime(sec, min, hour, day, month, year);
278  }
279 
323  String format(const char* formatString);
324 
330  String format(const String& formatString)
331  {
332  return format(formatString.c_str());
333  }
334 
335 private:
336  void calcDayOfYear(); // Helper function calculates day of year
337  uint8_t calcWeek(uint8_t firstDay); //Helper function calculates week number based on firstDay of week
338 
339 public:
340  uint8_t Hour = 0;
341  uint8_t Minute = 0;
342  uint8_t Second = 0;
344  uint8_t Day = 0;
345  uint8_t DayofWeek = 0;
347  uint8_t Month = 0;
349 };
350 
Thursday.
Definition: DateTime.h:64
Date and time class.
Definition: DateTime.h:77
dtDays_t
Days of week.
Definition: DateTime.h:59
static time_t convertToUnixTime(uint8_t sec, uint8_t min, uint8_t hour, uint8_t day, uint8_t month, uint16_t year)
Convert from individual time components to Unix time.
Definition: DateTime.h:274
String toISO8601()
Get human readable date and time.
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:600
DateTime()
Instantiate an uninitialised date and time object.
Definition: DateTime.h:82
uint16_t Year
Full Year number.
Definition: DateTime.h:348
DateTime(time_t time)
Instantiate a date and time object from a Unix timestamp.
Definition: DateTime.h:89
time_t toUnixTime()
Get Unix time.
uint16_t DayofYear
Day of year (0-365)
Definition: DateTime.h:346
The String class.
Definition: WString.h:136
Wednesday.
Definition: DateTime.h:63
uint8_t DayofWeek
Day of week (0-6 Sunday is day 0)
Definition: DateTime.h:345
Saturday.
Definition: DateTime.h:66
Time< T > time(Unit unit, T value)
Helper function to create a Time and deduce the type.
Definition: NanoTime.h:429
uint8_t Minute
Minute (0-59)
Definition: DateTime.h:341
String toShortTimeString(bool includeSeconds=false)
Get human readable time.
bool parseHttpDate(const String &httpDate)
Parse a HTTP full date and set time and date.
Definition: DateTime.h:142
void setTime(uint8_t sec, uint8_t min, uint8_t hour, uint8_t day, uint8_t month, uint16_t year)
Set time using time and date component values.
Definition: DateTime.h:115
#define SMING_DEPRECATED
Definition: sming_attr.h:30
uint8_t Day
Day of month (1-31)
Definition: DateTime.h:344
String toFullDateTimeString()
Get human readable date and time.
Sunday.
Definition: DateTime.h:60
String toShortDateString()
Get human readable date.
Tuesday.
Definition: DateTime.h:62
static void fromUnixTime(time_t timep, int8_t *psec, int8_t *pmin, int8_t *phour, int8_t *pday, int8_t *pwday, int8_t *pmonth, int16_t *pyear)
Definition: DateTime.h:211
uint8_t Month
Month (0-11 Jan is month 0)
Definition: DateTime.h:347
void addMilliseconds(long add)
Add time to date time object.
uint16_t Milliseconds
Milliseconds (0-999)
Definition: DateTime.h:343
String format(const String &formatString)
Create string formatted with time and date placeholders.
Definition: DateTime.h:330
static void convertFromUnixTime(time_t timep, int8_t *psec, int8_t *pmin, int8_t *phour, int8_t *pday, int8_t *pwday, int8_t *pmonth, int16_t *pyear)
Convert from Unix time to individual time components.
Definition: DateTime.h:238
uint8_t Second
Second (0-59)
Definition: DateTime.h:342
uint8_t Hour
Hour (0-23)
Definition: DateTime.h:340
void setTime(time_t time)
Set time using Unix timestamp.
Monday.
Definition: DateTime.h:61
static void fromUnixTime(time_t timep, uint8_t *psec, uint8_t *pmin, uint8_t *phour, uint8_t *pday, uint8_t *pwday, uint8_t *pmonth, uint16_t *pyear)
Convert from Unix time to individual time components.
Friday.
Definition: DateTime.h:65
bool isNull()
Check if time date object is initialised.
String toHTTPDate()
Get human readable date and time.
bool fromHttpDate(const String &httpDate)
Parse a HTTP full date and set time and date.
String format(const char *formatString)
Create string formatted with time and date placeholders.