open-spaced-repetition / rs-fsrs

Rust-based Scheduler for FSRS
https://crates.io/crates/rs-fsrs
MIT License
24 stars 6 forks source link

Support for fractional days #42

Open shivangp76 opened 1 month ago

shivangp76 commented 1 month ago

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)
    }
}
asukaminato0721 commented 1 week ago

seems fixed by #41

shivangp76 commented 1 week ago

This is more of a general question, throughout the crate. #41 is only for a specific function. For example, could Card.elapsed_days be an f64 instead of an i64? Could the following be changed to utilize something like num_fractional_days() mentioned in the original request?

https://github.com/open-spaced-repetition/rs-fsrs/blob/7c01007ae4221c1eee66ed9d60367428c99e683a/src/scheduler.rs#L24