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 arch/dreamcast/include/rtc.h
4 Copyright (C) 2000, 2001 Megan Potter
5 Copyright (C) 2023, 2024 Falco Girgis
6
7*/
8
9/** \file arch/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 in the
14 Dreamcast. Generally, you should prefer interacting with the higher level
15 standard C functions, like time(), rather than these when simply needing
16 to fetch the current system time.
17
18 \sa arch/wdt.h
19
20 \author Megan Potter
21 \author Falco Girgis
22*/
23
24#ifndef __ARCH_RTC_H
25#define __ARCH_RTC_H
26
27#include <sys/cdefs.h>
28__BEGIN_DECLS
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 Dreamcast's real-time clock. 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 The RTC that is used by the DC is located on the AICA rather than SH4,
44 presumably for power-efficiency reasons. Because of this, accessing
45 it requires a trip over the G2 BUS, which is notoriously slow.
46
47 \note
48 For reading the current date/time, you should favor the standard C,
49 C++, or POSIX functions, as they are platform-indpendent and are
50 calculating current time based on a cached boot time plus a delta
51 that is maintained by the timer subsystem, rather than actually
52 having to requery the RTC over the G2 BUS, so they are faster.
53
54 \warning
55 Internally, the RTC's date/time is maintained using a 32-bit counter
56 with an epoch of January 1, 1950 00:00. Because of this, the Dreamcast's
57 Y2K and the last timestamp it can represent before rolling over is
58 February 06 2086 06:28:15.
59
60 \sa wdt, timers, perf_counters
61
62 @{
63*/
64
65/** \brief Get the current date/time.
66
67 This function retrieves the current RTC value as a standard UNIX timestamp
68 (with an epoch of January 1, 1970 00:00). This is assumed to be in the
69 timezone of the user (as the RTC does not support timezones).
70
71 \return The current UNIX-style timestamp (local time).
72
73 \sa rtc_set_unix_secs()
74*/
75time_t rtc_unix_secs(void);
76
77/** \brief Set the current date/time.
78
79 This function sets the current RTC value as a standard UNIX timestamp
80 (with an epoch of January 1, 1970 00:00). This is assumed to be in the
81 timezone of the user (as the RTC does not support timezones).
82
83 \warning
84 This function may fail! Since `time_t` is typically 64-bit while the RTC
85 uses a 32-bit timestamp (which also has a different epoch), not all
86 `time_t` values can be represented within the RTC!
87
88 \param time Unix timestamp to set the current time to
89
90 \return 0 for success or -1 for failure (with errno set
91 appropriately).
92
93 \exception EINVAL \p time was an invalid timestamp or could not be
94 represented on the AICA's RTC.
95 \exception EPERM Failed to set and successfully read back \p time
96 from the RTC.
97
98 \sa rtc_unix_secs()
99*/
100int rtc_set_unix_secs(time_t time);
101
102/** \brief Get the time since the system was booted.
103
104 This function retrieves the cached RTC value from when KallistiOS was
105 started. As with rtc_unix_secs(), this is a UNIX-style timestamp in
106 local time.
107
108 \return The boot time as a UNIX-style timestamp.
109*/
110time_t rtc_boot_time(void);
111
112/* \cond INTERNAL */
113/* Internally called Init / Shutdown */
114int rtc_init(void);
115void rtc_shutdown(void);
116/* \endcond */
117
118/** @} */
119
120__END_DECLS
121
122#endif /* __ARCH_RTC_H */
123
time_t rtc_unix_secs(void)
Get the current date/time.
time_t rtc_boot_time(void)
Get the time since the system was booted.
int rtc_set_unix_secs(time_t time)
Set the current date/time.
KOS-implementation of select C11 and POSIX extensions.