Comments (7)
Days cannot be accurately represented in seconds without a reference instant due to the existence of leap seconds
That depends on what you mean by days. Unix Epoch timestamps don't include leap seconds, so incrementing the timestamp by 24*3600 always increases the time by one day. However if you want to count physical seconds, then you do need to worry about leap seconds. And if you have a scenario where you need to worry about leap seconds, then you somehow need to continually update a database of all past leap seconds, because it is impossible to predict when future leap seconds will be more than a few months in advance.
from chrono.
It should be noted that there should be two slightly different difference types for date and time:
- Created from two time points; we know that the resulting value has no ambiguity except for possible leap seconds (e.g. we already know the time zone so DST has no effect on the difference type). Note that ISO 8601 "intervals" are actually the difference plus the time point---seems that there are several different notions of "intervals" out there.
- Created directly; this is what we use when we are saying, e.g. "3 months before the certain time point".
Note that there should be only one difference type of the first kind while there can be multiple difference types of the second kind. So Chrono's eventual difference type of the first kind (which is definitely not time::Duration
, and I think it cannot also be std::time::Duration
---more on forthcoming issues) will be usable as a difference type of the second kind, but it won't be the only possible one. For example it is always possible to define RelativeMonth
type which adjusts the month in the local date, and implement std::ops::Sub
for appropriate combination of types. On the other hands, Chrono has explicitly made the choice to ignore leap seconds for most cases, so the leap second ambiguity in the difference type of the first kind should be not unexpected (and users are advised to use TAI or whatever to avoid that ambiguity).
It might be possible that we can just provide everything reasonable: the eventual difference type may directly support RelativeMonth
for example. I had been a firm opponent to this idea, but just in case, I've read the PostgreSQL documentation and... I must say it's convenient, if not confusing. So there are essentially three fields in the interval
type: year-month, day, everything else. The fractional intervals (e.g. 1.5 month
) are normalized assuming 30-day months and 24-hour days at the parsing time. Otherwise overflowing months and days are not normalized, and they are only "justified" with the dedicated functions (and the same assumption about months and days).
I will keep this issue open to track support for other difference types of the second kind.
from chrono.
there can be multiple difference types of the second kind
given this, I wonder if there should be a trait for this second time, so that users can easily create new types for this. For example a user could add a duration/interval that uses units of a non-Gregorian calendar, or more esoteric time units, like sidereal days. I'm not entirely sure what the interface of that trait would look like since it would need to work with Date, DateTime, NaiveTime, NaiveDate, and NaiveDateTime. My initial suggestion would be something like:
trait DurationLike {
fn addTo(&self, src: &NaiveDateTime) -> Option<NaiveDateTime>;
fn subFrom(&self, src: &NaiveDateTime) -> Option<NaiveDateTime>;
}
although that interface has some limitations, like the fact that it would be rather awkward to implement DurationLike
with the current NaiveDateTime since the main way to add or subtract to currently is with a time:Duration.
from chrono.
@tmccombs You don't need a separate trait, you can just impl Add<NaiveDateTime> for ObscureDuration
and so on. (Rust allows for multiple implementations when generic type parameters are disjoint enough.) Difference types of the first kind should be unique because it is defined via impl Sub<NaiveDateTime> for NaiveDateTime
, which output type (the associated type Sub::Output
) should be unique.
from chrono.
I suppose that's true. Although you would have to implement Add
for every Date and Time type.
from chrono.
Is there any progress on this? I would really like to have a bug free to iterate over last few months
from chrono.
We now have a dedicated issue on adding a CalendarDuration
type with these features: #1282.
from chrono.
Related Issues (20)
- Unexpected panics due to unwrap HOT 1
- Restart 0.5 branch? HOT 6
- input is not enough for unique date and time HOT 2
- Offer options to configure how to create rfc 3339 string HOT 6
- Split `clock` feature into `clock-now` and `clock-tz` HOT 6
- CI failures in MSRV jobs failing to pick up ahash version
- Operations on NaiveDate are not commutative. HOT 10
- Add `subsec` methods to `Duration` HOT 1
- Time formatting on en_US locale with '%X' not expected HOT 1
- FrameworkNotFound error when building for Apple using maturin HOT 1
- format paniced HOT 11
- panic with_timezone Jan 1, 1601 00:00 UTC HOT 32
- Add methods for float multiplication/division HOT 1
- panic with Local Timezone 2023/11/29 02:00:00 checked_sub_months HOT 2
- rfc HOT 1
- This API seems very complicated HOT 2
- Times before Unix epoch
- Truncating a timestamp close to EPOCH fails HOT 6
- Uhm build fails? HOT 14
- confusing deprecation notice for `chrono::TimeZone::datetime_from_str` HOT 7
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.