1 /*!
2  * \file      systime.h
3  *
4  * \brief     System time functions implementation.
5  *
6  * \copyright Revised BSD License, see section \ref LICENSE.
7  *
8  * \code
9  *                ______                              _
10  *               / _____)             _              | |
11  *              ( (____  _____ ____ _| |_ _____  ____| |__
12  *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
13  *               _____) ) ____| | | || |_| ____( (___| | | |
14  *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
15  *              (C)2013-2018 Semtech - STMicroelectronics
16  *
17  * \endcode
18  *
19  * \author    Miguel Luis ( Semtech )
20  *
21  * \author    Gregory Cristian ( Semtech )
22  *
23  * \author    MCD Application Team ( STMicroelectronics International )
24  */
25 #ifndef __SYS_TIME_H__
26 #define __SYS_TIME_H__
27 
28 #ifdef __cplusplus
29 extern "C"
30 {
31 #endif
32 
33 #include <stdint.h>
34 #include "time.h"
35 #include "timer.h"
36 
37 /*!
38  * \brief Days, Hours, Minutes and seconds of systime.h
39  */
40 #define TM_DAYS_IN_LEAP_YEAR                        ( ( uint32_t )  366U )
41 #define TM_DAYS_IN_YEAR                             ( ( uint32_t )  365U )
42 #define TM_SECONDS_IN_1DAY                          ( ( uint32_t )86400U )
43 #define TM_SECONDS_IN_1HOUR                         ( ( uint32_t ) 3600U )
44 #define TM_SECONDS_IN_1MINUTE                       ( ( uint32_t )   60U )
45 #define TM_MINUTES_IN_1HOUR                         ( ( uint32_t )   60U )
46 #define TM_HOURS_IN_1DAY                            ( ( uint32_t )   24U )
47 
48 
49 /*!
50  * \brief Months of systime.h
51  */
52 #define TM_MONTH_JANUARY                            ( ( uint8_t ) 0U )
53 #define TM_MONTH_FEBRUARY                           ( ( uint8_t ) 1U )
54 #define TM_MONTH_MARCH                              ( ( uint8_t ) 2U )
55 #define TM_MONTH_APRIL                              ( ( uint8_t ) 3U )
56 #define TM_MONTH_MAY                                ( ( uint8_t ) 4U )
57 #define TM_MONTH_JUNE                               ( ( uint8_t ) 5U )
58 #define TM_MONTH_JULY                               ( ( uint8_t ) 6U )
59 #define TM_MONTH_AUGUST                             ( ( uint8_t ) 7U )
60 #define TM_MONTH_SEPTEMBER                          ( ( uint8_t ) 8U )
61 #define TM_MONTH_OCTOBER                            ( ( uint8_t ) 9U )
62 #define TM_MONTH_NOVEMBER                           ( ( uint8_t )10U )
63 #define TM_MONTH_DECEMBER                           ( ( uint8_t )11U )
64 
65 /*!
66  * \brief Week days of systime.h
67  */
68 #define TM_WEEKDAY_SUNDAY                           ( ( uint8_t )0U )
69 #define TM_WEEKDAY_MONDAY                           ( ( uint8_t )1U )
70 #define TM_WEEKDAY_TUESDAY                          ( ( uint8_t )2U )
71 #define TM_WEEKDAY_WEDNESDAY                        ( ( uint8_t )3U )
72 #define TM_WEEKDAY_THURSDAY                         ( ( uint8_t )4U )
73 #define TM_WEEKDAY_FRIDAY                           ( ( uint8_t )5U )
74 #define TM_WEEKDAY_SATURDAY                         ( ( uint8_t )6U )
75 
76 /*!
77  * \brief Number of seconds elapsed between Unix and GPS epoch
78  */
79 #define UNIX_GPS_EPOCH_OFFSET                       315964800
80 
81 /*!
82  * \brief Structure holding the system time in seconds and milliseconds.
83  */
84 typedef struct SysTime_s
85 {
86     uint32_t Seconds;
87     int16_t  SubSeconds;
88 }SysTime_t;
89 
90 /*!
91  * \brief Adds 2 SysTime_t values
92  *
93  * \param a Value
94  * \param b Value to added
95  *
96  * \retval result Addition result (SysTime_t value)
97  */
98 SysTime_t SysTimeAdd( SysTime_t a, SysTime_t b );
99 
100 /*!
101  * \brief Subtracts 2 SysTime_t values
102  *
103  * \param a Value
104  * \param b Value to be subtracted
105  *
106  * \retval result Subtraction result (SysTime_t value)
107  */
108 SysTime_t SysTimeSub( SysTime_t a, SysTime_t b );
109 
110 /*!
111  * \brief Sets new system time
112  *
113  * \param  sysTime    New seconds/sub-seconds since UNIX epoch origin
114  */
115 void SysTimeSet( SysTime_t sysTime );
116 
117 /*!
118  * \brief Gets current system time
119  *
120  * \retval sysTime    Current seconds/sub-seconds since UNIX epoch origin
121  */
122 SysTime_t SysTimeGet( void );
123 
124 /*!
125  * \brief Gets current MCU system time
126  *
127  * \retval sysTime    Current seconds/sub-seconds since Mcu started
128  */
129 SysTime_t SysTimeGetMcuTime( void );
130 
131 /*!
132  * Converts the given SysTime to the equivalent RTC value in milliseconds
133  *
134  * \param [IN] sysTime System time to be converted
135  *
136  * \retval timeMs The RTC converted time value in ms
137  */
138 TimerTime_t SysTimeToMs( SysTime_t sysTime );
139 
140 /*!
141  * Converts the given RTC value in milliseconds to the equivalent SysTime
142  *
143  * \param [IN] timeMs The RTC time value in ms to be converted
144  *
145  * \retval sysTime Converted system time
146  */
147 SysTime_t SysTimeFromMs( TimerTime_t timeMs );
148 
149 /*!
150  * \brief Convert a calendar time into time since UNIX epoch as a uint32_t.
151  *
152  * \param [IN] localtime Pointer to the object containing the calendar time
153  * \retval     timestamp The calendar time as seconds since UNIX epoch.
154  */
155 uint32_t SysTimeMkTime( const struct tm* localtime );
156 
157 /*!
158  * \brief Converts a given time in seconds since UNIX epoch into calendar time.
159  *
160  * \param [IN]  timestamp The time since UNIX epoch to convert into calendar time.
161  * \param [OUT] localtime Pointer to the calendar time object which will contain
162                           the result of the conversion.
163  */
164 void SysTimeLocalTime( const uint32_t timestamp, struct tm *localtime );
165 
166 #ifdef __cplusplus
167 }
168 #endif
169 
170 #endif // __SYS_TIME_H__
171