Giter VIP home page Giter VIP logo

calends's People

Contributors

bluemoon avatar bobhy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

bobhy

calends's Issues

`RelativeDuration::num_days()` misleading output (or name?)

It seems that num_days(), num_weeks() and num_months() simply fetch the internal implementation fields unchanged.
I was expecting these would return the total length of the duration (truncated to whole days, weeks, months).
I wasn't expecting num_months() to be infallible, since it should need a reference Date to base calculations on in the general case.

Here's a sample program:

#![allow(unused_imports, dead_code)]
extern crate calends;
extern crate chrono;
use calends::RelativeDuration;
use chrono::{DateTime, Datelike, NaiveDate, NaiveDateTime, TimeZone, Timelike, Utc};
use std::fmt::Display;
pub fn main() {

    // simple case, length of month doesn't affect outcome:
    let rd1 = RelativeDuration::from_mwd(0, 3, 12);
    assert_contrapositive(rd1.num_weeks(), 3 + (12 / 7), "from_mwd().num_weeks");
    assert_contrapositive(rd1.num_days(), (3 * 7) + 12, "from_mwd().num_days");

    //  trickier case, length of month *does* come into it.

    let rd2 = RelativeDuration::from_duration_between(
        NaiveDate::from_ymd_opt(2023, 3, 31).unwrap(),
        NaiveDate::from_ymd_opt(2023, 4, 1).unwrap(),
    );

    assert_contrapositive(rd2.num_months(), 1, "from_duration_between().num_months");
    assert_contrapositive(rd2.num_days(), 1, "from_duration_between().num_days");
}

fn assert_contrapositive<T: Display + PartialOrd>(observed: T, expected: T, tag: &str) {
    if observed == expected {
        println!("SUCCESS?! Case {tag}, observed and expected were both: {observed}");
    } else {
        println!("Mismatch. Case {tag}, observed: {observed}, expected: {expected}");
    }
}

Which prints this, when run:

〉cargo run --quiet --example bug_rd
Mismatch. Case from_mwd().num_weeks, observed: 3, expected: 4
Mismatch. Case from_mwd().num_days, observed: 12, expected: 33
SUCCESS?! Case from_duration_between().num_months, observed and expected were both: 1
Mismatch. Case from_duration_between().num_days, observed: -30, expected: 1

FWIW, I think calends should have a helper function that does accept a date and can return total_days, total_weeks, total_months. (Unless there's already a way to get these? Calendar arithmetic is tricky.)

Plus, it would be OK for RelativeImpl to have the simple getters.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.