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-01to a Gregorian(year, month, day). - civil_
from_ unix - Converts UNIX
secondsto a civil date-time. - day_
time - Splits UNIX
secondsinto(hour, minute, second). - days_
in_ month - Number of days in
monthofyear(Gregorian calendar). - is_
leap_ year - True if
yearis a Gregorian leap year. - unix_
from_ civil - Converts a civil date-time to UNIX seconds.
- weekday_
from_ days - Day of week for
dayssince 1970-01-01,0= Sunday ..6= Saturday.