feat(duration): build a duration from a native DateInterval - #151
Merged
Conversation
Duration accepted only three numeric arguments. A caller that already has a DateInterval had to split it into months, days and nanoseconds by hand. The constructor now accepts a DateInterval as its only argument, and Duration::fromDateInterval() does the same as a named factory. Years fold into months, hours and smaller parts fold into nanoseconds, and an inverted interval gives a negative duration. A DateInterval together with the other two arguments throws InvalidArgumentException. Three arguments stay mandatory for the numeric form, so a short call throws ArgumentCountError. The conversion rejects an interval that does not fit in the CQL duration range. Months and days are 32 bit and nanoseconds are 64 bit.
|
Tick the box to add this pull request to the merge queue (same as
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Cassandra\Durationnow accepts PHP's nativeDateInterval.Why
The class took three numeric arguments only. A caller who already had a
DateInterval— fromDateTime::diff(), from Carbon, from a config value — had to split it into months, days, and nanoseconds by hand and get the calendar arithmetic right.Note: PHP has no
Durationclass.DateIntervalis the native type for this.Mapping
y,mmonths=y * 12 + mddaysh,i,s,fnanosinvertfholds microseconds, so the last three digits ofnanos()are always zero.Rules
DateIntervalmust be the only argument. Passing it with$days/$nanosthrowsInvalidArgumentException, because the other two values would be discarded.ArgumentCountError. The stub marks arguments 2 and 3 as optional to allow the one-argument form, so this check moved from arginfo into the constructor.monthsanddaysare 32 bit,nanosis 64 bit. Every intermediate product is checked for overflow.DateIntervalis rejected.DateIntervalsubclasses work,Carbon\CarbonIntervalincluded.Changes
src/DateTime/Duration.stub.php,src/DateTime/Duration.ctests/Unit/DurationTest.php— 16 new casesdocs/Cassandra/Duration.php,website/reference/values.md,website/guide/data-types.mdTest
Not in this change
toDateInterval(). The reverse direction loses precision below microseconds and needs its own decision about what to do with the remainder.