Skip to content

Fix DST caching bug by using strict timestamp arithmetic and add regression test - #215

Merged
Kevinrob merged 3 commits into
masterfrom
fix-dst-caching-issue
Mar 11, 2026
Merged

Fix DST caching bug by using strict timestamp arithmetic and add regression test#215
Kevinrob merged 3 commits into
masterfrom
fix-dst-caching-issue

Conversation

@Kevinrob

@Kevinrob Kevinrob commented Mar 7, 2026

Copy link
Copy Markdown
Owner

Description

This PR resolves issue #194, where cache entries could be incorrectly cached for nearly an hour during the Daylight Saving Time (DST) transition (Summer -> Winter).

Changes

  • Core Fix: Replaced \DateTimeImmutable::modify() with direct timestamp manipulation (setTimestamp()) for relative dates. This avoids the ambiguity of local time during DST transitions, as UNIX timestamps are absolute.
  • Regression Test: Added testDstTransitionDoesNotIncorrectlyCache in PrivateCacheTest. It mocks the system time precisely during the transition point in the Europe/Berlin timezone to ensure the fix works as expected.
  • Improved Test Isolation: Ensured the global Clock is reset to a NativeClock after time-sensitive tests.

Verification

  • Successfully reproduced the potential for error locally and verified that the new logic correctly identifies entries as stale during the critical transition window.
  • All 81 tests passed on PHP 8.5.

…ession test

This ensures that during DST transitions (Summer -> Winter), relative date calculations (like -1 seconds) are absolute and not affected by timezone ambiguity. Fixes #194
@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical bug where cache entries could be incorrectly marked as valid or stale for an extended period during Daylight Saving Time transitions. The fix involves a fundamental change in how cache expiration times are calculated, moving from potentially ambiguous date modifications to precise, absolute timestamp arithmetic. A dedicated regression test has been added to prevent future recurrences of this issue, ensuring robust caching behavior across time zone changes.

Highlights

  • DST Caching Fix: Replaced \DateTimeImmutable::modify() with direct timestamp manipulation (setTimestamp()) to prevent incorrect cache expiration during Daylight Saving Time transitions.
  • Regression Test: Introduced testDstTransitionDoesNotIncorrectlyCache in PrivateCacheTest to validate the fix by simulating a DST transition scenario.
  • Test Isolation Improvement: Ensured the global Clock is consistently reset to a NativeClock after time-sensitive tests to maintain test environment integrity.
Changelog
  • src/Strategy/GreedyCacheStrategy.php
    • Changed how cache entry expiration is calculated from Clock::now()->modify() to Clock::now()->setTimestamp($now->getTimestamp() + $ttl).
  • src/Strategy/PrivateCacheStrategy.php
    • Updated cache entry creation for 'no-cache' responses to use setTimestamp() for expiration.
    • Modified cache entry creation based on Cache-Control headers to use setTimestamp() for expiration.
    • Adjusted default cache entry creation to use setTimestamp() for expiration.
  • tests/PrivateCacheTest.php
    • Added testDstTransitionDoesNotIncorrectlyCache to simulate a DST transition and assert correct cache entry staleness.
    • Ensured the default timezone is restored and the Clock is reset to NativeClock in a finally block after the DST test.
Activity
  • No human activity (comments, reviews, etc.) has occurred on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Comment thread tests/PrivateCacheTest.php Outdated

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request correctly addresses a bug related to Daylight Saving Time transitions by using UNIX timestamps for cache expiration calculations instead of relative date modifications. The changes in GreedyCacheStrategy.php and PrivateCacheStrategy.php are sound. The addition of the regression test testDstTransitionDoesNotIncorrectlyCache is excellent, as it specifically targets the fixed bug and ensures it doesn't reappear. My feedback includes a couple of suggestions to further improve test isolation and code maintainability.

Comment thread src/Strategy/PrivateCacheStrategy.php Outdated
Comment thread tests/PrivateCacheTest.php
@Kevinrob
Kevinrob merged commit daffa41 into master Mar 11, 2026
16 checks passed
@Kevinrob
Kevinrob deleted the fix-dst-caching-issue branch March 11, 2026 06:58
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.

1 participant