php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #68029 PHP and monotonic date time
Submitted: 2014-09-16 16:07 UTC Modified: 2017-01-02 20:31 UTC
From: roberto at spadim dot com dot br Assigned: ab (profile)
Status: Closed Package: Date/time related
PHP Version: 5.6.1RC1 OS: any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: roberto at spadim dot com dot br
New email:
PHP Version: OS:

 

 [2014-09-16 16:07 UTC] roberto at spadim dot com dot br
Description:
------------
Hi guys i didn't found a monotonic clock function

for example:

<?php
        system("date -s \"2014-09-16 13:01:00 BRT\"");sleep(1);
        echo microtime(1)."\n";
        system("date -s \"2014-09-16 13:01:00 BRT\"");
        echo microtime(1)."\n";
?>

this script output 
# php date.php
Tue Sep 16 13:01:00 BRT 2014
1410883261.0003
Tue Sep 16 13:01:00 BRT 2014
1410883260.0002

the clock is ok since i changed the date time, but, how to use a monotonic clock? i just found clock_gettime() using CLOCK_MONOTONIC with FPM (php-src/sapi/fpm/fpm/fpm_clock.c), but it don't export functions to php script space, maybe we could implement a monotonic clock function?

Test script:
---------------
<?php
        system("date -s \"2014-09-16 13:01:00 BRT\"");sleep(1);
        echo microtime(1)."\n";
        system("date -s \"2014-09-16 13:01:00 BRT\"");
        echo microtime(1)."\n";
?>


Expected result:
----------------
correct output, since it's a feature request


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-09-16 16:14 UTC] roberto at spadim dot com dot br
functions to be used

http://tdistler.com/2010/06/27/high-performance-timing-on-linux-windows


linux: int clock_gettime(clockid_t clock_id, struct timespec *tp);
http://pubs.opengroup.org/onlinepubs/000095399/functions/clock_getres.html

windows: BOOL WINAPI QueryPerformanceCounter(LARGE_INTEGER lpPerformanceCount);
http://msdn.microsoft.com/en-us/library/ms644904(v=VS.85).aspx
 [2014-09-16 16:16 UTC] roberto at spadim dot com dot br
example with linux clock_gettime

https://github.com/php/php-src/blob/128eda843f7dff487fff529a384fee3c5494e0f6/sapi/fpm/fpm/fpm_clock.c
 [2014-09-16 22:23 UTC] ab@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: ab
 [2014-09-16 22:23 UTC] ab@php.net
please try http://pecl.php.net/hrtime
 [2017-01-01 15:03 UTC] me at kelunik dot com
I don't think an extension solves this feature request. We need a monotonic time function in core, otherwise NTP and leap seconds will be a problem for event loops and others that rely on the elapsed time.
 [2017-01-02 18:42 UTC] ab@php.net
Of course, feel free to reopen this and discuss on internals. Portable parts of the hrtime ext can be reused for the implementation in the core, or hrtime could be taken in. Otherwise, the ticket is closed, because there's actually no functionality in the core, that would require monotonic time.

I can also add, that every library you base your loop on, usually provides its own timer APIs. Take libuv or libevent as a ref, best way should be to rely on their routines. The core time functions use gettimeofday() or reimplement it, it is a known fact. It doesn't hurt to have monotonic time in the core, though preferable are probably the timers provided by the base libs. Except you implement the event loop in pure PHP ofc, which is likely not the case.

Thanks.
 [2017-01-02 19:32 UTC] me at kelunik dot com
I can't reopen it, as I don't have a php.net account.

> I can also add, that every library you base your loop on, usually provides its own timer APIs. [...] Except you implement the event loop in pure PHP ofc, which is likely not the case.

Right, those provide their own APIs, but PHP does not. Implementing an event loop in pure PHP isn't that uncommon:

 - https://github.com/amphp/loop/blob/master/lib/NativeLoop.php
 - https://github.com/reactphp/event-loop/blob/master/src/StreamSelectLoop.php
 [2017-01-02 20:31 UTC] ab@php.net
What you link is a compatibility shim. It's unlikely one would want to run a pure PHP implementation in production, and for development it doesn't matter much. Nevertheless, some monotonic timer implementation in the core could be pursued. To workout the best course of action the topic should be to discussed on internals.

Thanks.
 [2017-01-02 21:19 UTC] me at kelunik dot com
Running a stream-select loop in production is actually pretty fine, as long as you don't have thousands of concurrent streams.

I'll post to internals soon.
 [2017-01-04 12:41 UTC] k1316820 at mvrht dot com
Here's an example what problems you might encounter when language of your choice doesn't have monotonic time functions. https://blog.cloudflare.com/how-and-why-the-leap-second-affected-cloudflare-dns/
 [2017-01-04 12:56 UTC] me at kelunik dot com
I already sent a mail to internals that mentions that one, but mail distribution seems broken currently.

http://news.php.net/php.internals/97502
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 04:01:28 2024 UTC