You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current arithmetic API requires verbose method calls:
let tomorrow = date.update(DateUnit::Day,1)?;let last_week = datetime.update(DateTimeUnit::Day, -7)?;
Rust's std::ops::Add and std::ops::Sub traits enable a far more natural syntax that matches how developers think about time arithmetic, and is consistent with how chrono itself works:
Additionally implement Add<chrono::Duration> / Sub<chrono::Duration> for interop with existing chrono-using code:
let dt = DateTime::new(2024,1,1)?.with_time(0,0,0)?;let later = (dt + chrono::Duration::hours(3))?;
Implementation Notes
Output = Result<T, SpanError> is necessary because some additions can fail (e.g. arithmetic overflow, invalid resulting date). This is an unusual but correct use of Add.
Alternatively, Output = T with a panic on error could be offered as an unchecked variant, but the Result-returning form should be the default.
Weeks and Years should delegate to their Day and Month equivalents respectively (× 7 and × 12), consistent with how update works.
All unit wrappers should be Copy + Clone + Debug.
Feature-gate: unit wrappers only available when the corresponding feature (date, time, datetime) is active.
Acceptance Criteria
timeflow::units module with Years, Months, Weeks, Days, Hours, Minutes, Seconds newtypes
All newtypes re-exported from timeflow::prelude
Add and Sub implemented for all valid (Type, Unit) combinations
Add<chrono::Duration> / Sub<chrono::Duration> implemented for all three types
Output = Result<T, SpanError> for all implementations
Unit tests for: normal addition, subtraction, overflow/error path, month-end clamping
Summary
The current arithmetic API requires verbose method calls:
Rust's
std::ops::Addandstd::ops::Subtraits enable a far more natural syntax that matches how developers think about time arithmetic, and is consistent with howchronoitself works:Proposed Design
Unit value wrapper types
These are lightweight newtypes in a
timeflow::unitsmodule, re-exported fromprelude.Trait implementations
Full matrix:
{Date, DateTime} × {Years, Months, Weeks, Days}and{DateTime, Time} × {Hours, Minutes, Seconds}.Usage
Alternative:
chrono::DurationoverloadAdditionally implement
Add<chrono::Duration>/Sub<chrono::Duration>for interop with existingchrono-using code:Implementation Notes
Output = Result<T, SpanError>is necessary because some additions can fail (e.g. arithmetic overflow, invalid resulting date). This is an unusual but correct use ofAdd.Output = Twith apanicon error could be offered as anuncheckedvariant, but theResult-returning form should be the default.WeeksandYearsshould delegate to theirDayandMonthequivalents respectively (× 7 and × 12), consistent with howupdateworks.Copy + Clone + Debug.date,time,datetime) is active.Acceptance Criteria
timeflow::unitsmodule withYears,Months,Weeks,Days,Hours,Minutes,Secondsnewtypestimeflow::preludeAddandSubimplemented for all valid(Type, Unit)combinationsAdd<chrono::Duration>/Sub<chrono::Duration>implemented for all three typesOutput = Result<T, SpanError>for all implementationstimeflow::unitsmodule