Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for fractional days #42

Open
shivangp76 opened this issue Oct 24, 2024 · 0 comments
Open

Support for fractional days #42

shivangp76 opened this issue Oct 24, 2024 · 0 comments

Comments

@shivangp76
Copy link
Contributor

I was wondering if there would by any interest in supporting fractional days.

Chrono's .num_days() does not round the number of days. See this Rust playground.

A possible solution is to implement something like this:

pub trait FractionalDays {
    fn num_fractional_days(&self) -> f64;
    fn fractional_days(fractional_days: f64) -> Self;
}

impl FractionalDays for Duration {
    fn num_fractional_days(&self) -> f64 {
        let seconds_in_a_day = 24.0 * 3600.0;
        f64::from(self.num_seconds() as i32) / seconds_in_a_day
    }

    fn fractional_days(fractional_days: f64) -> Self {
        Duration::seconds((fractional_days * 24. * 60. * 60.).round() as i64)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant