refactor: optimize time type sizes and add convertion from Time to jiff::civil::Time

This commit is contained in:
yuankunzhang
2025-08-14 22:19:40 +08:00
committed by Daniel Hofstetter
parent a79ce1fc31
commit 920cfa5f1b
2 changed files with 32 additions and 17 deletions
+3 -3
View File
@@ -223,9 +223,9 @@ impl DateTimeBuilder {
dt.year(),
dt.month(),
dt.day(),
hour,
minute,
second,
hour as u32,
minute as u32,
second as u32,
nanosecond,
offset,
)?;
+29 -14
View File
@@ -51,11 +51,25 @@ use super::{
#[derive(PartialEq, Clone, Debug, Default)]
pub(crate) struct Time {
pub hour: u32,
pub minute: u32,
pub second: u32,
pub nanosecond: u32,
pub offset: Option<Offset>,
pub(crate) hour: u8,
pub(crate) minute: u8,
pub(crate) second: u8,
pub(crate) nanosecond: u32,
pub(crate) offset: Option<Offset>,
}
impl TryFrom<Time> for jiff::civil::Time {
type Error = &'static str;
fn try_from(time: Time) -> Result<Self, Self::Error> {
jiff::civil::Time::new(
time.hour as i8,
time.minute as i8,
time.second as i8,
time.nanosecond as i32,
)
.map_err(|_| "time is not valid")
}
}
#[derive(Clone)]
@@ -134,25 +148,26 @@ fn am_pm_time(input: &mut &str) -> ModalResult<Time> {
})
}
/// Parse a number of hours in `0..24`(preceded by whitespace)
pub(super) fn hour24(input: &mut &str) -> ModalResult<u32> {
/// Parse a number of hours in `0..24`.
pub(super) fn hour24(input: &mut &str) -> ModalResult<u8> {
s(dec_uint).verify(|x| *x < 24).parse_next(input)
}
/// Parse a number of hours in `0..=12` (preceded by whitespace)
fn hour12(input: &mut &str) -> ModalResult<u32> {
/// Parse a number of hours in `0..=12`.
fn hour12(input: &mut &str) -> ModalResult<u8> {
s(dec_uint).verify(|x| *x <= 12).parse_next(input)
}
/// Parse a number of minutes (preceded by whitespace)
pub(super) fn minute(input: &mut &str) -> ModalResult<u32> {
/// Parse a number of minutes in `0..60`.
pub(super) fn minute(input: &mut &str) -> ModalResult<u8> {
s(dec_uint).verify(|x| *x < 60).parse_next(input)
}
/// Parse a number of seconds (preceded by whitespace)
fn second(input: &mut &str) -> ModalResult<(u32, u32)> {
/// Parse a number of seconds in `0..60` and an optional number of nanoseconds
/// (default to 0 if not set).
fn second(input: &mut &str) -> ModalResult<(u8, u32)> {
sec_and_nsec
.verify_map(|(s, ns)| if s < 60 { Some((s as u32, ns)) } else { None })
.verify_map(|(s, ns)| if s < 60 { Some((s as u8, ns)) } else { None })
.parse_next(input)
}