pub struct Time {
pub hour: u8,
pub minute: u8,
pub second: u8,
pub nanosecond: u32,
/* private fields */
}Expand description
Represents the time of day portion of a TOML datetime value.
Field ranges are validated during parsing:
hour: 0–23minute: 0–59second: 0–60 (60 is permitted for leap seconds)nanosecond: 0–999999999
When seconds are omitted in the source (e.g. 12:30), second defaults
to 0. Use has_seconds to distinguish this from an
explicit :00.
§Examples
use toml_spanner::DateTime;
let dt: DateTime = "14:30:05.123".parse().unwrap();
let time = dt.time().unwrap();
assert_eq!(time.hour, 14);
assert_eq!(time.minute, 30);
assert_eq!(time.second, 5);
assert_eq!(time.nanosecond, 123000000);
assert_eq!(time.subsecond_precision(), 3);Fields§
§hour: u8Hour of the day (0–23).
minute: u8Minute of the hour (0–59).
second: u8Second of the minute (0–60).
nanosecond: u32Sub-second component in nanoseconds (0–999999999).
Implementations§
Source§impl Time
impl Time
Sourcepub fn subsecond_precision(&self) -> u8
pub fn subsecond_precision(&self) -> u8
Returns the number of fractional-second digits present in the source.
Returns 0 when no fractional part was written (e.g. 12:30:00),
and 1–9 for .1 through .123456789.
Sourcepub fn has_seconds(&self) -> bool
pub fn has_seconds(&self) -> bool
Returns true if seconds were explicitly written in the source.
When the input omits seconds (e.g. 12:30), second
is set to 0 but this method returns false.
Sourcepub fn new(hour: u8, minute: u8, second: u8, nanosecond: u32) -> Option<Time>
pub fn new(hour: u8, minute: u8, second: u8, nanosecond: u32) -> Option<Time>
Builds a Time from its clock components.
Returns None when the values fall outside the TOML range:
hourin0..=23minutein0..=59secondin0..=60(60is reserved for leap seconds)nanosecondin0..=999_999_999
Serialization emits only the fractional digits needed to round-trip
nanosecond exactly, so a trailing zero you did not provide will not
appear in the output:
nanosecond | serialized fraction |
|---|---|
0 | (none) |
500_000_000 | .5 |
123_000_000 | .123 |
123 | .000000123 |
§Examples
use toml_spanner::Time;
let time = Time::new(14, 30, 5, 123_000_000).unwrap();
assert_eq!(time.second, 5);
assert_eq!(time.subsecond_precision(), 3);
assert!(Time::new(24, 0, 0, 0).is_none());
assert!(Time::new(0, 0, 0, 1_000_000_000).is_none());