uni_core/time_source.rs
1/// Platform-agnostic time source abstraction for the Uni interpreter.
2///
3/// This trait allows embedding applications to provide date/time information
4/// without requiring the interpreter to depend on specific time libraries.
5/// Different platforms can implement this trait using their available
6/// hardware timers, RTCs, or system clocks.
7///
8/// The interface matches what RTC (Real-Time Clock) chips typically provide:
9/// date components (year, month, day, hour, minute, second) plus timezone offset.
10///
11/// # Examples
12///
13/// ```
14/// use uni_core::{TimeSource, DateComponents};
15///
16/// struct SystemTime;
17///
18/// impl TimeSource for SystemTime {
19/// fn now(&self) -> DateComponents {
20/// // On std platforms, use chrono or std::time
21/// DateComponents {
22/// year: 2025,
23/// month: 10,
24/// day: 18,
25/// hour: 14,
26/// minute: 30,
27/// second: 0,
28/// offset_minutes: 0, // UTC
29/// }
30/// }
31/// }
32/// ```
33
34/// Date and time components, matching what RTC chips provide
35#[derive(Debug, Clone, Copy)]
36pub struct DateComponents {
37 pub year: i32,
38 pub month: u8, // 1-12
39 pub day: u8, // 1-31
40 pub hour: u8, // 0-23
41 pub minute: u8, // 0-59
42 pub second: u8, // 0-59
43 pub offset_minutes: i32, // Timezone offset from UTC in minutes
44}
45
46pub trait TimeSource {
47 /// Get current date and time as components.
48 ///
49 /// For embedded systems without a real-time clock, this may return:
50 /// - Time from an external RTC chip
51 /// - Time from NTP (if network available)
52 /// - A fixed value (e.g., 2000-01-01 for systems without time)
53 ///
54 /// Timezone offset is positive east of UTC, negative west:
55 /// - UTC: 0
56 /// - EST (UTC-5): -300
57 /// - JST (UTC+9): +540
58 fn now(&self) -> DateComponents;
59}