AquilaOS documentation
How it works, and its code
Toggle main menu visibility
Loading...
Searching...
No Matches
time.c
Go to the documentation of this file.
1
10
11
#include <
core/system.h
>
12
#include <
core/time.h
>
13
#include <
core/arch.h
>
14
#include <
core/printk.h
>
15
16
/* The real time clock's seconds, read at the monotonic time
17
* clock_base_ns. */
18
static
time_t
clock_base_sec
;
19
static
uint64_t
clock_base_ns
;
20
static
int
clock_set
;
21
29
int
gettime
(
struct
timespec
*ts)
30
{
31
if
(!
clock_set
) {
32
int
err =
arch_time_get
(ts);
33
34
if
(err)
35
return
err;
36
37
clock_base_sec
= ts->
tv_sec
;
38
clock_base_ns
=
arch_rtime_ns
();
39
clock_set
= 1;
40
}
41
42
uint64_t
ns =
arch_rtime_ns
() -
clock_base_ns
;
43
44
ts->
tv_sec
=
clock_base_sec
+ ns / 1000000000ULL;
45
ts->
tv_nsec
= ns % 1000000000ULL;
46
return
0;
47
}
48
54
void
time_resync
(
void
)
55
{
56
struct
timespec
rtc, now;
57
58
if
(!
clock_set
||
arch_time_get
(&rtc) ||
gettime
(&now))
59
return
;
60
61
int64_t
delta = (
int64_t
) now.
tv_sec
- (
int64_t
) rtc.
tv_sec
;
62
63
if
(delta >= 2 || delta <= -2) {
64
printk
(
"time: %c%d s from the real time clock, set to it\n"
,
65
delta < 0 ?
'-'
:
'+'
, (
int
) (delta < 0 ? -delta : delta));
66
clock_base_sec
= rtc.
tv_sec
;
67
clock_base_ns
=
arch_rtime_ns
();
68
}
69
}
70
78
int
gettimeofday
(
struct
timeval
*tv,
struct
timezone
*tz)
79
{
80
int
err = 0;
81
82
struct
timespec
ts;
83
84
if
((err =
gettime
(&ts)))
85
return
err;
86
87
if
(tz) {
88
tz->
tz_minuteswest
= 0;
89
tz->
tz_dsttime
= 0;
90
}
91
92
if
(tv) {
93
tv->
tv_sec
= ts.
tv_sec
;
94
tv->
tv_usec
= ts.
tv_nsec
/ 1000;
95
}
96
97
return
0;
98
}
arch.h
system.h
printk
int printk(const char *fmt,...)
Writes a kernel message, to the message buffer and the consoles.
Definition
printk.c:249
printk.h
arch_time_get
int arch_time_get(struct timespec *ts)
Reads the date and time from the real time clock.
Definition
rtc.c:16
int64_t
signed long long int int64_t
Definition
stddef.h:19
uint64_t
unsigned long long int uint64_t
Definition
stddef.h:23
timespec
Definition
types.h:30
timespec::tv_nsec
uint32_t tv_nsec
Definition
types.h:32
timespec::tv_sec
time_t tv_sec
Definition
types.h:31
timeval
Definition
types.h:35
timeval::tv_usec
suseconds_t tv_usec
Definition
types.h:37
timeval::tv_sec
time_t tv_sec
Definition
types.h:36
timezone
Definition
types.h:40
timezone::tz_minuteswest
int tz_minuteswest
Definition
types.h:41
timezone::tz_dsttime
int tz_dsttime
Definition
types.h:42
clock_base_ns
static uint64_t clock_base_ns
Definition
time.c:19
gettime
int gettime(struct timespec *ts)
Reads the wall clock (CLOCK_REALTIME).
Definition
time.c:29
time_resync
void time_resync(void)
Sets the wall clock from the real time clock again, if they are 2 seconds or more apart.
Definition
time.c:54
gettimeofday
int gettimeofday(struct timeval *tv, struct timezone *tz)
Reads the wall clock, in microseconds, for gettimeofday().
Definition
time.c:78
clock_set
static int clock_set
Definition
time.c:20
clock_base_sec
static time_t clock_base_sec
Definition
time.c:18
time.h
arch_rtime_ns
uint64_t arch_rtime_ns(void)
Reads the monotonic clock, which starts at boot.
Definition
timer.c:56
time_t
uint64_t time_t
Definition
types.h:26
kernel
core
time.c
Generated by
1.18.0