Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2019 Peter Bigot Consulting, LLC
3 : : *
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : :
7 : : /*
8 : : * The time_days_from_civil function is derived directly from public
9 : : * domain content written by Howard Hinnant and available at:
10 : : * http://howardhinnant.github.io/date_algorithms.html#days_from_civil
11 : : */
12 : :
13 : : #include <errno.h>
14 : : #include <stdbool.h>
15 : : #include <stddef.h>
16 : : #include <stdint.h>
17 : : #include <time.h>
18 : :
19 : : #include <zephyr/sys/__assert.h>
20 : : #include <zephyr/sys/clock.h>
21 : : #include <zephyr/sys/div64.h>
22 : : #include <zephyr/sys/timeutil.h>
23 : :
24 : : /** Convert a civil (proleptic Gregorian) date to days relative to
25 : : * 1970-01-01.
26 : : *
27 : : * @param y the calendar year
28 : : * @param m the calendar month, in the range [1, 12]
29 : : * @param d the day of the month, in the range [1, last_day_of_month(y, m)]
30 : : *
31 : : * @return the signed number of days between the specified day and
32 : : * 1970-01-01
33 : : *
34 : : * @see http://howardhinnant.github.io/date_algorithms.html#days_from_civil
35 : : */
36 : 0 : static int64_t time_days_from_civil(int64_t y,
37 : : unsigned int m,
38 : : unsigned int d)
39 : : {
40 : 0 : y -= m <= 2;
41 : :
42 [ # # # # : 0 : int64_t era = sys_sdiv64_u32_const((y >= 0) ? y : (y - 399), 400);
# # ]
43 : 0 : unsigned int yoe = y - era * 400;
44 [ # # ]: 0 : unsigned int doy = (153U * (m + ((m > 2) ? -3 : 9)) + 2U) / 5U + d;
45 : 0 : unsigned int doe = yoe * 365U + yoe / 4U - yoe / 100U + doy;
46 : :
47 : 0 : return era * 146097 + (time_t)doe - 719468;
48 : : }
49 : :
50 : 0 : int64_t timeutil_timegm64(const struct tm *tm)
51 : : {
52 : 0 : int64_t y = TIME_UTILS_BASE_YEAR + (int64_t)tm->tm_year;
53 : 0 : unsigned int m = tm->tm_mon + 1;
54 : 0 : unsigned int d = tm->tm_mday - 1;
55 : 0 : int64_t ndays = time_days_from_civil(y, m, d);
56 : 0 : int64_t secs = tm->tm_sec;
57 : :
58 : 0 : secs += 60LL * (tm->tm_min + 60LL * tm->tm_hour);
59 : 0 : secs += 86400LL * ndays;
60 : :
61 : 0 : return secs;
62 : : }
63 : :
64 : 0 : time_t timeutil_timegm(const struct tm *tm)
65 : : {
66 : 0 : int64_t secs = timeutil_timegm64(tm);
67 : 0 : time_t rv = (time_t)secs;
68 : :
69 : 0 : errno = 0;
70 : 0 : if ((sizeof(rv) == sizeof(int32_t))
71 : : && ((secs < (int64_t)INT32_MIN)
72 [ # # ]: 0 : || (secs > (int64_t)INT32_MAX))) {
73 : 0 : errno = ERANGE;
74 : 0 : rv = -1;
75 : : }
76 : :
77 : 0 : return rv;
78 : : }
79 : :
80 : 0 : int timeutil_sync_state_update(struct timeutil_sync_state *tsp,
81 : : const struct timeutil_sync_instant *inst)
82 : : {
83 : 0 : int rv = -EINVAL;
84 : :
85 [ # # # # ]: 0 : if (((tsp->base.ref == 0) && (inst->ref > 0))
86 [ # # ]: 0 : || ((inst->ref > tsp->base.ref)
87 [ # # ]: 0 : && (inst->local > tsp->base.local))) {
88 [ # # ]: 0 : if (tsp->base.ref == 0) {
89 : 0 : tsp->base = *inst;
90 : 0 : tsp->latest = (struct timeutil_sync_instant){};
91 : 0 : tsp->skew = 1.0f;
92 : 0 : rv = 0;
93 : : } else {
94 : 0 : tsp->latest = *inst;
95 : 0 : rv = 1;
96 : : }
97 : : }
98 : :
99 : 0 : return rv;
100 : : }
101 : :
102 : 0 : int timeutil_sync_state_set_skew(struct timeutil_sync_state *tsp, float skew,
103 : : const struct timeutil_sync_instant *base)
104 : : {
105 : 0 : int rv = -EINVAL;
106 : :
107 [ # # ]: 0 : if (skew > 0) {
108 : 0 : tsp->skew = skew;
109 [ # # ]: 0 : if (base != NULL) {
110 : 0 : tsp->base = *base;
111 : 0 : tsp->latest = (struct timeutil_sync_instant){};
112 : : }
113 : : rv = 0;
114 : : }
115 : :
116 : 0 : return rv;
117 : : }
118 : :
119 : 0 : float timeutil_sync_estimate_skew(const struct timeutil_sync_state *tsp)
120 : : {
121 : 0 : float rv = 0;
122 : :
123 [ # # # # ]: 0 : if ((tsp->base.ref != 0) && (tsp->latest.ref != 0)
124 [ # # ]: 0 : && (tsp->latest.local > tsp->base.local)) {
125 : 0 : const struct timeutil_sync_config *cfg = tsp->cfg;
126 : 0 : double ref_delta = tsp->latest.ref - tsp->base.ref;
127 : 0 : double local_delta = tsp->latest.local - tsp->base.local;
128 : :
129 : 0 : rv = ref_delta * cfg->local_Hz / local_delta / cfg->ref_Hz;
130 : : }
131 : :
132 : 0 : return rv;
133 : : }
134 : :
135 : 0 : int timeutil_sync_ref_from_local(const struct timeutil_sync_state *tsp,
136 : : uint64_t local, uint64_t *refp)
137 : : {
138 : 0 : int rv = -EINVAL;
139 : :
140 [ # # # # : 0 : if ((tsp->skew > 0) && (tsp->base.ref > 0) && (refp != NULL)) {
# # ]
141 : 0 : const struct timeutil_sync_config *cfg = tsp->cfg;
142 : 0 : int64_t local_delta = local - tsp->base.local;
143 : : #ifdef CONFIG_TIMEUTIL_APPLY_SKEW
144 : : /* (x * 1.0) != x for large values of x.
145 : : * Therefore only apply the multiplication if the skew is not one.
146 : : */
147 [ # # ]: 0 : if (tsp->skew != 1.0f) {
148 : 0 : local_delta *= (double)tsp->skew;
149 : : }
150 : : #endif /* CONFIG_TIMEUTIL_APPLY_SKEW */
151 : 0 : int64_t ref_delta = local_delta * cfg->ref_Hz / cfg->local_Hz;
152 : 0 : int64_t ref_abs = (int64_t)tsp->base.ref + ref_delta;
153 : :
154 [ # # ]: 0 : if (ref_abs < 0) {
155 : : rv = -ERANGE;
156 : : } else {
157 : 0 : *refp = ref_abs;
158 : 0 : rv = (tsp->skew != 1.0f) ? 1 : 0;
159 : : }
160 : : }
161 : :
162 : 0 : return rv;
163 : : }
164 : :
165 : 0 : int timeutil_sync_local_from_ref(const struct timeutil_sync_state *tsp,
166 : : uint64_t ref, int64_t *localp)
167 : : {
168 : 0 : int rv = -EINVAL;
169 : :
170 [ # # # # : 0 : if ((tsp->skew > 0) && (tsp->base.ref > 0) && (localp != NULL)) {
# # ]
171 : 0 : const struct timeutil_sync_config *cfg = tsp->cfg;
172 : 0 : int64_t ref_delta = (int64_t)(ref - tsp->base.ref);
173 : 0 : int64_t local_delta = (ref_delta * cfg->local_Hz) / cfg->ref_Hz;
174 : : #ifdef CONFIG_TIMEUTIL_APPLY_SKEW
175 : : /* (x / 1.0) != x for large values of x.
176 : : * Therefore only apply the division if the skew is not one.
177 : : */
178 [ # # ]: 0 : if (tsp->skew != 1.0f) {
179 : 0 : local_delta /= (double)tsp->skew;
180 : : }
181 : : #endif /* CONFIG_TIMEUTIL_APPLY_SKEW */
182 : 0 : int64_t local_abs = (int64_t)tsp->base.local
183 : : + (int64_t)local_delta;
184 : :
185 : 0 : *localp = local_abs;
186 : 0 : rv = (tsp->skew != 1.0f) ? 1 : 0;
187 : : }
188 : :
189 : 0 : return rv;
190 : : }
191 : :
192 : 0 : int32_t timeutil_sync_skew_to_ppb(float skew)
193 : : {
194 : 0 : int64_t ppb64 = (int64_t)((1.0 - (double)skew) * 1E9);
195 : 0 : int32_t ppb32 = (int32_t)ppb64;
196 : :
197 [ # # ]: 0 : return (ppb64 == ppb32) ? ppb32 : INT32_MIN;
198 : : }
199 : :
200 : 0 : bool timespec_normalize(struct timespec *ts)
201 : : {
202 [ # # ]: 0 : __ASSERT_NO_MSG(ts != NULL);
203 : :
204 : 0 : long sec;
205 : :
206 [ # # ]: 0 : if (ts->tv_nsec >= (long)NSEC_PER_SEC) {
207 : 0 : sec = ts->tv_nsec / (long)NSEC_PER_SEC;
208 [ # # ]: 0 : } else if (ts->tv_nsec < 0) {
209 : 0 : sec = DIV_ROUND_UP((unsigned long)-ts->tv_nsec, NSEC_PER_SEC);
210 : : } else {
211 : : sec = 0;
212 : : }
213 : :
214 [ # # # # : 0 : if ((ts->tv_nsec < 0) && (ts->tv_sec < 0) && (ts->tv_sec - SYS_TIME_T_MIN < sec)) {
# # ]
215 : : /*
216 : : * When `tv_nsec` is negative and `tv_sec` is already most negative,
217 : : * further subtraction would cause integer overflow.
218 : : */
219 : : return false;
220 : : }
221 : :
222 [ # # # # ]: 0 : if ((ts->tv_nsec >= (long)NSEC_PER_SEC) && (ts->tv_sec > 0) &&
223 [ # # ]: 0 : (SYS_TIME_T_MAX - ts->tv_sec < sec)) {
224 : : /*
225 : : * When `tv_nsec` is >= `NSEC_PER_SEC` and `tv_sec` is already most
226 : : * positive, further addition would cause integer overflow.
227 : : */
228 : : return false;
229 : : }
230 : :
231 [ # # ]: 0 : if (ts->tv_nsec >= (long)NSEC_PER_SEC) {
232 : 0 : ts->tv_sec += sec;
233 : 0 : ts->tv_nsec -= sec * (long)NSEC_PER_SEC;
234 [ # # ]: 0 : } else if (ts->tv_nsec < 0) {
235 : 0 : ts->tv_sec -= sec;
236 : 0 : ts->tv_nsec += sec * (long)NSEC_PER_SEC;
237 : : } else {
238 : : /* no change: SonarQube was complaining */
239 : 0 : }
240 : :
241 [ # # ]: 0 : __ASSERT_NO_MSG(timespec_is_valid(ts));
242 : :
243 : : return true;
244 : : }
|