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