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  Partially rewritten for Sming Framework
12 */
13 
17 #ifndef _DateTime_h
18 #define _DateTime_h
19 
20 #include <user_config.h>
21 #include <time.h>
22 #include "../../Wiring/WString.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:
82  DateTime();
83 
87  DateTime(time_t time);
88 
92  operator time_t() { return toUnixTime(); }
93 
97  void setTime(time_t time);
98 
106  void setTime(int8_t sec, int8_t min, int8_t hour, int8_t day, int8_t month, int16_t year);
107 
114  bool parseHttpDate(String httpDate);
115 
119  bool isNull();
120 
125  time_t toUnixTime();
126 
131 
136  String toShortTimeString(bool includeSeconds = false);
137 
142  String toISO8601();
143 
147  void addMilliseconds(long add);
148 
149  // functions to convert to and from time components (hrs, secs, days, years etc) to time_t
165  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);
166 
179  static time_t convertToUnixTime(int8_t sec, int8_t min, int8_t hour, int8_t day, int8_t month, int16_t year);
180 
181 public:
182  int8_t Hour;
183  int8_t Minute;
184  int8_t Second;
185  int16_t Milliseconds;
186  int8_t Day;
187  int8_t DayofWeek;
188  int8_t Month;
189  int16_t Year;
190 };
191 
193 #endif /* DateTime_h */
194 
Thursday.
Definition: DateTime.h:64
Date and time class.
Definition: DateTime.h:77
dtDays_t
Days of week.
Definition: DateTime.h:59
DateTime()
Instantiate an uninitialised date and time object.
time_t toUnixTime()
Get Unix time.
int8_t Day
Day of month (1-31)
Definition: DateTime.h:186
Definition: WString.h:42
Wednesday.
Definition: DateTime.h:63
Saturday.
Definition: DateTime.h:66
int8_t Hour
Hour (0-23)
Definition: DateTime.h:182
String toShortTimeString(bool includeSeconds=false)
Get human readable time.
int8_t Month
Month (0-11 Jan is month 0)
Definition: DateTime.h:188
int8_t DayofWeek
Day of week (0-6 Sunday is day 0)
Definition: DateTime.h:187
String toFullDateTimeString()
Get human readable date and time.
int16_t Milliseconds
Milliseconds (0-999)
Definition: DateTime.h:185
int8_t Minute
Minute (0-59)
Definition: DateTime.h:183
Sunday.
Definition: DateTime.h:60
String toShortDateString()
Get human readable date.
Tuesday.
Definition: DateTime.h:62
void addMilliseconds(long add)
Add time to date time object.
static time_t convertToUnixTime(int8_t sec, int8_t min, int8_t hour, int8_t day, int8_t month, int16_t year)
Convert from individual time components to Unix time.
bool parseHttpDate(String httpDate)
Parse a HTTP full date and set time and date.
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.
int8_t Second
Second (0-59)
Definition: DateTime.h:184
void setTime(time_t time)
Set time using Unix time.
Monday.
Definition: DateTime.h:61
Friday.
Definition: DateTime.h:65
int16_t Year
Full Year number.
Definition: DateTime.h:189
bool isNull()
Check if time date object is initialised.