|
1 | | -/* |
2 | | - time.h - low level time and date functions |
3 | | -*/ |
4 | | - |
5 | | -/* |
6 | | - July 3 2011 - fixed elapsedSecsThisWeek macro (thanks Vincent Valdy for this) |
7 | | - - fixed daysToTime_t macro (thanks maniacbug) |
8 | | -*/ |
9 | | - |
10 | | -#ifndef _Time_h |
11 | | -#ifdef __cplusplus |
12 | | -#define _Time_h |
13 | | - |
14 | | -#include <inttypes.h> |
15 | | -#ifndef __AVR__ |
16 | | -#include <sys/types.h> // for __time_t_defined, but avr libc lacks sys/types.h |
17 | | -#endif |
18 | | - |
19 | | - |
20 | | -#if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc |
21 | | -typedef unsigned long time_t; |
22 | | -#endif |
23 | | - |
24 | | - |
25 | | -// This ugly hack allows us to define C++ overloaded functions, when included |
26 | | -// from within an extern "C", as newlib's sys/stat.h does. Actually it is |
27 | | -// intended to include "time.h" from the C library (on ARM, but AVR does not |
28 | | -// have that file at all). On Mac and Windows, the compiler will find this |
29 | | -// "Time.h" instead of the C library "time.h", so we may cause other weird |
30 | | -// and unpredictable effects by conflicting with the C library header "time.h", |
31 | | -// but at least this hack lets us define C++ functions as intended. Hopefully |
32 | | -// nothing too terrible will result from overriding the C library header?! |
33 | | -extern "C++" { |
34 | | -typedef enum {timeNotSet, timeNeedsSync, timeSet |
35 | | -} timeStatus_t ; |
36 | | - |
37 | | -typedef enum { |
38 | | - dowInvalid, dowSunday, dowMonday, dowTuesday, dowWednesday, dowThursday, dowFriday, dowSaturday |
39 | | -} timeDayOfWeek_t; |
40 | | - |
41 | | -typedef enum { |
42 | | - tmSecond, tmMinute, tmHour, tmWday, tmDay,tmMonth, tmYear, tmNbrFields |
43 | | -} tmByteFields; |
44 | | - |
45 | | -typedef struct { |
46 | | - uint8_t Second; |
47 | | - uint8_t Minute; |
48 | | - uint8_t Hour; |
49 | | - uint8_t Wday; // day of week, sunday is day 1 |
50 | | - uint8_t Day; |
51 | | - uint8_t Month; |
52 | | - uint8_t Year; // offset from 1970; |
53 | | -} tmElements_t, TimeElements, *tmElementsPtr_t; |
54 | | - |
55 | | -//convenience macros to convert to and from tm years |
56 | | -#define tmYearToCalendar(Y) ((Y) + 1970) // full four digit year |
57 | | -#define CalendarYrToTm(Y) ((Y) - 1970) |
58 | | -#define tmYearToY2k(Y) ((Y) - 30) // offset is from 2000 |
59 | | -#define y2kYearToTm(Y) ((Y) + 30) |
60 | | - |
61 | | -typedef time_t(*getExternalTime)(); |
62 | | -//typedef void (*setExternalTime)(const time_t); // not used in this version |
63 | | - |
64 | | - |
65 | | -/*==============================================================================*/ |
66 | | -/* Useful Constants */ |
67 | | -#define SECS_PER_MIN (60UL) |
68 | | -#define SECS_PER_HOUR (3600UL) |
69 | | -#define SECS_PER_DAY (SECS_PER_HOUR * 24UL) |
70 | | -#define DAYS_PER_WEEK (7UL) |
71 | | -#define SECS_PER_WEEK (SECS_PER_DAY * DAYS_PER_WEEK) |
72 | | -#define SECS_PER_YEAR (SECS_PER_WEEK * 52UL) |
73 | | -#define SECS_YR_2000 (946684800UL) // the time at the start of y2k |
74 | | - |
75 | | -/* Useful Macros for getting elapsed time */ |
76 | | -#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN) |
77 | | -#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN) |
78 | | -#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR) |
79 | | -#define dayOfWeek(_time_) ((( _time_ / SECS_PER_DAY + 4) % DAYS_PER_WEEK)+1) // 1 = Sunday |
80 | | -#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY) // this is number of days since Jan 1 1970 |
81 | | -#define elapsedSecsToday(_time_) (_time_ % SECS_PER_DAY) // the number of seconds since last midnight |
82 | | -// The following macros are used in calculating alarms and assume the clock is set to a date later than Jan 1 1971 |
83 | | -// Always set the correct time before settting alarms |
84 | | -#define previousMidnight(_time_) (( _time_ / SECS_PER_DAY) * SECS_PER_DAY) // time at the start of the given day |
85 | | -#define nextMidnight(_time_) ( previousMidnight(_time_) + SECS_PER_DAY ) // time at the end of the given day |
86 | | -#define elapsedSecsThisWeek(_time_) (elapsedSecsToday(_time_) + ((dayOfWeek(_time_)-1) * SECS_PER_DAY) ) // note that week starts on day 1 |
87 | | -#define previousSunday(_time_) (_time_ - elapsedSecsThisWeek(_time_)) // time at the start of the week for the given time |
88 | | -#define nextSunday(_time_) ( previousSunday(_time_)+SECS_PER_WEEK) // time at the end of the week for the given time |
89 | | - |
90 | | - |
91 | | -/* Useful Macros for converting elapsed time to a time_t */ |
92 | | -#define minutesToTime_t ((M)) ( (M) * SECS_PER_MIN) |
93 | | -#define hoursToTime_t ((H)) ( (H) * SECS_PER_HOUR) |
94 | | -#define daysToTime_t ((D)) ( (D) * SECS_PER_DAY) // fixed on Jul 22 2011 |
95 | | -#define weeksToTime_t ((W)) ( (W) * SECS_PER_WEEK) |
96 | | - |
97 | | -/*============================================================================*/ |
98 | | -/* time and date functions */ |
99 | | -int hour(); // the hour now |
100 | | -int hour(time_t t); // the hour for the given time |
101 | | -int hourFormat12(); // the hour now in 12 hour format |
102 | | -int hourFormat12(time_t t); // the hour for the given time in 12 hour format |
103 | | -uint8_t isAM(); // returns true if time now is AM |
104 | | -uint8_t isAM(time_t t); // returns true the given time is AM |
105 | | -uint8_t isPM(); // returns true if time now is PM |
106 | | -uint8_t isPM(time_t t); // returns true the given time is PM |
107 | | -int minute(); // the minute now |
108 | | -int minute(time_t t); // the minute for the given time |
109 | | -int second(); // the second now |
110 | | -int second(time_t t); // the second for the given time |
111 | | -int day(); // the day now |
112 | | -int day(time_t t); // the day for the given time |
113 | | -int weekday(); // the weekday now (Sunday is day 1) |
114 | | -int weekday(time_t t); // the weekday for the given time |
115 | | -int month(); // the month now (Jan is month 1) |
116 | | -int month(time_t t); // the month for the given time |
117 | | -int year(); // the full four digit year: (2009, 2010 etc) |
118 | | -int year(time_t t); // the year for the given time |
119 | | - |
120 | | -time_t now(); // return the current time as seconds since Jan 1 1970 |
121 | | -void setTime(time_t t); |
122 | | -void setTime(int hr,int min,int sec,int day, int month, int yr); |
123 | | -void adjustTime(long adjustment); |
124 | | - |
125 | | -/* date strings */ |
126 | | -#define dt_MAX_STRING_LEN 9 // length of longest date string (excluding terminating null) |
127 | | -char* monthStr(uint8_t month); |
128 | | -char* dayStr(uint8_t day); |
129 | | -char* monthShortStr(uint8_t month); |
130 | | -char* dayShortStr(uint8_t day); |
131 | | - |
132 | | -/* time sync functions */ |
133 | | -timeStatus_t timeStatus(); // indicates if time has been set and recently synchronized |
134 | | -void setSyncProvider( getExternalTime getTimeFunction); // identify the external time provider |
135 | | -void setSyncInterval(time_t interval); // set the number of seconds between re-sync |
136 | | - |
137 | | -/* low level functions to convert to and from system time */ |
138 | | -void breakTime(time_t time, tmElements_t &tm); // break time_t into elements |
139 | | -time_t makeTime(tmElements_t &tm); // convert time elements into time_t |
140 | | - |
141 | | -} // extern "C++" |
142 | | -#endif // __cplusplus |
143 | | -#endif /* _Time_h */ |
144 | | - |
| 1 | +#include "TimeLib.h" |
0 commit comments