Skip to content

Introduce new time retrieval Zend-API - #21370

Open
marc-mabe wants to merge 1 commit into
php:masterfrom
marc-mabe:new-time-retrieval-api
Open

Introduce new time retrieval Zend-API#21370
marc-mabe wants to merge 1 commit into
php:masterfrom
marc-mabe:new-time-retrieval-api

Conversation

@marc-mabe

Copy link
Copy Markdown
Contributor

This is split out from #19202 to first introduce the new time retrieval API. Later PRs will follow to refactor time retrieval usages.

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_time_real_spec
    A unified wrapper around clock_gettime(), timespec_get(), gettimeofday(), and time() using the real/wall clock.
    Returns a timespec structure with up-to nanosecond precision.

  • Added zend_time_real_sec
    A lightweight wrapper around time(NULL) for simple, low-resolution time retrieval in seconds.

  • Added zend_time_mono_fallback_nsec
    A wrapper for zend_hrtime or falls back to zend_time_real_spec.
    Useful for time measurements (e.g. timeout handling) where monotonic time is preferred, but wall time is an acceptable fallback.

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

The time retrieval functions are not inlined to make it possible to be mocked in unit tests.

@TimWolla TimWolla left a comment

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.

The time retrieval functions are not inlined to make it possible to be mocked in unit tests.

Is it really necessary to mock them rather than mocking their implementation default? Not allowing them to be inlined is likely going to make them expensive for basically only being a wrapper.

Comment thread Zend/zend_time.h Outdated
Comment thread Zend/zend_time.h Outdated
Comment thread Zend/zend_time.h Outdated
@marc-mabe

Copy link
Copy Markdown
Contributor Author

Is it really necessary to mock them rather than mocking their implementation default? Not allowing them to be inlined is likely going to make them expensive for basically only being a wrapper.

I have inlined zend_time_real_sec as this wraps time() only. For mocking zend_time_real_spec and zend_time_mono_fallback_nsec it would require to know which underlying function to mock (as in

int __wrap_gettimeofday(struct timeval *time_Info, struct timezone *timezone_Info)
) effectively repeating the pre-compiler logic in the unit test.

@kocsismate

kocsismate commented Apr 10, 2026

Copy link
Copy Markdown
Member

My #21715 PR desperately needs this API, so I would appreciate if it could be merged soon. Thanks, Marc, for working on this topic!

@marc-mabe
marc-mabe force-pushed the new-time-retrieval-api branch 2 times, most recently from d35c01c to 9c071b7 Compare August 31, 2026 06:56
@marc-mabe

Copy link
Copy Markdown
Contributor Author

@TimWolla @derickr Now all functions do return instead of pointer-out.
Hope it's good to go now.

@marc-mabe
marc-mabe force-pushed the new-time-retrieval-api branch from 9c071b7 to 55f4ef9 Compare August 31, 2026 07:11
@ndossche
ndossche removed their request for review August 31, 2026 09:16
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.

5 participants