Giter Site home page Giter Site logo

Comments (4)

lifthrasiir avatar lifthrasiir commented on May 3, 2024 2

While Chrono is inherently tied to the proleptic Gregorian calendar, I don't think such specific functions are not a good fit for general purpose library. If you only care about the correct answer (and not the performance), the following can be used:

fn is_leap_year(year: i32) -> bool {
    NaiveDate::from_ymd_opt(year, 2, 29).is_some()
}

fn last_day_of_month(year: i32, month: 32) -> u32 {
    NaiveDate::from_ymd_opt(year, month + 1, 1).unwrap_or(NaiveDate::from_ymd(year + 1, 1, 1)).pred().day()
}

I think JodaTime does have methods like is_leap, but mostly for supporting different calendar systems with varying leap definitions.

from chrono.

westy92 avatar westy92 commented on May 3, 2024 1

You can add this functionality for your project with:

trait NaiveDateExt {
    fn days_in_month(&self) -> i32;
    fn days_in_year(&self) -> i32;
    fn is_leap_year(&self) -> bool;
}

impl NaiveDateExt for chrono::NaiveDate {
    fn days_in_month(&self) -> i32 {
        let month = self.month();
        match month {
            1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
            4 | 6 | 9 | 11 => 30,
            2 => if self.is_leap_year() { 29 } else { 28 },
            _ => panic!("Invalid month: {}" , month),
        }
    }

    fn days_in_year(&self) -> i32 {
        if self.is_leap_year() { 366 } else { 365 }
    }

    fn is_leap_year(&self) -> bool {
        let year = self.year();
        return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
    }
}

from chrono.

sshine avatar sshine commented on May 3, 2024

such specific functions are not a good fit for general purpose library

I apologize for necroposting, but could you explain why?

A safe variation of the last_day_of_month() posted earlier,

pub fn last_of_month(year: i32, month: u32) -> Option<chrono::NaiveDate> {
    chrono::NaiveDate::from_ymd_opt(year, month + 1, 1)
        .or_else(|| chrono::NaiveDate::from_ymd_opt(year + 1, 1, 1))?
        .pred_opt()
}

from chrono.

pitdicker avatar pitdicker commented on May 3, 2024

I have seen this request more than once, and I also think it is reasonable and useful to add.

#1247 includes NaiveDate::leap_year, and #69 is the open issue for adding something like last_day_of_month(). I used the same workaround as you in a number of cases.

I am not sure yet about the best API for something like last_day_of_month(). In one case (the NaiveDate::diff_months_days() method in #1247) it was not enough for me to know the number of days in the current month, it needed the number in the previous month. #69 (comment) suggests adding a type such as YearMonth and implementing the method on that would be more flexible, and #203 also requests such a type.

from chrono.

Related Issues (20)

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.