Comments (6)
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.
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.
from_naive_utc_and_offset
is assuming your NaiveDateTime
s are in UTC. So the resulting DateTime
s 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.
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.
Yes, @BurntSushi is asking the question more directly :).
from chrono.
Sorry, I didn't read closely enough. You are creating two DateTime
s from identical NaiveDateTime
s, using a method that assumes the value is in UTC.
We have two kinds of methods to construct DateTime
s. 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)
- Add a method to the `Offset` trait to return DST info HOT 5
- 0.5: Add a `LocalOffset` type HOT 4
- Verification of published packages HOT 2
- `gh-pages` branch HOT 3
- Releases has problematic naming of 0.4.8 release which puts it at the top HOT 3
- 0.4.37 semver-incompatibly removes trait bounds on `DateTime` HOT 10
- How do I get the raw underlying bytes of a `NaiveDate` HOT 4
- Parsing from string to DateTime - input is not enough for unique date and time HOT 3
- TimeDelta always returning a PT in seconds HOT 2
- NaiveDate::years_since wrong documentation
- Saturating operations HOT 9
- Datetime Parse Error when converting rfc2822 date to chrono Datetime HOT 2
- Extra slow Utc to Local conversion HOT 6
- Wasm support for NaiveTime, NaiveDate and alike HOT 2
- Bincode 2.0.0 support HOT 2
- Need Duration to Represent Time in Months/Years HOT 1
- DelayedFormat: Panic in rendering HOT 5
- Err(ParseError(TooShort)) when parsing datetime with trailing 0 HOT 4
- Support for `24:00:00` in `DateTime` Parsing HOT 2
- Century cutoff for %y undocumented
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chrono.