Skip to content

Commit 0e2a74c

Browse files
committed
Convert zend_realtime_get() to be a macro wrapper around time(NULL)
1 parent 16a58c6 commit 0e2a74c

File tree

12 files changed

+33
-71
lines changed

12 files changed

+33
-71
lines changed

Zend/zend_time.c

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,6 @@
1616

1717
#include "zend_time.h"
1818

19-
ZEND_API time_t zend_realtime_get(time_t *sec, long *nsec) {
20-
if (!nsec) {
21-
return time(sec);
22-
}
23-
24-
#if defined(HAVE_CLOCK_GETTIME)
25-
26-
struct timespec ts;
27-
clock_gettime(CLOCK_REALTIME, &ts);
28-
if (sec) *sec = ts.tv_sec;
29-
*nsec = ts.tv_nsec;
30-
return ts.tv_sec;
31-
32-
#elif defined(HAVE_TIMESPEC_GET)
33-
34-
struct timespec ts;
35-
timespec_get(&ts, TIME_UTC);
36-
if (sec) *sec = ts.tv_sec;
37-
*nsec = ts.tv_nsec;
38-
return ts.tv_sec;
39-
40-
#elif defined(HAVE_GETTIMEOFDAY)
41-
42-
struct timeval tv;
43-
gettimeofday(&tv, NULL);
44-
45-
if (sec) *sec = tv.tv_sec;
46-
*nsec = tv.tv_usec * 1000;
47-
return tv.tv_sec;
48-
49-
#else
50-
51-
*nsec = 0;
52-
return time(sec);
53-
54-
#endif
55-
}
56-
5719
ZEND_API void zend_realtime_spec(struct timespec *ts) {
5820
#if defined(HAVE_CLOCK_GETTIME)
5921

Zend/zend_time.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262

6363
BEGIN_EXTERN_C()
6464

65-
/* Like time() but with up to nanosecond */
66-
ZEND_API time_t zend_realtime_get(time_t *sec, long *nsec);
65+
/* Helper macro to get current timestamp in seconds */
66+
#define zend_realtime_get() time(NULL)
6767

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

ext/date/php_date.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ static void php_date(INTERNAL_FUNCTION_PARAMETERS, bool localtime)
847847
ZEND_PARSE_PARAMETERS_END();
848848

849849
if (ts_is_null) {
850-
ts = zend_realtime_get(NULL, NULL);
850+
ts = zend_realtime_get();
851851
}
852852

853853
RETURN_STR(php_format_date(ZSTR_VAL(format), ZSTR_LEN(format), ts, localtime));
@@ -1011,7 +1011,7 @@ PHP_FUNCTION(idate)
10111011
}
10121012

10131013
if (ts_is_null) {
1014-
ts = zend_realtime_get(NULL, NULL);
1014+
ts = zend_realtime_get();
10151015
}
10161016

10171017
ret = php_idate(ZSTR_VAL(format)[0], ts, 0);
@@ -1091,7 +1091,7 @@ PHP_FUNCTION(strtotime)
10911091
now->tz_info = tzi;
10921092
now->zone_type = TIMELIB_ZONETYPE_ID;
10931093
timelib_unixtime2local(now,
1094-
!preset_ts_is_null ? (timelib_sll) preset_ts : (timelib_sll) zend_realtime_get(NULL, NULL));
1094+
!preset_ts_is_null ? (timelib_sll) preset_ts : (timelib_sll) zend_realtime_get());
10951095

10961096
t = timelib_strtotime(ZSTR_VAL(times), ZSTR_LEN(times), &error,
10971097
DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
@@ -1142,15 +1142,15 @@ PHPAPI void php_mktime(INTERNAL_FUNCTION_PARAMETERS, bool gmt)
11421142
/* Initialize structure with current time */
11431143
now = timelib_time_ctor();
11441144
if (gmt) {
1145-
timelib_unixtime2gmt(now, (timelib_sll) zend_realtime_get(NULL, NULL));
1145+
timelib_unixtime2gmt(now, (timelib_sll) zend_realtime_get());
11461146
} else {
11471147
tzi = get_timezone_info();
11481148
if (!tzi) {
11491149
return;
11501150
}
11511151
now->tz_info = tzi;
11521152
now->zone_type = TIMELIB_ZONETYPE_ID;
1153-
timelib_unixtime2local(now, (timelib_sll) zend_realtime_get(NULL, NULL));
1153+
timelib_unixtime2local(now, (timelib_sll) zend_realtime_get());
11541154
}
11551155

11561156
now->h = hou;
@@ -1260,7 +1260,7 @@ PHPAPI void php_strftime(INTERNAL_FUNCTION_PARAMETERS, bool gmt)
12601260
}
12611261

12621262
if (timestamp_is_null) {
1263-
timestamp = (zend_long) zend_realtime_get(NULL, NULL);
1263+
timestamp = (zend_long) zend_realtime_get();
12641264
}
12651265

12661266
ts = timelib_time_ctor();
@@ -1356,7 +1356,7 @@ PHP_FUNCTION(time)
13561356
{
13571357
ZEND_PARSE_PARAMETERS_NONE();
13581358

1359-
RETURN_LONG((zend_long) zend_realtime_get(NULL, NULL));
1359+
RETURN_LONG((zend_long) zend_realtime_get());
13601360
}
13611361
/* }}} */
13621362

@@ -1376,7 +1376,7 @@ PHP_FUNCTION(localtime)
13761376
ZEND_PARSE_PARAMETERS_END();
13771377

13781378
if (timestamp_is_null) {
1379-
timestamp = (zend_long) zend_realtime_get(NULL, NULL);
1379+
timestamp = (zend_long) zend_realtime_get();
13801380
}
13811381

13821382
tzi = get_timezone_info();
@@ -1431,7 +1431,7 @@ PHP_FUNCTION(getdate)
14311431
ZEND_PARSE_PARAMETERS_END();
14321432

14331433
if (timestamp_is_null) {
1434-
timestamp = (zend_long) zend_realtime_get(NULL, NULL);
1434+
timestamp = (zend_long) zend_realtime_get();
14351435
}
14361436

14371437
tzi = get_timezone_info();

ext/date/php_date.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ ZEND_END_MODULE_GLOBALS(date)
129129

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

132-
/* DEPRECATED, use zend_realtime_get(NULL, NULL) instead */
133-
#define php_time() zend_realtime_get(NULL, NULL)
132+
/* DEPRECATED, use zend_realtime_get() instead */
133+
#define php_time() zend_realtime_get()
134134

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

ext/openssl/xp_ssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ static void php_openssl_limit_handshake_reneg(const SSL *ssl) /* {{{ */
10791079

10801080
stream = php_openssl_get_stream_from_ssl_handle(ssl);
10811081
sslsock = (php_openssl_netstream_data_t*)stream->abstract;
1082-
now = (zend_long)zend_realtime_get(NULL, NULL);
1082+
now = (zend_long)zend_realtime_get();
10831083

10841084
/* The initial handshake is never rate-limited */
10851085
if (sslsock->reneg->prev_handshake == 0) {

ext/session/session.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,7 @@ CACHE_LIMITER_FUNC(public)
12621262
time_t expire_at;
12631263

12641264

1265-
expire_at = zend_realtime_get(NULL, NULL) + PS(cache_expire) * 60;
1265+
expire_at = zend_realtime_get() + PS(cache_expire) * 60;
12661266
memcpy(buf, EXPIRES, sizeof(EXPIRES) - 1);
12671267
strcpy_gmt(buf + sizeof(EXPIRES) - 1, &expire_at);
12681268
ADD_HEADER(buf);
@@ -1385,7 +1385,7 @@ static zend_result php_session_send_cookie(void)
13851385
if (PS(cookie_lifetime) > 0) {
13861386
time_t t;
13871387

1388-
t = zend_realtime_get(NULL, NULL) + PS(cookie_lifetime);
1388+
t = zend_realtime_get() + PS(cookie_lifetime);
13891389

13901390
if (t > 0) {
13911391
date_fmt = php_format_date(ZEND_STRL("D, d M Y H:i:s \\G\\M\\T"), t, false);

ext/standard/head.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ PHPAPI zend_result php_setcookie(zend_string *name, zend_string *value, time_t e
155155
smart_str_append(&buf, dt);
156156
zend_string_free(dt);
157157

158-
diff = difftime(expires, zend_realtime_get(NULL, NULL));
158+
diff = difftime(expires, zend_realtime_get());
159159
if (diff < 0) {
160160
diff = 0;
161161
}

ext/standard/uniqid.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
#include "ext/random/php_random.h"
3131
#include "ext/random/php_random_csprng.h"
3232

33-
ZEND_TLS time_t prev_sec = 0;
34-
ZEND_TLS long prev_usec = 0;
33+
ZEND_TLS time_t prev_tv_sec = 0;
34+
ZEND_TLS long prev_tv_usec = 0;
3535

3636
/* {{{ Generates a unique ID */
3737
PHP_FUNCTION(uniqid)
@@ -40,8 +40,8 @@ PHP_FUNCTION(uniqid)
4040
bool more_entropy = 0;
4141
zend_string *uniqid;
4242
size_t prefix_len = 0;
43-
time_t sec;
44-
long nsec, usec;
43+
struct timespec ts;
44+
long tv_usec;
4545
int isec, iusec;
4646

4747
ZEND_PARSE_PARAMETERS_START(0, 2)
@@ -56,15 +56,15 @@ PHP_FUNCTION(uniqid)
5656
* another process, causing a pause of around 10ms.
5757
*/
5858
do {
59-
zend_realtime_get(&sec, &nsec);
60-
usec = nsec / 1000;
61-
} while (sec == prev_sec && usec == prev_usec);
59+
zend_realtime_spec(&ts);
60+
tv_usec = ts.tv_nsec / 1000;
61+
} while (ts.tv_sec == prev_tv_sec && tv_usec == prev_tv_usec);
6262

63-
prev_sec = sec;
64-
prev_usec = usec;
63+
prev_tv_sec = ts.tv_sec;
64+
prev_tv_usec = tv_usec;
6565

66-
isec = (int) sec;
67-
iusec = (int) (usec % 0x100000);
66+
isec = (int) ts.tv_sec;
67+
iusec = (int) (tv_usec % 0x100000);
6868

6969
/* The max value usec can have is 0xF423F, so we use only five hex
7070
* digits for usecs.

sapi/cli/php_cli_server.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ static bool php_cli_server_get_system_time(char *buf) {
269269
time_t sec;
270270
struct tm tm;
271271

272-
zend_realtime_get(&sec, NULL);
272+
sec = zend_realtime_get();
273273
if (!php_localtime_r(&sec, &tm)) {
274274
return false;
275275
}
@@ -385,7 +385,7 @@ static void append_essential_headers(smart_str* buffer, php_cli_server_client *c
385385
zend_string *dt = php_format_date(
386386
"D, d M Y H:i:s",
387387
sizeof("D, d M Y H:i:s") - 1,
388-
zend_realtime_get(NULL, NULL),
388+
zend_realtime_get(),
389389
0
390390
);
391391
smart_str_appends_ex(buffer, "Date: ", persistent);

sapi/fpm/fpm/fpm_log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ int fpm_log_write(char *log_format) /* {{{ */
128128
test = true;
129129
}
130130

131-
now_epoch = zend_realtime_get(NULL, NULL);
131+
now_epoch = zend_realtime_get();
132132

133133
if (!test) {
134134
scoreboard = fpm_scoreboard_get();

0 commit comments

Comments
 (0)