Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Zend/Optimizer/zend_func_infos.h
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,8 @@ static const func_info_t func_infos[] = {
F1("dechex", MAY_BE_STRING),
F1("base_convert", MAY_BE_STRING),
F1("number_format", MAY_BE_STRING),
#if defined(HAVE_GETTIMEOFDAY)
F1("microtime", MAY_BE_STRING|MAY_BE_DOUBLE),
F1("gettimeofday", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_DOUBLE),
#endif
#if defined(HAVE_GETRUSAGE)
F1("getrusage", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_FALSE),
#endif
Expand Down Expand Up @@ -597,9 +595,7 @@ static const func_info_t func_infos[] = {
F1("stream_resolve_include_path", MAY_BE_STRING|MAY_BE_FALSE),
F1("stream_get_wrappers", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
F1("stream_get_transports", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
#if defined(HAVE_GETTIMEOFDAY)
F1("uniqid", MAY_BE_STRING),
#endif
F1("parse_url", MAY_BE_LONG|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_NULL|MAY_BE_FALSE),
F1("urlencode", MAY_BE_STRING),
F1("urldecode", MAY_BE_STRING),
Expand Down
55 changes: 55 additions & 0 deletions Zend/zend_time.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Marc Bennewitz <marc@mabe.berlin> |
+----------------------------------------------------------------------+
*/

#include "zend_time.h"

/* Current real/wall-time in seconds */
ZEND_API time_t zend_time_real_get(void) {
return time(NULL);
}

ZEND_API void zend_time_real_spec(struct timespec *ts) {
#if defined(HAVE_CLOCK_GETTIME)

(void) clock_gettime(CLOCK_REALTIME, ts);

#elif defined(HAVE_TIMESPEC_GET)

(void) timespec_get(ts, TIME_UTC);

#elif defined(HAVE_GETTIMEOFDAY)

struct timeval tv;
(void) gettimeofday(&tv, NULL);
zend_time_val2spec(tv, ts);

#else

ts->tv_sec = zend_time_real_get();
ts->tv_nsec = 0;

#endif
}

ZEND_API uint64_t zend_time_mono_fallback(void) {
#if ZEND_HRTIME_AVAILABLE
return (uint64_t)zend_hrtime();
#else
struct timespec ts;
zend_time_real_spec(&ts);
return ((uint64_t) ts.tv_sec * ZEND_NANO_IN_SEC) + ts.tv_nsec;
#endif
}
95 changes: 95 additions & 0 deletions Zend/zend_time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Marc Bennewitz <marc@mabe.berlin> |
+----------------------------------------------------------------------+
*/

#ifndef ZEND_TIME_H
#define ZEND_TIME_H

#include "zend_portability.h"

#ifdef PHP_WIN32
# include "win32/time.h"
#endif
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#include <time.h>

#include "zend_hrtime.h"

#ifndef PHP_WIN32
# define tv_sec_t time_t
# define tv_usec_t suseconds_t
#else
# define tv_sec_t long
# define tv_usec_t long
#endif

#define ZEND_MILLI_IN_SEC 1000U
#define ZEND_MICRO_IN_SEC 1000000U

BEGIN_EXTERN_C()

/* Assign seconds to timeval */
static zend_always_inline void zend_time_sec2val(time_t s, struct timeval *tv) {
tv->tv_sec = (tv_sec_t) s;
tv->tv_usec = 0;
}

/* Assign microseconds to timeval */
static zend_always_inline void zend_time_usec2val(int64_t usec, struct timeval *tv) {
tv->tv_sec = (tv_sec_t) (usec / ZEND_MICRO_IN_SEC);
tv->tv_usec = (tv_usec_t) (usec % ZEND_MICRO_IN_SEC);

if (UNEXPECTED(tv->tv_usec < 0)) {
tv->tv_usec += ZEND_MICRO_IN_SEC;
tv->tv_sec -= 1;
}
}

/* Assign double (seconds) to timeval */
static zend_always_inline void zend_time_dbl2val(double s, struct timeval *tv) {
tv->tv_sec = (tv_sec_t) s;
tv->tv_usec = (tv_usec_t) ((s - tv->tv_sec) * ZEND_MICRO_IN_SEC);

if (UNEXPECTED(tv->tv_usec < 0)) {
tv->tv_usec += ZEND_MICRO_IN_SEC;
tv->tv_sec -= 1;
} else if (UNEXPECTED(tv->tv_usec >= ZEND_MICRO_IN_SEC)) {
// rare, but protects against rounding up to exactly 1 second
tv->tv_usec -= ZEND_MICRO_IN_SEC;
tv->tv_sec += 1;
}
}

/* Assign timeval to timespec */
static zend_always_inline void zend_time_val2spec(struct timeval tv, struct timespec *ts) {
ts->tv_sec = (time_t) tv.tv_sec;
ts->tv_nsec = (long) (tv.tv_usec * 1000);
}

/* Current real/wall-time in seconds */
ZEND_API time_t zend_time_real_get(void);

/* Current real/wall-time in up-to nano seconds */
ZEND_API void zend_time_real_spec(struct timespec *ts);

/* Monotonic time in nanoseconds with a fallback to real/wall-time
if no monotonic timer is available */
ZEND_API uint64_t zend_time_mono_fallback(void);

END_EXTERN_C()

#endif // ZEND_TIME_H
4 changes: 2 additions & 2 deletions Zend/zend_virtual_cwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>

#include "zend.h"
#include "zend_virtual_cwd.h"
#include "zend_time.h"

#ifdef ZEND_WIN32
#include <io.h>
Expand Down Expand Up @@ -577,7 +577,7 @@ static size_t tsrm_realpath_r(char *path, size_t start, size_t len, int *ll, tim
if (start && save && CWDG(realpath_cache_size_limit)) {
/* cache lookup for absolute path */
if (!*t) {
*t = time(0);
*t = zend_time_real_get();
}
if ((bucket = realpath_cache_find(path, len, *t)) != NULL) {
if (is_dir && !bucket->is_dir) {
Expand Down
3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ AC_CHECK_FUNCS(m4_normalize([
asctime_r
asprintf
chroot
clock_gettime
ctime_r
explicit_memset
fdatasync
Expand Down Expand Up @@ -591,6 +592,7 @@ AC_CHECK_FUNCS(m4_normalize([
strptime
strtok_r
symlink
timespec_get
tzset
unsetenv
usleep
Expand Down Expand Up @@ -1759,6 +1761,7 @@ PHP_ADD_SOURCES([Zend], m4_normalize([
zend_generators.c
zend_hash.c
zend_highlight.c
zend_time.c
zend_hrtime.c
zend_inheritance.c
zend_ini_parser.c
Expand Down
2 changes: 1 addition & 1 deletion docs-old/streams.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ PHPAPI php_stream *php_stream_sock_open_from_socket(int socket, int persistent);
/* Convert a socket into a stream. */

PHPAPI php_stream *php_stream_sock_open_host(const char *host, unsigned short port,
int socktype, int timeout, int persistent);
int socktype, struct timeval *timeout, const char *persistent_id);
/* Open a connection to a host and return a stream. */

PHPAPI php_stream *php_stream_sock_open_unix(const char *path, int persistent,
Expand Down
4 changes: 2 additions & 2 deletions ext/calendar/cal_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "php.h"
#include "php_calendar.h"
#include "sdncal.h"
#include <time.h>
#include "zend_time.h"

#define SECS_PER_DAY (24 * 3600)

Expand All @@ -36,7 +36,7 @@ PHP_FUNCTION(unixtojd)
}

if (tl_is_null) {
ts = time(NULL);
ts = zend_time_real_get();
} else if (tl >= 0) {
ts = (time_t) tl;
} else {
Expand Down
5 changes: 2 additions & 3 deletions ext/calendar/easter.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "php.h"
#include "php_calendar.h"
#include "sdncal.h"
#include <time.h>
#include "zend_time.h"

/**
* If `gm` is true this will return the timestamp at midnight on Easter of the given year. If it is false this
Expand All @@ -43,9 +43,8 @@ static void _cal_easter(INTERNAL_FUNCTION_PARAMETERS, bool gm)

/* Default to the current year if year parameter is not given */
if (year_is_null) {
time_t a;
time_t a = zend_time_real_get();
struct tm b, *res;
time(&a);
res = php_localtime_r(&a, &b);
if (!res) {
year = 1900;
Expand Down
Loading