-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
time.c
244 lines (195 loc) · 6.2 KB
/
time.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* @file time.c
*
* @section License
* Copyright (C) 2014-2016, Erik Moqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* This file is part of the Simba project.
*/
/**
* `time_time_to_date()` is based on the musl project code.
*
* musl as a whole is licensed under the following standard MIT license:
*
* Copyright © 2005-2013 Rich Felker
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "simba.h"
#include <limits.h>
#include "time_port.i"
/* 2000-03-01 (mod 400 year, immediately after feb29 */
#define LEAPOCH (946684800LL + 86400*(31+29))
#define DAYS_PER_400Y (365L*400L + 97L)
#define DAYS_PER_100Y (365L*100L + 24L)
#define DAYS_PER_4Y (365L*4L + 1L)
struct state_t {
uint64_t tick; /* Current tick. 64 bits so it does not wrap around
during the system's uptime. */
};
static struct state_t state = {
.tick = 0
};
static inline void tick_to_time(uint64_t tick,
struct time_t *time_p)
{
time_p->seconds = (tick / CONFIG_SYSTEM_TICK_FREQUENCY);
time_p->nanoseconds =
((1000000000ULL * (tick % CONFIG_SYSTEM_TICK_FREQUENCY))
/ CONFIG_SYSTEM_TICK_FREQUENCY);
}
/**
* Update the current time every system tick.
*/
void time_tick_isr(void)
{
state.tick += 1;
}
int time_get(struct time_t *now_p)
{
ASSERTN(now_p != NULL, EINVAL);
uint64_t tick;
sys_lock();
tick = state.tick;
sys_unlock();
tick_to_time(tick, now_p);
return (0);
}
int time_set(struct time_t *new_p)
{
ASSERTN(new_p != NULL, EINVAL);
uint64_t tick;
tick = ((new_p->seconds * CONFIG_SYSTEM_TICK_FREQUENCY) +
DIV_CEIL((DIV_CEIL(new_p->nanoseconds, 1000)
* CONFIG_SYSTEM_TICK_FREQUENCY), 1000000));
sys_lock();
state.tick = tick;
sys_unlock();
return (0);
}
int time_diff(struct time_t *diff_p,
struct time_t *left_p,
struct time_t *right_p)
{
ASSERTN(diff_p != NULL, EINVAL);
ASSERTN(left_p != NULL, EINVAL);
ASSERTN(right_p != NULL, EINVAL);
diff_p->seconds = (left_p->seconds - right_p->seconds);
diff_p->nanoseconds = (left_p->nanoseconds - right_p->nanoseconds);
if (diff_p->nanoseconds < 0) {
if (diff_p->seconds > 0) {
diff_p->seconds--;
diff_p->nanoseconds += 1000000000;
}
}
return (0);
}
void time_busy_wait_us(long microseconds)
{
time_port_busy_wait_us(microseconds);
}
int time_unix_time_to_date(struct date_t *date_p,
struct time_t *time_p)
{
ASSERTN(date_p != NULL, EINVAL);
ASSERTN(time_p != NULL, EINVAL);
long long t = time_p->seconds;
long long weeks, days, secs;
int32_t remdays, remsecs, remyears;
int32_t qc_cycles, c_cycles, q_cycles;
int32_t years, months;
int32_t wday, yday, leap;
static const char days_in_month[] = {
31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29
};
/* Reject time_t values whose year would overflow int32_t */
if ((t < INT32_MIN * 31622400LL) || (t > INT32_MAX * 31622400LL)) {
return (-1);
}
secs = (t - LEAPOCH);
days = (secs / 86400);
remsecs = (secs - (86400 * days));
if (remsecs < 0) {
remsecs += 86400;
days--;
}
weeks = ((3 + days) / 7);
wday = ((3 + days) - (7 * weeks));
if (wday < 0) {
wday += 7;
}
qc_cycles = (days / DAYS_PER_400Y);
remdays = (days - (qc_cycles * DAYS_PER_400Y));
if (remdays < 0) {
remdays += DAYS_PER_400Y;
qc_cycles--;
}
c_cycles = (remdays / DAYS_PER_100Y);
if (c_cycles == 4) {
c_cycles--;
}
remdays -= (c_cycles * DAYS_PER_100Y);
q_cycles = (remdays / DAYS_PER_4Y);
if (q_cycles == 25) {
q_cycles--;
}
remdays -= (q_cycles * DAYS_PER_4Y);
remyears = (remdays / 365);
if (remyears == 4) {
remyears--;
}
remdays -= (remyears * 365);
leap = (!remyears && (q_cycles || !c_cycles));
yday = (remdays + 31 + 28 + leap);
if (yday >= (365 + leap)) {
yday -= (365 + leap);
}
years = (remyears + 4 * q_cycles + 100 * c_cycles + 400 * qc_cycles);
for (months = 0; days_in_month[months] <= remdays; months++) {
remdays -= days_in_month[months];
}
if ((years + 100 > INT32_MAX) || (years + 100 < INT32_MIN)) {
return (-1);
}
date_p->year = (years + 100);
date_p->year += 1900;
date_p->month = (months + 2);
if (date_p->month >= 12) {
date_p->month -= 12;
date_p->year++;
}
date_p->month++;
date_p->date = (remdays + 1);
date_p->day = wday;
date_p->hour = (remsecs / 3600);
date_p->minute = (remsecs / 60 % 60);
date_p->second = (remsecs % 60);
return (0);
}