Skip to content

Conversation

marc-mabe
Copy link
Contributor

@marc-mabe marc-mabe commented Jul 21, 2025

This pull request refactors how PHP internally handles current time retrieval and time measurement, introducing a more consistent and portable approach. The primary goals are to encapsulate platform-specific differences, improve time resolution, and prepare for long-term compatibility (e.g., Y2038 on WIN64).

Key Changes

  • Introduced zend_time.h
    A new internal header that abstracts system time functions, replacing direct usage of <time.h> and related platform-specific APIs.

  • Added zend_realtime_spec
    A unified wrapper around clock_gettime(), timespec_get(), gettimeofday(), and time() using the real/wall clock.
    Returns a timespec structure with nanosecond precision (when available).

  • Defined zend_realtime_get macro
    A lightweight macro wrapping time(NULL) for simple, low-resolution time retrieval.

  • Introduced zend_monotime_fallback
    A macro that maps to zend_hrtime or falls back to zend_realtime_spec.
    Useful for time measurements (e.g. timeout handling) where monotonic time is preferred, but wall time is an acceptable fallback.

  • Provided helper macros
    Added utilities to simplify usage of timeval and timespec structures.

  • Replaced direct system time API usage
    Internal calls to system time functions now use zend_realtime_* or zend_monotime_fallback.

  • Standardized time representation
    Transitioned to timespec for current time where appropriate, while retaining timeval for files and stream-related functionality.

Benefits

  • Improved Portability and Readability
    Platform-specific time logic is encapsulated, reducing conditionals and improving clarity.

  • Guaranteed Time API Availability
    The new abstraction ensures time can always be retrieved via a fallback chain:
    clock_gettime()timespec_get()gettimeofday()time()

  • Y2038 Compatibility on WIN64
    Addresses issues caused by long in timeval.tv_sec on 64-bit Windows systems.

    Related issue: #17856 – time() and Y2038 problem on 64-bit Windows

  • Improved Resolution for Time Functions
    microtime() and gettimeofday(true) now offer better time precision without breaking backward compatibility.

  • Removed Redundant Availability Checks
    Previously unverified use of gettimeofday() has been replaced by a robust fallback mechanism.
    Conditional checks for microtime(), gettimeofday(), and uniqid() have been removed.

@marc-mabe marc-mabe marked this pull request as ready for review July 25, 2025 08:12
@marc-mabe marc-mabe force-pushed the current_time_wrapper branch 2 times, most recently from 7e3d98c to fdabd9b Compare July 26, 2025 08:46
@marc-mabe marc-mabe force-pushed the current_time_wrapper branch from 1058493 to 9429859 Compare July 28, 2025 07:02
@marc-mabe marc-mabe requested a review from devnexen as a code owner September 23, 2025 06:12
@marc-mabe marc-mabe force-pushed the current_time_wrapper branch from 315f346 to 956b7a0 Compare October 4, 2025 19:57
@marc-mabe marc-mabe changed the title Refactor basic time usages Refactor Internal Time Retrieval Handling for Improved Consistency and Resolution Oct 5, 2025
@marc-mabe marc-mabe force-pushed the current_time_wrapper branch from 956b7a0 to 88ec473 Compare October 5, 2025 05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants