Skip to main content

Module rtc_math

Module rtc_math 

Source
Expand description

Pure calendar and BCD arithmetic shared by the RTC implementations.

Two very different components need the same arithmetic: the host CMOS driver (crate::hardware::timer::rtc), which reads the real MC146818 and converts its registers to and from UNIX time, and the s9vm guest RTC device model (crate::s9vm::rtc), which does the opposite : it computes the register values a guest reads from an emulated chip. Keeping one copy of the Howard Hinnant civil-date algorithm and of the BCD conversions in a neutral, dependency-free module guarantees the two cannot disagree about leap years, month lengths or digit packing.

Everything here is pure core code with no I/O and no state: it is host-tested through both users.

Constants§

CIVIL_EPOCH_SHIFT 🔒
UNIX epoch day offset used by the civil-date algorithm.
DAYS_PER_YEAR
Days in one common year.
SECONDS_PER_DAY
Seconds in one day.

Functions§

bcd_to_binary
Converts a packed BCD byte to binary (0x42 -> 42).
binary_to_bcd
Converts a binary value (0..=99) to packed BCD (42 -> 0x42).
civil_from_days
Converts days since 1970-01-01 to a Gregorian (year, month, day).
civil_from_unix
Converts UNIX seconds to a civil date-time.
day_time
Splits UNIX seconds into (hour, minute, second).
days_in_month
Number of days in month of year (Gregorian calendar).
is_leap_year
True if year is a Gregorian leap year.
unix_from_civil
Converts a civil date-time to UNIX seconds.
weekday_from_days
Day of week for days since 1970-01-01, 0 = Sunday .. 6 = Saturday.