Repository navigation
Hijri date math and comparisons (8.4.x) - #39
Merged
Merged
Conversation
A Hijri instance stored the Hijri date as a Gregorian one (1445 AD), so its timestamp, comparisons and differences were about 579 years off, and Carbon's date math applied Gregorian month lengths to the Hijri numbers. Keep the real date and time, and read the year, month and day from it with the instance's adjustment. Day and time math, comparisons and timestamps now work with any other date, and months, quarters, years and larger units, setDate() and the year/month/day setters, start/end and first/last/nth boundaries, month and year differences and same-month checks follow the Hijri calendar. format(), isoFormat() and the readable to*String() methods still print the Hijri date; rawFormat(), the timestamp and the ISO/JSON strings give the Gregorian one. diffInMonths() and diffInYears() return an int in Carbon 2 and a float in Carbon 3, so their overrides are loaded for the installed version.
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.
Summary
Fixes the two remaining issues from the laravel-hijri PR #3 review: date math and comparisons on a
Hijriinstance.Hijriinstance used to store the Hijri date as a Gregorian one (year 1445 AD), sogetTimestamp(),eq(),lt(),between(),isPast(),diffInDays()anddiffForHumans()were about 579 years off. For example,Hijri::fromGregorian('2024-03-11')->diffInDays(Carbon::parse('2024-03-11'))returned211302. The instance now holds the real date and time, and reads the year, month and day from it with the adjustment it was converted with.addMonths(),add(),sub(),CarbonInterval,DateIntervalandCarbonPeriod. From 29 Safar 1445,addDay()now gives 1 Rabi' Al-Awwal (it gave the 2nd) andaddMonth()gives 29 Rabi' Al-Awwal (it gave 1 Rabi' Al-Akher).setDate()and theyear/month/daysetters take Hijri values.daysInMonth,dayOfYear,isLeapYear(),diffInMonths(),diffInYears(),age,isSameMonth(),isSameYear()andisBirthday()are Hijri.format(),isoFormat(),toDateString(),toDateTimeString()and(string)still print the Hijri date.rawFormat(),getTimestamp(),toISOString()andjson_encode()give the Gregorian date, so APIs receive a standard date.Hijri::now(),Hijri::create()andHijri::createFromFormat()now return Hijri dates with Hijri month names.fromGregorian()andtoHijri()keep the exact moment of aDateTimeInterface.serialize()keeps the date, timezone and adjustment.diffInMonths()/diffInYears()returnintin Carbon 2 andfloatin Carbon 3, sosrc/Concerns/HijriDifference.phploads the trait for the installed version.tests/phpstan-carbon.phpmakes PHPStan analyse only that trait.HijriDateMathTestcovering:Behaviour changes (listed under "Changed" in the changelog):
getTimestamp()and the ISO/JSON strings are now Gregorian.toGregorian()no longer converts a changed instance's values or throws.Hijriproperties used for the old representation were removed.Verified on each line's own PHP version with the lowest and highest dependencies, and also with Carbon 2.55.0, 2.62.1, 2.73, 3.0.0 and 3.14.