GithubHelp home page GithubHelp logo

Comments (3)

pitdicker avatar pitdicker commented on September 26, 2024 1

I am hoping to have DateTime::set_time at some point. Then you can write Local::now().set_time(NaiveTime::MIN).unwrap(). That also takes care of one remaining panic case from #1047 that I don't have a workaround for.

I'll leave this issue open though. Maybe today_at is a useful convenience method for Local to have.

from chrono.

pitdicker avatar pitdicker commented on September 26, 2024

I am afraid that is pretty much it.

let today = Local::now().date_naive();
let midnight = NaiveTime::MIN;
let today_at_midnight = Local.from_local_datetime(today.and_time(midnight)).unwrap(); // or handle the result

from chrono.

cecton avatar cecton commented on September 26, 2024

Thank you!! The tip with NaiveTime::MIN is neat, it can remove one of the unwrap().

In the end I made this helper because I use that pattern a lot, especially for 00:00.

pub trait TimeZoneExt: TimeZone {
    fn today_at(&self, hour: u32, min: u32, sec: u32) -> Option<DateTime<Self>>;
}

impl TimeZoneExt for Local {
    fn today_at(&self, hour: u32, min: u32, sec: u32) -> Option<DateTime<Local>> {
        Local
            .from_local_datetime(&Local::now().date_naive().and_hms_opt(hour, min, sec)?)
            .single()
    }
}

If you are interested to get this in the API of chrono please let me know and I will make a PR. (Adapted to use NaiveTime probably)

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.