Skip to content

Commit dc55bf1

Browse files
committed
Addressed PR comments
1 parent 25cd3c9 commit dc55bf1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+202
-206
lines changed

Zend/zend_time.c

Lines changed: 0 additions & 40 deletions
This file was deleted.

Zend/zend_time.h

Lines changed: 72 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,45 +40,86 @@
4040
#define ZEND_MILLI_IN_SEC 1000U
4141
#define ZEND_MICRO_IN_SEC 1000000U
4242

43-
/* Helper macro to assign seconds to timeval */
44-
#define zend_time_sec2val(s, tv) \
45-
(tv).tv_sec = (tv_sec_t) (s); \
46-
(tv).tv_usec = 0;
47-
48-
/* Helper macro to assign microseconds to timeval */
49-
#define zend_time_usec2val(usec, tv) \
50-
(tv).tv_sec = (tv_sec_t) ((usec) / ZEND_MICRO_IN_SEC); \
51-
(tv).tv_usec = (tv_usec_t) ((usec) % ZEND_MICRO_IN_SEC);
52-
53-
/* Helper macro to assign double (seconds) to timeval */
54-
#define zend_time_dbl2val(d, tv) \
55-
(tv).tv_sec = (tv_sec_t) (d); \
56-
(tv).tv_usec = (tv_usec_t) (((d) - (tv).tv_sec) * ZEND_MICRO_IN_SEC);
57-
58-
/* Helper macro to copy timeval to timespec */
59-
#define zend_time_val2spec(tv, ts) \
60-
(ts).tv_sec = (time_t) (tv).tv_sec; \
61-
(ts).tv_nsec = (long) ((tv).tv_usec * 1000);
62-
6343
BEGIN_EXTERN_C()
6444

65-
/* Helper macro to get current timestamp in seconds */
66-
#define zend_realtime_get() time(NULL)
45+
/* Assign seconds to timeval */
46+
static zend_always_inline void zend_time_sec2val(time_t s, struct timeval *tv) {
47+
tv->tv_sec = (tv_sec_t) s;
48+
tv->tv_usec = 0;
49+
}
50+
51+
/* Assign microseconds to timeval */
52+
static zend_always_inline void zend_time_usec2val(int64_t usec, struct timeval *tv) {
53+
tv->tv_sec = (tv_sec_t) (usec / ZEND_MICRO_IN_SEC);
54+
tv->tv_usec = (tv_usec_t) (usec % ZEND_MICRO_IN_SEC);
55+
56+
if (UNEXPECTED(tv->tv_usec < 0)) {
57+
tv->tv_usec += ZEND_MICRO_IN_SEC;
58+
tv->tv_sec -= 1;
59+
}
60+
}
61+
62+
/* Assign double (seconds) to timeval */
63+
static zend_always_inline void zend_time_dbl2val(double s, struct timeval *tv) {
64+
tv->tv_sec = (tv_sec_t) s;
65+
tv->tv_usec = (tv_usec_t) ((s - tv->tv_sec) * ZEND_MICRO_IN_SEC);
66+
67+
if (UNEXPECTED(tv->tv_usec < 0)) {
68+
tv->tv_usec += ZEND_MICRO_IN_SEC;
69+
tv->tv_sec -= 1;
70+
} else if (UNEXPECTED(tv->tv_usec >= ZEND_MICRO_IN_SEC)) {
71+
// rare, but protects against rounding up to exactly 1 second
72+
tv->tv_usec -= ZEND_MICRO_IN_SEC;
73+
tv->tv_sec += 1;
74+
}
75+
}
76+
77+
/* Assign timeval to timespec */
78+
static zend_always_inline void zend_time_val2spec(struct timeval tv, struct timespec *ts) {
79+
ts->tv_sec = (time_t) tv.tv_sec;
80+
ts->tv_nsec = (long) (tv.tv_usec * 1000);
81+
}
82+
83+
/* Get current timestamp in seconds */
84+
static zend_always_inline time_t zend_time_real_get(void) {
85+
return time(NULL);
86+
}
6787

6888
/* wrapper around clock_gettime/timestamp_get/gettimeofday/time */
69-
ZEND_API void zend_realtime_spec(struct timespec *ts);
89+
static zend_always_inline void zend_time_real_spec(struct timespec *ts) {
90+
#if defined(HAVE_CLOCK_GETTIME)
91+
92+
(void) clock_gettime(CLOCK_REALTIME, ts);
93+
94+
#elif defined(HAVE_TIMESPEC_GET)
95+
96+
(void) timespec_get(ts, TIME_UTC);
97+
98+
#elif defined(HAVE_GETTIMEOFDAY)
99+
100+
struct timeval tv;
101+
(void) gettimeofday(&tv, NULL);
102+
zend_time_val2spec(tv, ts);
103+
104+
#else
105+
106+
ts->tv_sec = zend_time_real_get();
107+
ts->tv_nsec = 0;
108+
109+
#endif
110+
}
70111

71112
/* Monotonic time in nanoseconds with a fallback to real/wall-time
72113
if no monotonic timer is available */
73-
#if ZEND_HRTIME_AVAILABLE
74-
# define zend_monotime_fallback() (uint64_t)zend_hrtime()
75-
#else
76-
ZEND_API zend_always_inline uint64_t zend_monotime_fallback(void) {
77-
struct timespec ts;
78-
zend_realtime_spec(&ts);
79-
return ((uint64_t) ts.tv_sec * ZEND_NANO_IN_SEC) + ts.tv_nsec;
114+
static zend_always_inline uint64_t zend_time_mono_fallback(void) {
115+
#if ZEND_HRTIME_AVAILABLE
116+
return (uint64_t)zend_hrtime();
117+
#else
118+
struct timespec ts;
119+
zend_time_real_spec(&ts);
120+
return ((uint64_t) ts.tv_sec * ZEND_NANO_IN_SEC) + ts.tv_nsec;
121+
#endif
80122
}
81-
#endif
82123

83124
END_EXTERN_C()
84125

Zend/zend_virtual_cwd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ static size_t tsrm_realpath_r(char *path, size_t start, size_t len, int *ll, tim
577577
if (start && save && CWDG(realpath_cache_size_limit)) {
578578
/* cache lookup for absolute path */
579579
if (!*t) {
580-
*t = zend_realtime_get();
580+
*t = zend_time_real_get();
581581
}
582582
if ((bucket = realpath_cache_find(path, len, *t)) != NULL) {
583583
if (is_dir && !bucket->is_dir) {

configure.ac

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,6 @@ PHP_ADD_SOURCES([Zend], m4_normalize([
17611761
zend_generators.c
17621762
zend_hash.c
17631763
zend_highlight.c
1764-
zend_time.c
17651764
zend_hrtime.c
17661765
zend_inheritance.c
17671766
zend_ini_parser.c

ext/calendar/cal_unix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ PHP_FUNCTION(unixtojd)
3636
}
3737

3838
if (tl_is_null) {
39-
ts = zend_realtime_get();
39+
ts = zend_time_real_get();
4040
} else if (tl >= 0) {
4141
ts = (time_t) tl;
4242
} else {

ext/calendar/easter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static void _cal_easter(INTERNAL_FUNCTION_PARAMETERS, bool gm)
4343

4444
/* Default to the current year if year parameter is not given */
4545
if (year_is_null) {
46-
time_t a = zend_realtime_get();
46+
time_t a = zend_time_real_get();
4747
struct tm b, *res;
4848
res = php_localtime_r(&a, &b);
4949
if (!res) {

ext/date/php_date.c

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ static inline long long php_date_llabs( long long i ) { return i >= 0 ? i : -i;
4949
#define DATE_A64I(i, s) i = strtoll(s, NULL, 10)
5050
#endif
5151

52+
ZEND_ATTRIBUTE_DEPRECATED
53+
PHPAPI time_t php_time(void) {
54+
return zend_time_real_get();
55+
}
56+
5257
/*
5358
* RFC822, Section 5.1: http://www.ietf.org/rfc/rfc822.txt
5459
* date-time = [ day "," ] date time ; dd mm yy hh:mm:ss zzz
@@ -847,7 +852,7 @@ static void php_date(INTERNAL_FUNCTION_PARAMETERS, bool localtime)
847852
ZEND_PARSE_PARAMETERS_END();
848853

849854
if (ts_is_null) {
850-
ts = zend_realtime_get();
855+
ts = zend_time_real_get();
851856
}
852857

853858
RETURN_STR(php_format_date(ZSTR_VAL(format), ZSTR_LEN(format), ts, localtime));
@@ -1011,7 +1016,7 @@ PHP_FUNCTION(idate)
10111016
}
10121017

10131018
if (ts_is_null) {
1014-
ts = zend_realtime_get();
1019+
ts = zend_time_real_get();
10151020
}
10161021

10171022
ret = php_idate(ZSTR_VAL(format)[0], ts, 0);
@@ -1091,7 +1096,7 @@ PHP_FUNCTION(strtotime)
10911096
now->tz_info = tzi;
10921097
now->zone_type = TIMELIB_ZONETYPE_ID;
10931098
timelib_unixtime2local(now,
1094-
!preset_ts_is_null ? (timelib_sll) preset_ts : (timelib_sll) zend_realtime_get());
1099+
!preset_ts_is_null ? (timelib_sll) preset_ts : (timelib_sll) zend_time_real_get());
10951100

10961101
t = timelib_strtotime(ZSTR_VAL(times), ZSTR_LEN(times), &error,
10971102
DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
@@ -1142,15 +1147,15 @@ PHPAPI void php_mktime(INTERNAL_FUNCTION_PARAMETERS, bool gmt)
11421147
/* Initialize structure with current time */
11431148
now = timelib_time_ctor();
11441149
if (gmt) {
1145-
timelib_unixtime2gmt(now, (timelib_sll) zend_realtime_get());
1150+
timelib_unixtime2gmt(now, (timelib_sll) zend_time_real_get());
11461151
} else {
11471152
tzi = get_timezone_info();
11481153
if (!tzi) {
11491154
return;
11501155
}
11511156
now->tz_info = tzi;
11521157
now->zone_type = TIMELIB_ZONETYPE_ID;
1153-
timelib_unixtime2local(now, (timelib_sll) zend_realtime_get());
1158+
timelib_unixtime2local(now, (timelib_sll) zend_time_real_get());
11541159
}
11551160

11561161
now->h = hou;
@@ -1260,7 +1265,7 @@ PHPAPI void php_strftime(INTERNAL_FUNCTION_PARAMETERS, bool gmt)
12601265
}
12611266

12621267
if (timestamp_is_null) {
1263-
timestamp = (zend_long) zend_realtime_get();
1268+
timestamp = (zend_long) zend_time_real_get();
12641269
}
12651270

12661271
ts = timelib_time_ctor();
@@ -1356,7 +1361,7 @@ PHP_FUNCTION(time)
13561361
{
13571362
ZEND_PARSE_PARAMETERS_NONE();
13581363

1359-
RETURN_LONG((zend_long) zend_realtime_get());
1364+
RETURN_LONG((zend_long) zend_time_real_get());
13601365
}
13611366
/* }}} */
13621367

@@ -1376,7 +1381,7 @@ PHP_FUNCTION(localtime)
13761381
ZEND_PARSE_PARAMETERS_END();
13771382

13781383
if (timestamp_is_null) {
1379-
timestamp = (zend_long) zend_realtime_get();
1384+
timestamp = (zend_long) zend_time_real_get();
13801385
}
13811386

13821387
tzi = get_timezone_info();
@@ -1431,7 +1436,7 @@ PHP_FUNCTION(getdate)
14311436
ZEND_PARSE_PARAMETERS_END();
14321437

14331438
if (timestamp_is_null) {
1434-
timestamp = (zend_long) zend_realtime_get();
1439+
timestamp = (zend_long) zend_time_real_get();
14351440
}
14361441

14371442
tzi = get_timezone_info();
@@ -2333,15 +2338,6 @@ static void php_date_set_time_fraction(timelib_time *time, int microsecond)
23332338
time->us = microsecond;
23342339
}
23352340

2336-
static void php_date_get_current_time_with_fraction(time_t *sec, suseconds_t *usec)
2337-
{
2338-
struct timespec ts;
2339-
2340-
zend_realtime_spec(&ts);
2341-
*sec = ts.tv_sec;
2342-
*usec = ts.tv_nsec / 1000;
2343-
}
2344-
23452341
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) /* {{{ */
23462342
{
23472343
timelib_time *now;
@@ -2350,8 +2346,7 @@ PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, siz
23502346
int type = TIMELIB_ZONETYPE_ID, new_dst = 0;
23512347
char *new_abbr = NULL;
23522348
timelib_sll new_offset = 0;
2353-
time_t sec;
2354-
suseconds_t usec;
2349+
struct timespec ts;
23552350
int options = 0;
23562351

23572352
if (dateobj->time) {
@@ -2430,9 +2425,9 @@ PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, siz
24302425
now->tz_abbr = new_abbr;
24312426
break;
24322427
}
2433-
php_date_get_current_time_with_fraction(&sec, &usec);
2434-
timelib_unixtime2local(now, (timelib_sll) sec);
2435-
php_date_set_time_fraction(now, usec);
2428+
zend_time_real_spec(&ts);
2429+
timelib_unixtime2local(now, (timelib_sll) ts.tv_sec);
2430+
php_date_set_time_fraction(now, ts.tv_nsec / 1000);
24362431

24372432
if (!format
24382433
&& time_str_len == sizeof("now") - 1

ext/date/php_date.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "lib/timelib.h"
2121
#include "Zend/zend_hash.h"
22+
#include "Zend/zend_time.h"
2223

2324
/* Same as SIZEOF_ZEND_LONG but using TIMELIB_LONG_MAX/MIN */
2425
#if TIMELIB_LONG_MAX == INT32_MAX
@@ -129,8 +130,8 @@ ZEND_END_MODULE_GLOBALS(date)
129130

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

132-
/* DEPRECATED, use zend_realtime_get() instead */
133-
#define php_time() zend_realtime_get()
133+
ZEND_ATTRIBUTE_DEPRECATED
134+
PHPAPI time_t php_time(void);
134135

135136
/* Backwards compatibility wrapper */
136137
PHPAPI zend_long php_parse_date(const char *string, zend_long *now);

ext/ftp/ftp.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ time_t ftp_mdtm(ftpbuf_t *ftp, const char *path, const size_t path_len)
11081108
tm.tm_isdst = -1;
11091109

11101110
/* figure out the GMT offset */
1111-
stamp = zend_realtime_get();
1111+
stamp = zend_time_real_get();
11121112
gmt = php_gmtime_r(&stamp, &tmbuf);
11131113
if (!gmt) {
11141114
return -1;
@@ -1373,11 +1373,11 @@ static int my_poll(php_socket_t fd, int events, int timeout) {
13731373
uint64_t timeout_ns = (uint64_t) timeout * 1000000;
13741374

13751375
while (true) {
1376-
uint64_t start_ns = zend_monotime_fallback();
1376+
uint64_t start_ns = zend_time_mono_fallback();
13771377
n = php_pollfd_for_ms(fd, events, (int) (timeout_ns / 1000000));
13781378

13791379
if (n == -1 && php_socket_errno() == EINTR) {
1380-
uint64_t delta_ns = zend_monotime_fallback() - start_ns;
1380+
uint64_t delta_ns = zend_time_mono_fallback() - start_ns;
13811381
if (delta_ns > timeout_ns) {
13821382
#ifndef PHP_WIN32
13831383
errno = ETIMEDOUT;
@@ -1607,7 +1607,7 @@ static databuf_t* ftp_getdata(ftpbuf_t *ftp)
16071607
/* connect */
16081608
/* Win 95/98 seems not to like size > sizeof(sockaddr_in) */
16091609
size = php_sockaddr_size(&ftp->pasvaddr);
1610-
zend_time_sec2val(ftp->timeout_sec, tv);
1610+
zend_time_sec2val(ftp->timeout_sec, &tv);
16111611
if (php_connect_nonb(fd, (struct sockaddr*) &ftp->pasvaddr, size, &tv) == -1) {
16121612
php_error_docref(NULL, E_WARNING, "php_connect_nonb() failed: %s (%d)", strerror(errno), errno);
16131613
goto bail;

ext/mysqlnd/mysqlnd_debug.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ MYSQLND_METHOD(mysqlnd_debug, log)(MYSQLND_DEBUG * self,
7979
/* The following from FF's DBUG library, which is in the public domain */
8080
struct timespec ts;
8181
struct tm *tm_p;
82-
zend_realtime_spec(&ts);
82+
zend_time_real_spec(&ts);
8383
if ((tm_p = localtime((const time_t *)&ts.tv_sec))) {
8484
snprintf(time_buffer, sizeof(time_buffer) - 1,
8585
/* "%04d-%02d-%02d " */
@@ -164,7 +164,7 @@ MYSQLND_METHOD(mysqlnd_debug, log_va)(MYSQLND_DEBUG *self,
164164
/* The following from FF's DBUG library, which is in the public domain */
165165
struct timespec ts;
166166
struct tm *tm_p;
167-
zend_realtime_spec(&ts);
167+
zend_time_real_spec(&ts);
168168
if ((tm_p = localtime((const time_t *)&ts.tv_sec))) {
169169
snprintf(time_buffer, sizeof(time_buffer) - 1,
170170
/* "%04d-%02d-%02d " */

0 commit comments

Comments
 (0)