Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Refactor Internal Time Retrieval Handling for Improved Consistency and Resolution #19202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
marc-mabe wants to merge 6 commits into php:master
base: master
Choose a base branch
Loading
from marc-mabe:current_time_wrapper
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
View file Open in desktop
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
40 changes: 40 additions & 0 deletions Zend/zend_time.c
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
+----------------------------------------------------------------------+
| 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"

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

clock_gettime(CLOCK_REALTIME, ts);

#elif defined(HAVE_TIMESPEC_GET)

timespec_get(ts, TIME_UTC);

#elif defined(HAVE_GETTIMEOFDAY)

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

#else

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

#endif
}
85 changes: 85 additions & 0 deletions Zend/zend_time.h
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
+----------------------------------------------------------------------+
| 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

/* Helper macro to assign seconds to timeval */
#define zend_time_sec2val(s, tv) \
(tv).tv_sec = (tv_sec_t) (s); \
(tv).tv_usec = 0;

/* Helper macro to assign microseconds to timeval */
#define zend_time_usec2val(usec, tv) \
(tv).tv_sec = (tv_sec_t) ((usec) / ZEND_MICRO_IN_SEC); \
(tv).tv_usec = (tv_usec_t) ((usec) % ZEND_MICRO_IN_SEC);

/* Helper macro to assign double (seconds) to timeval */
#define zend_time_dbl2val(d, tv) \
(tv).tv_sec = (tv_sec_t) (d); \
(tv).tv_usec = (tv_usec_t) (((d) - (tv).tv_sec) * ZEND_MICRO_IN_SEC);

/* Helper macro to copy timeval to timespec */
#define zend_time_val2spec(tv, ts) \
(ts).tv_sec = (time_t) (tv).tv_sec; \
(ts).tv_nsec = (long) ((tv).tv_usec * 1000);

BEGIN_EXTERN_C()

/* Helper macro to get current timestamp in seconds */
#define zend_realtime_get() time(NULL)

/* wrapper around clock_gettime/timestamp_get/gettimeofday/time */
ZEND_API void zend_realtime_spec(struct timespec *ts);

/* Monotonic time in nanoseconds with a fallback to real/wall-time
if no monotonic timer is available */
#if ZEND_HRTIME_AVAILABLE
# define zend_monotime_fallback() (uint64_t)zend_hrtime()
#else
ZEND_API zend_always_inline uint64_t zend_monotime_fallback(void) {
struct timespec ts;
zend_realtime_spec(&ts);
return ((uint64_t) ts.tv_sec * ZEND_NANO_IN_SEC) + ts.tv_nsec;
}
#endif

END_EXTERN_C()

#endif // ZEND_TIME_H
4 changes: 2 additions & 2 deletions Zend/zend_virtual_cwd.c
View file Open in desktop
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_realtime_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
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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_realtime_get();
} else if (tl >= 0) {
ts = (time_t) tl;
} else {
Expand Down
5 changes: 2 additions & 3 deletions ext/calendar/easter.c
View file Open in desktop
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_realtime_get();
struct tm b, *res;
time(&a);
res = php_localtime_r(&a, &b);
if (!res) {
year = 1900;
Expand Down
53 changes: 14 additions & 39 deletions ext/date/php_date.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@
#include "zend_attributes.h"
#include "zend_interfaces.h"
#include "zend_exceptions.h"
#include "zend_time.h"
#include "lib/timelib.h"
#include "lib/timelib_private.h"
#ifndef PHP_WIN32
#include <time.h>
#else
#include "win32/time.h"
#endif

#ifdef PHP_WIN32
static __inline __int64 php_date_llabs( __int64 i ) { return i >= 0? i: -i; }
Expand All @@ -53,22 +49,6 @@ static inline long long php_date_llabs( long long i ) { return i >= 0 ? i : -i;
#define DATE_A64I(i, s) i = strtoll(s, NULL, 10)
#endif

PHPAPI time_t php_time(void)
{
#ifdef HAVE_GETTIMEOFDAY
struct timeval tm;

if (UNEXPECTED(gettimeofday(&tm, NULL) != SUCCESS)) {
/* fallback, can't reasonably happen */
return time(NULL);
}

return tm.tv_sec;
#else
return time(NULL);
#endif
}

/*
* RFC822, Section 5.1: http://www.ietf.org/rfc/rfc822.txt
* date-time = [ day "," ] date time ; dd mm yy hh:mm:ss zzz
Expand Down Expand Up @@ -867,7 +847,7 @@ static void php_date(INTERNAL_FUNCTION_PARAMETERS, bool localtime)
ZEND_PARSE_PARAMETERS_END();

if (ts_is_null) {
ts = php_time();
ts = zend_realtime_get();
}

RETURN_STR(php_format_date(ZSTR_VAL(format), ZSTR_LEN(format), ts, localtime));
Expand Down Expand Up @@ -1031,7 +1011,7 @@ PHP_FUNCTION(idate)
}

if (ts_is_null) {
ts = php_time();
ts = zend_realtime_get();
}

ret = php_idate(ZSTR_VAL(format)[0], ts, 0);
Expand Down Expand Up @@ -1111,7 +1091,7 @@ PHP_FUNCTION(strtotime)
now->tz_info = tzi;
now->zone_type = TIMELIB_ZONETYPE_ID;
timelib_unixtime2local(now,
!preset_ts_is_null ? (timelib_sll) preset_ts : (timelib_sll) php_time());
!preset_ts_is_null ? (timelib_sll) preset_ts : (timelib_sll) zend_realtime_get());

t = timelib_strtotime(ZSTR_VAL(times), ZSTR_LEN(times), &error,
DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
Expand Down Expand Up @@ -1162,15 +1142,15 @@ PHPAPI void php_mktime(INTERNAL_FUNCTION_PARAMETERS, bool gmt)
/* Initialize structure with current time */
now = timelib_time_ctor();
if (gmt) {
timelib_unixtime2gmt(now, (timelib_sll) php_time());
timelib_unixtime2gmt(now, (timelib_sll) zend_realtime_get());
} else {
tzi = get_timezone_info();
if (!tzi) {
return;
}
now->tz_info = tzi;
now->zone_type = TIMELIB_ZONETYPE_ID;
timelib_unixtime2local(now, (timelib_sll) php_time());
timelib_unixtime2local(now, (timelib_sll) zend_realtime_get());
}

now->h = hou;
Expand Down Expand Up @@ -1280,7 +1260,7 @@ PHPAPI void php_strftime(INTERNAL_FUNCTION_PARAMETERS, bool gmt)
}

if (timestamp_is_null) {
timestamp = (zend_long) php_time();
timestamp = (zend_long) zend_realtime_get();
}

ts = timelib_time_ctor();
Expand Down Expand Up @@ -1376,7 +1356,7 @@ PHP_FUNCTION(time)
{
ZEND_PARSE_PARAMETERS_NONE();

RETURN_LONG((zend_long)php_time());
RETURN_LONG((zend_long) zend_realtime_get());
}
/* }}} */

Expand All @@ -1396,7 +1376,7 @@ PHP_FUNCTION(localtime)
ZEND_PARSE_PARAMETERS_END();

if (timestamp_is_null) {
timestamp = (zend_long) php_time();
timestamp = (zend_long) zend_realtime_get();
}

tzi = get_timezone_info();
Expand Down Expand Up @@ -1451,7 +1431,7 @@ PHP_FUNCTION(getdate)
ZEND_PARSE_PARAMETERS_END();

if (timestamp_is_null) {
timestamp = (zend_long) php_time();
timestamp = (zend_long) zend_realtime_get();
}

tzi = get_timezone_info();
Expand Down Expand Up @@ -2353,16 +2333,11 @@ static void php_date_set_time_fraction(timelib_time *time, int microsecond)

static void php_date_get_current_time_with_fraction(time_t *sec, suseconds_t *usec)
{
#ifdef HAVE_GETTIMEOFDAY
struct timeval tp = {0}; /* For setting microsecond */
struct timespec ts;

gettimeofday(&tp, NULL);
*sec = tp.tv_sec;
*usec = tp.tv_usec;
#else
*sec = time(NULL);
*usec = 0;
#endif
zend_realtime_spec(&ts);
*sec = ts.tv_sec;
*usec = ts.tv_nsec / 1000;
}

PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, size_t time_str_len, const char *format, zval *timezone_object, int flags) /* {{{ */
Expand Down
3 changes: 2 additions & 1 deletion ext/date/php_date.h
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ ZEND_END_MODULE_GLOBALS(date)

#define DATEG(v) ZEND_MODULE_GLOBALS_ACCESSOR(date, v)

PHPAPI time_t php_time(void);
/* DEPRECATED, use zend_realtime_get() instead */
#define php_time() zend_realtime_get()

/* Backwards compatibility wrapper */
PHPAPI zend_long php_parse_date(const char *string, zend_long *now);
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /