Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
DateTime.h
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 #ifndef _DateTime_h
18 #define _DateTime_h
19 
20 #include <time.h>
21 #include "WString.h"
22 #include "SmingLocale.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 
114  void setTime(uint8_t sec, uint8_t min, uint8_t hour, uint8_t day, uint8_t month, uint16_t year)
115  {
116  Second = sec;
117  Minute = min;
118  Hour = hour;
119  Day = day;
120  Month = month;
121  Year = year;
122  Milliseconds = 0;
123  calcDayOfYear();
124  }
125 
132  bool fromHttpDate(const String& httpDate);
133 
141  bool parseHttpDate(const String& httpDate) SMING_DEPRECATED
142  {
143  return fromHttpDate(httpDate);
144  }
145 
149  bool isNull();
150 
155  time_t toUnixTime();
156 
161 
166  String toShortTimeString(bool includeSeconds = false);
167 
172 
176  String toISO8601();
177 
181  String toHTTPDate();
182 
186  void addMilliseconds(long add);
187 
188  // functions to convert to and from time components (hrs, secs, days, years etc) to time_t
205  static void fromUnixTime(time_t timep, uint8_t* psec, uint8_t* pmin, uint8_t* phour, uint8_t* pday, uint8_t* pwday,
206  uint8_t* pmonth, uint16_t* pyear);
207 
210  static void fromUnixTime(time_t timep, int8_t* psec, int8_t* pmin, int8_t* phour, int8_t* pday, int8_t* pwday,
211  int8_t* pmonth, int16_t* pyear) SMING_DEPRECATED
212  {
213  fromUnixTime(timep, reinterpret_cast<uint8_t*>(psec), reinterpret_cast<uint8_t*>(pmin),
214  reinterpret_cast<uint8_t*>(phour), reinterpret_cast<uint8_t*>(pday),
215  reinterpret_cast<uint8_t*>(pwday), reinterpret_cast<uint8_t*>(pmonth),
216  reinterpret_cast<uint16_t*>(pyear));
217  }
218 
219  // functions to convert to and from time components (hrs, secs, days, years etc) to time_t
237  static void convertFromUnixTime(time_t timep, int8_t* psec, int8_t* pmin, int8_t* phour, int8_t* pday,
238  int8_t* pwday, int8_t* pmonth, int16_t* pyear) SMING_DEPRECATED
239  {
240  fromUnixTime(timep, reinterpret_cast<uint8_t*>(psec), reinterpret_cast<uint8_t*>(pmin),
241  reinterpret_cast<uint8_t*>(phour), reinterpret_cast<uint8_t*>(pday),
242  reinterpret_cast<uint8_t*>(pwday), reinterpret_cast<uint8_t*>(pmonth),
243  reinterpret_cast<uint16_t*>(pyear));
244  }
245 
258  static time_t toUnixTime(uint8_t sec, uint8_t min, uint8_t hour, uint8_t day, uint8_t month, uint16_t year);
259 
273  static time_t convertToUnixTime(uint8_t sec, uint8_t min, uint8_t hour, uint8_t day, uint8_t month,
274  uint16_t year) SMING_DEPRECATED
275  {
276  return toUnixTime(sec, min, hour, day, month, year);
277  }
278 
322  String format(const char* formatString);
323 
329  String format(const String& formatString)
330  {
331  return format(formatString.c_str());
332  }
333 
334 private:
335  void calcDayOfYear(); // Helper function calculates day of year
336  uint8_t calcWeek(uint8_t firstDay); //Helper function calculates week number based on firstDay of week
337 
338 public:
339  uint8_t Hour = 0;
340  uint8_t Minute = 0;
341  uint8_t Second = 0;
342  uint16_t Milliseconds = 0;
343  uint8_t Day = 0;
344  uint8_t DayofWeek = 0;
345  uint16_t DayofYear = 0;
346  uint8_t Month = 0;
347  uint16_t Year = 0;
348 };
349 
351 #endif /* DateTime_h */
Thursday.
Definition: DateTime.h:64
Date and time class.
Definition: DateTime.h:77
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) SMING_DEPRECATED
Definition: DateTime.h:210
dtDays_t
Days of week.
Definition: DateTime.h:59
String toISO8601()
Get human readable date and time.
DateTime()
Instantiate an uninitialised date and time object.
Definition: DateTime.h:82
uint16_t Year
Full Year number.
Definition: DateTime.h:347
DateTime(time_t time)
Instantiate a date and time object from a Unix timestamp.
Definition: DateTime.h:89
time_t toUnixTime()
Get Unix time.
bool parseHttpDate(const String &httpDate) SMING_DEPRECATED
Parse a HTTP full date and set time and date.
Definition: DateTime.h:141
uint16_t DayofYear
Day of year (0-365)
Definition: DateTime.h:345
The string class.
Definition: WString.h:104
Wednesday.
Definition: DateTime.h:63
uint8_t DayofWeek
Day of week (0-6 Sunday is day 0)
Definition: DateTime.h:344
Saturday.
Definition: DateTime.h:66
uint8_t Minute
Minute (0-59)
Definition: DateTime.h:340
String toShortTimeString(bool includeSeconds=false)
Get human readable time.
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:114
uint8_t Day
Day of month (1-31)
Definition: DateTime.h:343
String toFullDateTimeString()
Get human readable date and time.
Sunday.
Definition: DateTime.h:60
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) SMING_DEPRECATED
Convert from Unix time to individual time components.
Definition: DateTime.h:237
static time_t convertToUnixTime(uint8_t sec, uint8_t min, uint8_t hour, uint8_t day, uint8_t month, uint16_t year) SMING_DEPRECATED
Convert from individual time components to Unix time.
Definition: DateTime.h:273
String toShortDateString()
Get human readable date.
Tuesday.
Definition: DateTime.h:62
uint8_t Month
Month (0-11 Jan is month 0)
Definition: DateTime.h:346
void addMilliseconds(long add)
Add time to date time object.
uint16_t Milliseconds
Milliseconds (0-999)
Definition: DateTime.h:342
String format(const String &formatString)
Create string formatted with time and date placeholders.
Definition: DateTime.h:329
uint8_t Second
Second (0-59)
Definition: DateTime.h:341
uint8_t Hour
Hour (0-23)
Definition: DateTime.h:339
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.