Skip to content

Fix Time comparison to ignore metadata and date context - #281

Merged
kylekatarnls merged 3 commits into
spatie:masterfrom
Amunak:fix/time-comparison-by-clock
Jul 9, 2026
Merged

Fix Time comparison to ignore metadata and date context#281
kylekatarnls merged 3 commits into
spatie:masterfrom
Amunak:fix/time-comparison-by-clock

Conversation

@Amunak

@Amunak Amunak commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Time::isBefore() and Time::isAfter() were using PHP object comparison, which is quite brittle. It looked at the $data and $date fields. Two times with the same hours and minutes could be ordered differently just because they had different metadata attached.

This fix makes it so that we compare only the clock time (hours and minutes), matching what isSame() already did.

Bug reproduction

Before the fix, these failed:

  • Time::fromString('09:00', data: ['label' => 'morning']) vs Time::fromString('09:00', data: ['label' => 'opening']): same clock time, but one could be considered before/after the other
  • Same clock time with different date reference points was also incorrectly ordered

Changes

  • Compare times by total minutes (hours * 60 + minutes) instead of object comparison.
  • Route isSame(), isBefore(), and isAfter() through this same comparison path.
  • Added regression tests for metadata/date-insensitive comparison and 24:00 ordering.

isBefore() and isAfter() previously used PHP object comparison operators,
which compare all object properties including data and date fields. This
caused times with identical clock values but different metadata to be
ordered incorrectly.

Replace object comparison with an explicit clock-minutes comparison path
shared by isSame(), isBefore(), and isAfter(). Add regression tests for
metadata/date-insensitive comparison and 24:00 ordering.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b597413807

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/Time.php Outdated
@kylekatarnls
kylekatarnls force-pushed the fix/time-comparison-by-clock branch from b0191f5 to e78cb17 Compare July 9, 2026 19:22
@kylekatarnls
kylekatarnls merged commit 4618d11 into spatie:master Jul 9, 2026
11 checks passed
@kylekatarnls

Copy link
Copy Markdown
Collaborator

Fixed in 4.2.2

@Amunak

Amunak commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the quick merge! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants