]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/Time/Time.h
first commit
[arduino] / books / pdummies / Libraries / Time / Time.h
1 /*
2   time.h - low level time and date functions
3 */
4
5 #ifndef _Time_h
6 #define _Time_h
7
8 #include <inttypes.h>
9
10 typedef unsigned long time_t;
11
12 typedef enum {timeNotSet, timeNeedsSync, timeSet
13 }  timeStatus_t ;
14
15 typedef enum {
16     dowInvalid, dowSunday, dowMonday, dowTuesday, dowWednesday, dowThursday, dowFriday, dowSaturday
17 } timeDayOfWeek_t;
18
19 typedef enum {
20     tmSecond, tmMinute, tmHour, tmWday, tmDay,tmMonth, tmYear, tmNbrFields
21 } tmByteFields;    
22
23 typedef struct  { 
24   uint8_t Second; 
25   uint8_t Minute; 
26   uint8_t Hour; 
27   uint8_t Wday;   // day of week, sunday is day 1
28   uint8_t Day;
29   uint8_t Month; 
30   uint8_t Year;   // offset from 1970; 
31 }       tmElements_t, TimeElements, *tmElementsPtr_t;
32
33 //convenience macros to convert to and from tm years 
34 #define  tmYearToCalendar(Y) ((Y) + 1970)  // full four digit year 
35 #define  CalendarYrToTm(Y)   ((Y) - 1970)
36 #define  tmYearToY2k(Y)      ((Y) - 30)    // offset is from 2000
37 #define  y2kYearToTm(Y)      ((Y) + 30)   
38
39 typedef time_t(*getExternalTime)();
40 //typedef void  (*setExternalTime)(const time_t); // not used in this version
41
42
43 /*==============================================================================*/
44 /* Useful Constants */
45 #define SECS_PER_MIN  (60UL)
46 #define SECS_PER_HOUR (3600UL)
47 #define SECS_PER_DAY  (SECS_PER_HOUR * 24UL)
48 #define DAYS_PER_WEEK (7UL)
49 #define SECS_PER_WEEK (SECS_PER_DAY * DAYS_PER_WEEK)
50 #define SECS_PER_YEAR (SECS_PER_WEEK * 52UL)
51 #define SECS_YR_2000  (946684800UL) // the time at the start of y2k
52  
53 /* Useful Macros for getting elapsed time */
54 #define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)  
55 #define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN) 
56 #define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR)
57 #define dayOfWeek(_time_)  ((( _time_ / SECS_PER_DAY + 4)  % DAYS_PER_WEEK)+1) // 1 = Sunday
58 #define elapsedDays(_time_) ( _time_ / SECS_PER_DAY)  // this is number of days since Jan 1 1970
59 #define elapsedSecsToday(_time_)  (_time_ % SECS_PER_DAY)   // the number of seconds since last midnight 
60 #define previousMidnight(_time_) (( _time_ / SECS_PER_DAY) * SECS_PER_DAY)  // time at the start of the given day
61 #define nextMidnight(_time_) ( previousMidnight(_time_)  + SECS_PER_DAY ) // time at the end of the given day 
62 #define elapsedSecsThisWeek(_time_)  (elapsedSecsToday(_time_) +  (dayOfWeek(_time_) * SECS_PER_DAY) )   
63
64 /* Useful Macros for converting elapsed time to a time_t */
65 #define minutesToTime_t ((M)) ( (M) * SECS_PER_MIN)  
66 #define hoursToTime_t   ((H)) ( (H) * SECS_PER_HOUR)  
67 #define daysToTime_t    ((H)) ( (D) * SECS_PER_DAY) 
68 #define weeksToTime_t   ((W)) ( (W) * SECS_PER_WEEK)   
69
70 /*============================================================================*/
71 /*  time and date functions   */
72 int     hour();            // the hour now 
73 int     hour(time_t t);    // the hour for the given time
74 int     hourFormat12();    // the hour now in 12 hour format
75 int     hourFormat12(time_t t); // the hour for the given time in 12 hour format
76 uint8_t isAM();            // returns true if time now is AM
77 uint8_t isAM(time_t t);    // returns true the given time is AM
78 uint8_t isPM();            // returns true if time now is PM
79 uint8_t isPM(time_t t);    // returns true the given time is PM
80 int     minute();          // the minute now 
81 int     minute(time_t t);  // the minute for the given time
82 int     second();          // the second now 
83 int     second(time_t t);  // the second for the given time
84 int     day();             // the day now 
85 int     day(time_t t);     // the day for the given time
86 int     weekday();         // the weekday now (Sunday is day 1) 
87 int     weekday(time_t t); // the weekday for the given time 
88 int     month();           // the month now  (Jan is month 1)
89 int     month(time_t t);   // the month for the given time
90 int     year();            // the full four digit year: (2009, 2010 etc) 
91 int     year(time_t t);    // the year for the given time
92
93 time_t now();              // return the current time as seconds since Jan 1 1970 
94 void    setTime(time_t t);
95 void    setTime(int hr,int min,int sec,int day, int month, int yr);
96 void    adjustTime(long adjustment);
97
98 /* date strings */ 
99 #define dt_MAX_STRING_LEN 9 // length of longest date string (excluding terminating null)
100 char* monthStr(uint8_t month);
101 char* dayStr(uint8_t day);
102 char* monthShortStr(uint8_t month);
103 char* dayShortStr(uint8_t day);
104         
105 /* time sync functions  */
106 timeStatus_t timeStatus(); // indicates if time has been set and recently synchronized
107 void    setSyncProvider( getExternalTime getTimeFunction); // identify the external time provider
108 void    setSyncInterval(time_t interval); // set the number of seconds between re-sync
109
110 /* low level functions to convert to and from system time                     */
111 void breakTime(time_t time, tmElements_t &tm);  // break time_t into elements
112 time_t makeTime(tmElements_t &tm);  // convert time elements into time_t
113
114
115 #endif /* _Time_h */
116