1 /*
2  * Copyright (C) 2017-2019 Alibaba Group Holding Limited
3  */
4 
5 #ifndef _TIME_H_
6 #define _TIME_H_
7 
8 /********************************************************************************
9  * Included Files
10  ********************************************************************************/
11 #include <stdint.h>
12 
13 /********************************************************************************
14  * Pre-processor Definitions
15  ********************************************************************************/
16 
17 /* Clock tick of the system (frequency Hz).
18  *
19  * NOTE: This symbolic name CLK_TCK has been removed from the standard.  It is
20  * replaced with CLOCKS_PER_SEC.  Both are defined here.
21  *
22  * The default value is 1Mz
23  */
24 #define CLK_TCK           (1000000)
25 #define CLOCKS_PER_SEC    (1000000)
26 
27 #define NSEC_PER_SEC        1000000000
28 #define USEC_PER_SEC        1000000
29 #define NSEC_PER_USEC       1000
30 #define USEC_PER_MSEC       1000
31 #define MSEC_PER_SEC        1000
32 #define NSEC_PER_MSEC       1000000
33 #define TICK2MSEC(tick)     ((tick)* (1000 / CLOCKS_PER_SEC))
34 
35 /* CLOCK_REALTIME refers to the standard time source.  For most
36  * implementations, the standard time source is the system timer interrupt.
37  * However, if the platform supports an RTC, then the standard time source
38  * will be the RTC for the clock_gettime() and clock_settime() interfaces
39  * (the system timer is still the time source for all of the interfaces).
40  *
41  * CLOCK_REALTIME represents the machine's best-guess as to the current
42  * wall-clock, time-of-day time. This means that CLOCK_REALTIME can jump
43  * forward and backward as the system time-of-day clock is changed.
44  */
45 
46 #define CLOCK_REALTIME     0
47 
48 /* Clock that cannot be set and represents monotonic time since some
49  * unspecified starting point. It is not affected by changes in the
50  * system time-of-day clock.
51  */
52 #define CLOCK_MONOTONIC  1
53 
54 /* This is a flag that may be passed to the timer_settime() function */
55 
56 #define TIMER_ABSTIME      1
57 
58 /* Local time is the same as gmtime in this implementation */
59 #define localtime_r(c,r)   gmtime_r(c,r)
60 
61 /********************************************************************************
62  * Public Types
63  ********************************************************************************/
64 /* Scalar types */
65 
66 #if !defined(__time_t_defined) && !defined(_TIME_T_DECLARED)
67 typedef int32_t  time_t;         /* Holds time in seconds */
68 #define __time_t_defined
69 #define _TIME_T_DECLARED
70 #endif
71 #if !defined(__clockid_t_defined) && !defined(_CLOCKID_T_DECLARED)
72 typedef uint8_t   clockid_t;      /* Identifies one time base source */
73 #define __clockid_t_defined
74 #define _CLOCKID_T_DECLARED
75 #endif
76 
77 /* struct timespec is the standard representation of time as seconds and
78  * nanoseconds.
79  */
80 
81 #ifndef _SYS__TIMESPEC_H_
82 #define _SYS__TIMESPEC_H_
83 struct timespec {
84     time_t tv_sec;                   /* Seconds */
85     long   tv_nsec;                  /* Nanoseconds */
86 };
87 #endif
88 
89 /* struct tm is the standard representation for "broken out" time.
90  *
91  * REVISIT: This structure could be packed better using uint8_t's and
92  * uint16_t's.  The standard definition does, however, call out type int for
93  * all of the members.  NOTE: Any changes to this structure must be also be
94  * reflected in struct rtc_time defined in include/nuttx/timers/rtc.h; these
95  * two structures must be cast compatible.
96  */
97 
98 struct tm {
99     int tm_sec;     /* Seconds (0-61, allows for leap seconds) */
100     int tm_min;     /* Minutes (0-59) */
101     int tm_hour;    /* Hours (0-23) */
102     int tm_mday;    /* Day of the month (1-31) */
103     int tm_mon;     /* Month (0-11) */
104     int tm_year;    /* Years since 1900 */
105     int tm_wday;    /* Day of the week (0-6) */
106     int tm_yday;    /* Day of the year (0-365) */
107     int tm_isdst;   /* Non-0 if daylight savings time is in effect */
108 };
109 
110 /* Struct itimerspec is used to define settings for an interval timer */
111 
112 #ifndef _SYS__TIMESPEC_H_
113 struct itimerspec {
114     struct timespec it_value;    /* First time */
115     struct timespec it_interval; /* and thereafter */
116 };
117 #endif
118 
119 /* forward reference (defined in signal.h) */
120 
121 struct sigevent;
122 
123 /********************************************************************************
124  * Public Data
125  ********************************************************************************/
126 
127 #undef EXTERN
128 #if defined(__cplusplus)
129 #define EXTERN extern "C"
130 extern "C"
131 {
132 #else
133 #define EXTERN extern
134 #endif
135 
136 /********************************************************************************
137  * Public Function Prototypes
138  ********************************************************************************/
139 int clock_timer_init(void);
140 int clock_timer_uninit(void);
141 int clock_timer_start(void);
142 int clock_timer_stop(void);
143 int clock_gettime(clockid_t clockid, struct timespec *tp);
144 
145 time_t mktime(struct tm *tp);
146 struct tm *gmtime_r(const time_t *timep, struct tm *result);
147 
148 #undef EXTERN
149 #if defined(__cplusplus)
150 }
151 #endif
152 #endif  /*_SYS_TIME_H_*/
153