Comments (9)
A single person requesting this feature is not enough, in this case, to add this feature, I think. Let's keep this open and see if other people turn up that want/need this?
from chrono.
For integers saturation operations make sense because they have a commonly agreed-upon range.
For dates and datetimes the range we support is somewhat arbitrary, and min and max are way outside the sensible range of dates. Saturating operations would make the min and max values more prominent, and I don't think that is good thing.
from chrono.
How can I know if there were any time gaps so that I could skip them?
If the time zone is UTC there are no gaps, Utc::now().checked_sub_days(chrono::Days::new(30))
will always succeed unless your pc clock is set 250.000+ years into the past. Or were you thinking of another cause?
If the time zone is Local
or something else that supports DST it is best to work with methods that return LocalResult
, en we will use it as the return value of more methods in chrono 0.5. Let me need if I should write an example.
from chrono.
I encountered a case where I need to add Duration
to NativeDate
with saturation. There's a function that generates intervals of dates (NativeDate
), with some step (Duration
).
It makes sense to have step = Duration::max_value()
if the caller wants a single interval. Saturating add seemd quite helpful there.
pub fn generate_date_ranges(
start: NaiveDate,
end: NaiveDate,
step: Duration,
) -> Vec<RangeInclusive<NaiveDate>> {
let mut date_range = Vec::new();
let mut next_start = start;
while next_start < end {
let next_end = next_start.saturating_add(step);
date_range.push(RangeInclusive::new(next_start, next_end));
next_start = next_end;
}
date_range
}
from chrono.
Hey @djc. I'm writing a code that needs to pass DateTime<Utc>
as a filter to a DB request to specify a time range for the calculation to be limited to the past 30 days. I don't see any good way to handle the None
case of Utc::now().checked_sub_days(chrono::Days::new(30))
. I just want a date that is exactly 30 days from now (or lower if at the edge of timestamp starting points). How can I know if there were any time gaps so that I could skip them? How should I skip them? I think a saturating subtraction method would need to handle this.
Maybe I landed on a wrong issue, but the main comment is that I'd like a method that gracefully handles gaps in time and skips them when doing subtraction even if it doesn't saturate at the bounds of allowed date-time values. I can just use a default DateTime::MIN_UTC
instead. In fact, I think checked_sub
method should return a LocalResult
instead of option so that I could act upon timestamps ambiguity or out-of-range errors but it should still handle gaps.
from chrono.
@pitdicker was thinking about this: if the result of a calculation is None
, IIRC that means it falls into a gap. I read the previous comment as wondering if there was a way to get at the gap boundaries, which seems like a reasonable request. Do we have something like that today? (I don't think so.) How hard do you think that would be to add?
from chrono.
Local::now().naive_local().checked_sub_days(chrono::Days::new(30))?.and_local_timezone(Local)
should do the same and return a LocalResult
. earliest()
and latest()
can then be used for the gap boundaries.
Quoting from #1448:
Methods on
DateTime
currently don't return aLocalResult
, but consider any result that falls in a timezone transition as an error. This makes it cumbersome for users to handle these cases correctly. They have to work on aNaiveDateTime
and then convert it back withTimeZone::from_local_datetime
. Returning aLocalResult
would be a big improvement for correctness in my opinion.
Sorry for being absent again. I really need to continue working on this for 0.5...
from chrono.
If the time zone is UTC there are no gaps
Oh, right... I was confused by the method signature and docs mentioning gaps. It is indeed basically infallible in UTC, so there is no problem in my case
from chrono.
Local::now().naive_local().checked_sub_days(chrono::Days::new(30))?.and_local_timezone(Local)
should do the same and return aLocalResult
.earliest()
andlatest()
can then be used for the gap boundaries.
earliest()
and latest()
don't actually return gap boundaries, right? They only yield a value for Single
and Ambiguous
which I suppose don't apply here.
from chrono.
Related Issues (20)
- "input is not enough" for DateTime::parse_from_str but works for Utc.datetime_from_str HOT 2
- Deprecations causing large amount of churn, and result in less elegant code HOT 3
- Compile error using most recent version of chrono 0.4.36 HOT 2
- Version 0.4.36 has breaking changes with 0.4.35 HOT 2
- the trait bound `DateTime<Utc>: OptionFrom/ToWasmAbi` is not satisfied when targeting wasm32 HOT 3
- 0.5: Consider using smaller types for arguments
- Make Weekday::num_days_from public HOT 2
- 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
- Datetime Parse Error when converting rfc2822 date to chrono Datetime HOT 2
- Extra slow Utc to Local conversion HOT 6
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.