Skip to content

Fix ChronosDate ordinal relative modifiers - #533

Open
dereuromark wants to merge 4 commits into
3.xfrom
fix/chronos-date-second-ordinal
Open

dereuromark wants to merge 4 commits into
3.xfrom
fix/chronos-date-second-ordinal

Conversation

@dereuromark

@dereuromark dereuromark commented Sep 16, 2026 •

Copy link
Copy Markdown
Member

Summary

  • parse relative modifiers before rejecting time components, so ordinal expressions such as second monday of this month remain valid
  • reject uppercase, abbreviated, fractional, and wrapped-day time modifiers
  • reject negative time components passed to add() and sub()
  • preserve timestamp modifiers and date arithmetic across time zones that skip midnight

Closes #532

@dereuromark
dereuromark marked this pull request as ready for review September 16, 2026 12:48
@markstory markstory added this to the 3.x milestone Sep 19, 2026
Comment thread src/ChronosDate.php Outdated
$parsed = date_parse($modifier);
$relative = $parsed['relative'] ?? [];
$timestampSeconds = 0;
if (preg_match('/(?:^|\s)@(-?\d+)(?:\.\d+)?(?:\s|$)/', $modifier, $matches)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it be simpler to check native after the modify() call and see if the new value has seconds/milliseconds/microseconds set?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I tried that first, but it misses cases that land back on a clean time: +24 hours ends at midnight the next day and 1000 ms ends at 00:00:01.000000. It would also reject 12:00 and +1 day in zones that skip midnight (Havana 2023-03-12 gives 01:00). So the check stays before modify(). I added a comment saying why.

A post-modify time check cannot see '+24 hours' or '1000 ms', which land
back on midnight or whole seconds, and would reject clock times and dates
in time zones that skip midnight.

@josbeir josbeir left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All 993 tests pass, but there's a timestamp compatibility issue that the current tests don't cover.

Comment thread src/ChronosDate.php Resolved
PHP accepts @86400+1 day, @86400UTC, @86400+00:00 and +1 day@86400.
The anchored regex missed them, so the timestamp's seconds were treated
as a time adjustment and the modifier was rejected.

@josbeir josbeir left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The timestamp whitespace cases are fixed, and all 999 tests pass on PHP 8.5.12. There's still one timestamp case that needs coverage.

Comment thread src/ChronosDate.php Resolved
date_parse() counts an @timestamp as relative seconds and negates them
for 'ago', so '@86400 1 day ago' was rejected as a time adjustment.
Removing the timestamp before parsing drops the bookkeeping that tried
to cancel those seconds out.
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.

ChronosDate::modify() fails on 'second' relative keywords

3 participants