1 /***********************************************************************************************************************************
2 Time Management
3 ***********************************************************************************************************************************/
4 #ifndef COMMON_TIME_H
5 #define COMMON_TIME_H
6 
7 #include <stdint.h>
8 #include <time.h>
9 
10 /***********************************************************************************************************************************
11 Time types
12 ***********************************************************************************************************************************/
13 typedef uint64_t TimeMSec;
14 
15 /***********************************************************************************************************************************
16 Constants describing number of sub-units in an interval
17 ***********************************************************************************************************************************/
18 #define MSEC_PER_SEC                                                ((TimeMSec)1000)
19 #define SEC_PER_DAY                                                 ((time_t)86400)
20 
21 /***********************************************************************************************************************************
22 Functions
23 ***********************************************************************************************************************************/
24 // Sleep for specified milliseconds
25 void sleepMSec(TimeMSec sleepMSec);
26 
27 // Epoch time in milliseconds
28 TimeMSec timeMSec(void);
29 
30 // Are the date parts valid? (year >= 1970, month 1-12, day 1-31)
31 void datePartsValid(int year, int month, int day);
32 
33 // Are the time parts valid?
34 void timePartsValid(int hour, int minute, int second);
35 
36 // Are the timezone offset parts valid?
37 void tzPartsValid(int tzHour, int tzMinute);
38 
39 // Given the hours, minutes and sign of a time zone (e.g. -0700 => -7, 0) return the signed number or seconds (e.g. -25200)
40 int tzOffsetSeconds(int tzHour, int tzMinute);
41 
42 // Is the year a leap year?
43 bool yearIsLeap(int year);
44 
45 // Get days since the beginning of the year (year >= 1970, month 1-12, day 1-31), returns 1-366
46 int dayOfYear(int year, int month, int day);
47 
48 // Return epoch time from date/time parts (year >= 1970, month 1-12, day 1-31, hour 0-23, minute 0-59, second 0-59, tzOffsetSecond
49 // is the number of seconds plus or minus (+/-) the provided time - if 0, then the datetime is assumed to be UTC)
50 time_t epochFromParts(int year, int month, int day, int hour, int minute, int second, int tzOffsetSecond);
51 
52 /***********************************************************************************************************************************
53 Macros for function logging
54 ***********************************************************************************************************************************/
55 #define FUNCTION_LOG_TIME_MSEC_TYPE                                                                                                \
56     TimeMSec
57 #define FUNCTION_LOG_TIME_MSEC_FORMAT(value, buffer, bufferSize)                                                                   \
58     cvtUInt64ToZ(value, buffer, bufferSize)
59 
60 #endif
61