AquilaOS documentation
How it works, and its code
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. */
20static int clock_set;
21
29int gettime(struct timespec *ts)
30{
31 if (!clock_set) {
32 int err = arch_time_get(ts);
33
34 if (err)
35 return err;
36
39 clock_set = 1;
40 }
41
43
44 ts->tv_sec = clock_base_sec + ns / 1000000000ULL;
45 ts->tv_nsec = ns % 1000000000ULL;
46 return 0;
47}
48
54void 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));
68 }
69}
70
78int 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}
int printk(const char *fmt,...)
Writes a kernel message, to the message buffer and the consoles.
Definition printk.c:249
int arch_time_get(struct timespec *ts)
Reads the date and time from the real time clock.
Definition rtc.c:16
signed long long int int64_t
Definition stddef.h:19
unsigned long long int uint64_t
Definition stddef.h:23
uint32_t tv_nsec
Definition types.h:32
time_t tv_sec
Definition types.h:31
suseconds_t tv_usec
Definition types.h:37
time_t tv_sec
Definition types.h:36
int tz_minuteswest
Definition types.h:41
int tz_dsttime
Definition types.h:42
static uint64_t clock_base_ns
Definition time.c:19
int gettime(struct timespec *ts)
Reads the wall clock (CLOCK_REALTIME).
Definition time.c:29
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
int gettimeofday(struct timeval *tv, struct timezone *tz)
Reads the wall clock, in microseconds, for gettimeofday().
Definition time.c:78
static int clock_set
Definition time.c:20
static time_t clock_base_sec
Definition time.c:18
uint64_t arch_rtime_ns(void)
Reads the monotonic clock, which starts at boot.
Definition timer.c:56
uint64_t time_t
Definition types.h:26