GithubHelp home page GithubHelp logo

Comments (6)

pitdicker avatar pitdicker commented on September 26, 2024 1

DateTime compares values as equal if they are the same instant in time, acting as if both values are converted to UTC (which they are internally).

With a DateTime you can always tell if an event is at the same time, or tell the order events, independent of the time zone the DateTime is in.

from chrono.

jasobrown-rs avatar jasobrown-rs commented on September 26, 2024 1

Ahhh, ok. It's making sense to me now. It looks like my code choose the path which "makes things compile", but doesn't create the DateTime instances "correctly". (The comment intended for use cases such as deserializing a DateTime or passing it through FFI makes sense now).

fwiw, I saw a bunch of warnings when upgrading about deprecated functions, like this:

Deprecated since 0.4.27: Use TimeZone::from_utc_datetime() or DateTime::from_naive_utc_and_offset instead

And looks like I picked the less optimial choice (without reading the docs thoroughly). To wrap up, I did try out a more typical (and recommended) path, and the DateTime eqaulity check fails as expected

    let e_date = NaiveDateTime::new(
        NaiveDate::from_ymd_opt(2020, 1, 2).unwrap(),
        NaiveTime::from_hms_milli_opt(3, 4, 5, 660).unwrap(),
    );
    let e = FixedOffset::east_opt(0)
        .unwrap()
        .from_local_datetime(&e_date);
    let f_date = NaiveDateTime::new(
        NaiveDate::from_ymd_opt(2020, 1, 2).unwrap(),
        NaiveTime::from_hms_milli_opt(3, 4, 5, 660).unwrap(),
    );
    let f = FixedOffset::east_opt(18000)
        .unwrap()
        .from_local_datetime(&f_date);
    assert_eq!(e, f);  // fails!

I think I'm good here. Thank you for all the help, @pitdicker

from chrono.

pitdicker avatar pitdicker commented on September 26, 2024

from_naive_utc_and_offset is assuming your NaiveDateTimes are in UTC. So the resulting DateTimes are really not describing the same instant in time. You probably want to use TimeZone::from_local_datetime:

    let a = FixedOffset::east_opt(0).unwrap().from_local_datetime(
        NaiveDateTime::new(
            NaiveDate::from_ymd_opt(2020, 1, 2).unwrap(),
            NaiveTime::from_hms_milli_opt(3, 4, 5, 660).unwrap(),
        )
    );

from chrono.

BurntSushi avatar BurntSushi commented on September 26, 2024

Perhaps I'm misunderstanding here, but if a and b are not the same instant in time, why does assert_eq!(a, b) pass?

from chrono.

jasobrown-rs avatar jasobrown-rs commented on September 26, 2024

Yes, @BurntSushi is asking the question more directly :).

from chrono.

pitdicker avatar pitdicker commented on September 26, 2024

Sorry, I didn't read closely enough. You are creating two DateTimes from identical NaiveDateTimes, using a method that assumes the value is in UTC.

We have two kinds of methods to construct DateTimes. Some assume the NaiveDateTime is in UTC, some assume it is in local time.

DateTime::from_naive_utc_and_offset assumes the naive value is in UTC (it's in the method name). Also it is not really a method I recommend using. From the documentation:

This is a low-level method, intended for use cases such as deserializing a DateTime or passing it through FFI.`

NaiveDateTime::and_local_timezone and TimeZone::from_local_datetime are two alternatives that assume the naive value is in local time. Both have 'local' in the name.

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.