KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
rtc.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 include/kos/rtc.h
4 Copyright (C) 2000, 2001 Megan Potter
5 Copyright (C) 2023, 2024 Falco Girgis
6
7*/
8
9/** \file kos/rtc.h
10 \brief Low-level real-time clock functionality.
11 \ingroup rtc
12
13 This file contains functions for interacting with the real-time clock.
14 Generally, you should prefer interacting with the higher level standard C
15 functions, like time(), rather than these when simply needing to fetch the
16 current system time.
17
18 \author Megan Potter
19 \author Falco Girgis
20*/
21
22#ifndef __KOS_RTC_H
23#define __KOS_RTC_H
24
25#include <kos/cdefs.h>
26__BEGIN_DECLS
27
28#include <arch/rtc.h>
29
30#include <time.h>
31
32/** \defgroup rtc Real-Time Clock
33 \brief Real-Time Clock (RTC) Management
34 \ingroup timing
35
36 Provides an API for fetching and managing the date/time using
37 the hardware real-time clock (RTC). All timestamps are in standard
38 Unix format, with an epoch of January 1, 1970. Due to the fact
39 that there is no time zone data on the RTC, all times are expected
40 to be in the local time zone.
41
42 \note
43 For reading the current date/time, you should favor the standard C,
44 C++, or POSIX functions, as they are platform-indpendent and are
45 calculating current time based on a cached boot time plus a delta
46 that is maintained by the timer subsystem, rather than actually
47 having to requery the RTC, so they are faster.
48
49 \sa wdt, timers, perf_counters
50
51 @{
52*/
53
54/** \brief Get the current date/time.
55
56 This function retrieves the current RTC value as a standard UNIX timestamp
57 (with an epoch of January 1, 1970 00:00). This is assumed to be in the
58 timezone of the user (as the RTC does not support timezones).
59
60 \return The current UNIX-style timestamp (local time).
61
62 \sa rtc_set_unix_secs()
63*/
64static inline time_t rtc_unix_secs(void) {
65 return arch_rtc_unix_secs();
66}
67
68/** \brief Set the current date/time.
69
70 This function sets the current RTC value as a standard UNIX timestamp
71 (with an epoch of January 1, 1970 00:00). This is assumed to be in the
72 timezone of the user (as the RTC does not support timezones).
73
74 \warning
75 This function may fail! Since `time_t` is typically 64-bit while the RTC
76 uses a 32-bit timestamp (which also has a different epoch), not all
77 `time_t` values can be represented within the RTC!
78
79 \param time Unix timestamp to set the current time to
80
81 \return 0 for success or -1 for failure (with errno set
82 appropriately).
83
84 \exception EINVAL \p time was an invalid timestamp or could not be
85 represented on the hardware RTC.
86 \exception EPERM Failed to set and successfully read back \p time
87 from the RTC.
88
89 \sa rtc_unix_secs()
90*/
91static inline int rtc_set_unix_secs(time_t time) {
92 return arch_rtc_set_unix_secs(time);
93}
94
95/** \brief Get the time since the system was booted.
96
97 This function retrieves the cached RTC value from when KallistiOS was
98 started. As with rtc_unix_secs(), this is a UNIX-style timestamp in
99 local time.
100
101 \return The boot time as a UNIX-style timestamp.
102*/
103static inline time_t rtc_boot_time(void) {
104 return arch_rtc_boot_time();
105}
106
107/* \cond INTERNAL */
108/* Internally called Init / Shutdown */
109static inline int rtc_init(void) {
110 return arch_rtc_init();
111}
112
113static inline void rtc_shutdown(void) {
114 arch_rtc_shutdown();
115}
116/* \endcond */
117
118/** @} */
119
120__END_DECLS
121
122#endif /* __KOS_RTC_H */
Various common macros used throughout the codebase.
static int rtc_set_unix_secs(time_t time)
Set the current date/time.
Definition rtc.h:91
static time_t rtc_unix_secs(void)
Get the current date/time.
Definition rtc.h:64
static time_t rtc_boot_time(void)
Get the time since the system was booted.
Definition rtc.h:103
Low-level real-time clock functionality.
KOS-implementation of select C11 and POSIX extensions.