Skip to content

Commit ffcc813

Browse files
committed
Convert UNKNOWN default values to null in ext/date
Closes GH-5509
1 parent 089d8cb commit ffcc813

File tree

6 files changed

+114
-105
lines changed

6 files changed

+114
-105
lines changed

Zend/zend_API.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,9 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_value_error(uint32_t arg_num
13541354
#define Z_PARAM_DOUBLE(dest) \
13551355
Z_PARAM_DOUBLE_EX(dest, _dummy, 0, 0)
13561356

1357+
#define Z_PARAM_DOUBLE_OR_NULL(dest, is_null) \
1358+
Z_PARAM_DOUBLE_EX(dest, is_null, 1, 0)
1359+
13571360
/* old "f" */
13581361
#define Z_PARAM_FUNC_EX2(dest_fci, dest_fcc, check_null, deref, separate) \
13591362
Z_PARAM_PROLOGUE(deref, separate); \

ext/date/php_date.c

Lines changed: 81 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -782,14 +782,15 @@ static void php_date(INTERNAL_FUNCTION_PARAMETERS, int localtime)
782782
{
783783
zend_string *format;
784784
zend_long ts;
785+
zend_bool ts_is_null = 1;
785786

786787
ZEND_PARSE_PARAMETERS_START(1, 2)
787788
Z_PARAM_STR(format)
788789
Z_PARAM_OPTIONAL
789-
Z_PARAM_LONG(ts)
790+
Z_PARAM_LONG_OR_NULL(ts, ts_is_null)
790791
ZEND_PARSE_PARAMETERS_END();
791792

792-
if (ZEND_NUM_ARGS() == 1) {
793+
if (ts_is_null) {
793794
ts = php_time();
794795
}
795796

@@ -940,21 +941,22 @@ PHP_FUNCTION(gmdate)
940941
PHP_FUNCTION(idate)
941942
{
942943
zend_string *format;
943-
zend_long ts = 0;
944+
zend_long ts;
945+
zend_bool ts_is_null = 1;
944946
int ret;
945947

946948
ZEND_PARSE_PARAMETERS_START(1, 2)
947949
Z_PARAM_STR(format)
948950
Z_PARAM_OPTIONAL
949-
Z_PARAM_LONG(ts)
951+
Z_PARAM_LONG_OR_NULL(ts, ts_is_null)
950952
ZEND_PARSE_PARAMETERS_END();
951953

952954
if (ZSTR_LEN(format) != 1) {
953955
php_error_docref(NULL, E_WARNING, "idate format is one char");
954956
RETURN_FALSE;
955957
}
956958

957-
if (ZEND_NUM_ARGS() == 1) {
959+
if (ts_is_null) {
958960
ts = php_time();
959961
}
960962

@@ -1011,14 +1013,15 @@ PHP_FUNCTION(strtotime)
10111013
zend_string *times;
10121014
int error1, error2;
10131015
timelib_error_container *error;
1014-
zend_long preset_ts = 0, ts;
1016+
zend_long preset_ts, ts;
1017+
zend_bool preset_ts_is_null = 1;
10151018
timelib_time *t, *now;
10161019
timelib_tzinfo *tzi;
10171020

10181021
ZEND_PARSE_PARAMETERS_START(1, 2)
10191022
Z_PARAM_STR(times)
10201023
Z_PARAM_OPTIONAL
1021-
Z_PARAM_LONG(preset_ts)
1024+
Z_PARAM_LONG_OR_NULL(preset_ts, preset_ts_is_null)
10221025
ZEND_PARSE_PARAMETERS_END();
10231026

10241027
tzi = get_timezone_info();
@@ -1027,7 +1030,7 @@ PHP_FUNCTION(strtotime)
10271030
now->tz_info = tzi;
10281031
now->zone_type = TIMELIB_ZONETYPE_ID;
10291032
timelib_unixtime2local(now,
1030-
(ZEND_NUM_ARGS() == 2) ? (timelib_sll) preset_ts : (timelib_sll) php_time());
1033+
!preset_ts_is_null ? (timelib_sll) preset_ts : (timelib_sll) php_time());
10311034

10321035
t = timelib_strtotime(ZSTR_VAL(times), ZSTR_LEN(times), &error,
10331036
DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
@@ -1051,7 +1054,8 @@ PHP_FUNCTION(strtotime)
10511054
/* {{{ php_mktime - (gm)mktime helper */
10521055
PHPAPI void php_mktime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
10531056
{
1054-
zend_long hou = 0, min = 0, sec = 0, mon = 0, day = 0, yea = 0;
1057+
zend_long hou, min, sec, mon, day, yea;
1058+
zend_bool min_is_null = 1, sec_is_null = 1, mon_is_null = 1, day_is_null = 1, yea_is_null = 1;
10551059
timelib_time *now;
10561060
timelib_tzinfo *tzi = NULL;
10571061
zend_long ts, adjust_seconds = 0;
@@ -1060,11 +1064,11 @@ PHPAPI void php_mktime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
10601064
ZEND_PARSE_PARAMETERS_START(1, 6)
10611065
Z_PARAM_LONG(hou)
10621066
Z_PARAM_OPTIONAL
1063-
Z_PARAM_LONG(min)
1064-
Z_PARAM_LONG(sec)
1065-
Z_PARAM_LONG(mon)
1066-
Z_PARAM_LONG(day)
1067-
Z_PARAM_LONG(yea)
1067+
Z_PARAM_LONG_OR_NULL(min, min_is_null)
1068+
Z_PARAM_LONG_OR_NULL(sec, sec_is_null)
1069+
Z_PARAM_LONG_OR_NULL(mon, mon_is_null)
1070+
Z_PARAM_LONG_OR_NULL(day, day_is_null)
1071+
Z_PARAM_LONG_OR_NULL(yea, yea_is_null)
10681072
ZEND_PARSE_PARAMETERS_END();
10691073

10701074
/* Initialize structure with current time */
@@ -1077,33 +1081,34 @@ PHPAPI void php_mktime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
10771081
now->zone_type = TIMELIB_ZONETYPE_ID;
10781082
timelib_unixtime2local(now, (timelib_sll) php_time());
10791083
}
1080-
/* Fill in the new data */
1081-
switch (ZEND_NUM_ARGS()) {
1082-
case 6:
1083-
if (yea >= 0 && yea < 70) {
1084-
yea += 2000;
1085-
} else if (yea >= 70 && yea <= 100) {
1086-
yea += 1900;
1087-
}
1088-
now->y = yea;
1089-
/* break intentionally missing again */
1090-
case 5:
1091-
now->d = day;
1092-
/* break missing intentionally here too */
1093-
case 4:
1094-
now->m = mon;
1095-
/* and here */
1096-
case 3:
1097-
now->s = sec;
1098-
/* yup, this break isn't here on purpose too */
1099-
case 2:
1100-
now->i = min;
1101-
/* last intentionally missing break */
1102-
case 1:
1103-
now->h = hou;
1104-
break;
1105-
EMPTY_SWITCH_DEFAULT_CASE()
1084+
1085+
now->h = hou;
1086+
1087+
if (!min_is_null) {
1088+
now->i = min;
1089+
}
1090+
1091+
if (!sec_is_null) {
1092+
now->s = sec;
1093+
}
1094+
1095+
if (!mon_is_null) {
1096+
now->m = mon;
11061097
}
1098+
1099+
if (!day_is_null) {
1100+
now->d = day;
1101+
}
1102+
1103+
if (!yea_is_null) {
1104+
if (yea >= 0 && yea < 70) {
1105+
yea += 2000;
1106+
} else if (yea >= 70 && yea <= 100) {
1107+
yea += 1900;
1108+
}
1109+
now->y = yea;
1110+
}
1111+
11071112
/* Update the timestamp */
11081113
if (gmt) {
11091114
timelib_update_ts(now, NULL);
@@ -1163,7 +1168,8 @@ PHP_FUNCTION(checkdate)
11631168
PHPAPI void php_strftime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
11641169
{
11651170
zend_string *format;
1166-
zend_long timestamp = 0;
1171+
zend_long timestamp;
1172+
zend_bool timestamp_is_null = 1;
11671173
struct tm ta;
11681174
int max_reallocs = 5;
11691175
size_t buf_len = 256, real_len;
@@ -1172,18 +1178,20 @@ PHPAPI void php_strftime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
11721178
timelib_time_offset *offset = NULL;
11731179
zend_string *buf;
11741180

1175-
timestamp = (zend_long) php_time();
1176-
11771181
ZEND_PARSE_PARAMETERS_START(1, 2)
11781182
Z_PARAM_STR(format)
11791183
Z_PARAM_OPTIONAL
1180-
Z_PARAM_LONG(timestamp)
1184+
Z_PARAM_LONG_OR_NULL(timestamp, timestamp_is_null)
11811185
ZEND_PARSE_PARAMETERS_END();
11821186

11831187
if (ZSTR_LEN(format) == 0) {
11841188
RETURN_FALSE;
11851189
}
11861190

1191+
if (timestamp_is_null) {
1192+
timestamp = (zend_long) php_time();
1193+
}
1194+
11871195
ts = timelib_time_ctor();
11881196
if (gmt) {
11891197
tzi = NULL;
@@ -1286,17 +1294,18 @@ PHP_FUNCTION(time)
12861294
PHP_FUNCTION(localtime)
12871295
{
12881296
zend_long timestamp;
1297+
zend_bool timestamp_is_null = 1;
12891298
zend_bool associative = 0;
12901299
timelib_tzinfo *tzi;
12911300
timelib_time *ts;
12921301

12931302
ZEND_PARSE_PARAMETERS_START(0, 2)
12941303
Z_PARAM_OPTIONAL
1295-
Z_PARAM_LONG(timestamp)
1304+
Z_PARAM_LONG_OR_NULL(timestamp, timestamp_is_null)
12961305
Z_PARAM_BOOL(associative)
12971306
ZEND_PARSE_PARAMETERS_END();
12981307

1299-
if (ZEND_NUM_ARGS() == 0) {
1308+
if (timestamp_is_null) {
13001309
timestamp = (zend_long) php_time();
13011310
}
13021311

@@ -1339,15 +1348,16 @@ PHP_FUNCTION(localtime)
13391348
PHP_FUNCTION(getdate)
13401349
{
13411350
zend_long timestamp;
1351+
zend_bool timestamp_is_null = 1;
13421352
timelib_tzinfo *tzi;
13431353
timelib_time *ts;
13441354

13451355
ZEND_PARSE_PARAMETERS_START(0, 1)
13461356
Z_PARAM_OPTIONAL
1347-
Z_PARAM_LONG(timestamp)
1357+
Z_PARAM_LONG_OR_NULL(timestamp, timestamp_is_null)
13481358
ZEND_PARSE_PARAMETERS_END();
13491359

1350-
if (ZEND_NUM_ARGS() == 0) {
1360+
if (timestamp_is_null) {
13511361
timestamp = (zend_long) php_time();
13521362
}
13531363

@@ -4511,10 +4521,11 @@ PHP_FUNCTION(date_default_timezone_get)
45114521
*/
45124522
static void php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAMETERS, int calc_sunset)
45134523
{
4514-
double latitude = 0.0, longitude = 0.0, zenith = 0.0, gmt_offset = 0, altitude;
4524+
double latitude, longitude, zenith, gmt_offset, altitude;
4525+
zend_bool latitude_is_null = 1, longitude_is_null = 1, zenith_is_null = 1, gmt_offset_is_null = 1;
45154526
double h_rise, h_set, N;
45164527
timelib_sll rise, set, transit;
4517-
zend_long time, retformat = 0;
4528+
zend_long time, retformat = SUNFUNCS_RET_STRING;
45184529
int rs;
45194530
timelib_time *t;
45204531
timelib_tzinfo *tzi;
@@ -4524,33 +4535,28 @@ static void php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAMETERS, int calc_su
45244535
Z_PARAM_LONG(time)
45254536
Z_PARAM_OPTIONAL
45264537
Z_PARAM_LONG(retformat)
4527-
Z_PARAM_DOUBLE(latitude)
4528-
Z_PARAM_DOUBLE(longitude)
4529-
Z_PARAM_DOUBLE(zenith)
4530-
Z_PARAM_DOUBLE(gmt_offset)
4538+
Z_PARAM_DOUBLE_OR_NULL(latitude, latitude_is_null)
4539+
Z_PARAM_DOUBLE_OR_NULL(longitude, longitude_is_null)
4540+
Z_PARAM_DOUBLE_OR_NULL(zenith, zenith_is_null)
4541+
Z_PARAM_DOUBLE_OR_NULL(gmt_offset, gmt_offset_is_null)
45314542
ZEND_PARSE_PARAMETERS_END();
45324543

4533-
switch (ZEND_NUM_ARGS()) {
4534-
case 1:
4535-
retformat = SUNFUNCS_RET_STRING;
4536-
case 2:
4537-
latitude = INI_FLT("date.default_latitude");
4538-
case 3:
4539-
longitude = INI_FLT("date.default_longitude");
4540-
case 4:
4541-
if (calc_sunset) {
4542-
zenith = INI_FLT("date.sunset_zenith");
4543-
} else {
4544-
zenith = INI_FLT("date.sunrise_zenith");
4545-
}
4546-
case 5:
4547-
case 6:
4548-
break;
4549-
default:
4550-
php_error_docref(NULL, E_WARNING, "Invalid format");
4551-
RETURN_FALSE;
4552-
break;
4544+
if (latitude_is_null) {
4545+
latitude = INI_FLT("date.default_latitude");
4546+
}
4547+
4548+
if (longitude_is_null) {
4549+
latitude = INI_FLT("date.default_longitude");
4550+
}
4551+
4552+
if (zenith_is_null) {
4553+
if (calc_sunset) {
4554+
zenith = INI_FLT("date.sunset_zenith");
4555+
} else {
4556+
zenith = INI_FLT("date.sunrise_zenith");
4557+
}
45534558
}
4559+
45544560
if (retformat != SUNFUNCS_RET_TIMESTAMP &&
45554561
retformat != SUNFUNCS_RET_STRING &&
45564562
retformat != SUNFUNCS_RET_DOUBLE)
@@ -4566,7 +4572,7 @@ static void php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAMETERS, int calc_su
45664572
t->tz_info = tzi;
45674573
t->zone_type = TIMELIB_ZONETYPE_ID;
45684574

4569-
if (ZEND_NUM_ARGS() <= 5) {
4575+
if (gmt_offset_is_null) {
45704576
gmt_offset = timelib_get_current_offset(t) / 3600;
45714577
}
45724578

ext/date/php_date.stub.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@
22

33
/** @generate-function-entries */
44

5-
function strtotime(string $time, int $now = UNKNOWN): int|false {}
5+
function strtotime(string $time, ?int $now = null): int|false {}
66

7-
function date(string $format, int $timestamp = UNKNOWN): string {}
7+
function date(string $format, ?int $timestamp = null): string {}
88

9-
function idate(string $format, int $timestamp = UNKNOWN): int|false {}
9+
function idate(string $format, ?int $timestamp = null): int|false {}
1010

11-
function gmdate(string $format, int $timestamp = UNKNOWN): string {}
11+
function gmdate(string $format, ?int $timestamp = null): string {}
1212

1313
function mktime(
14-
int $hour, int $min = UNKNOWN, int $sec = UNKNOWN,
15-
int $mon = UNKNOWN, int $day = UNKNOWN, int $year = UNKNOWN): int|false {}
14+
int $hour, ?int $min = null, ?int $sec = null,
15+
?int $mon = null, ?int $day = null, ?int $year = null): int|false {}
1616

1717
function gmmktime(
18-
int $hour, int $min = UNKNOWN, int $sec = UNKNOWN,
19-
int $mon = UNKNOWN, int $day = UNKNOWN, int $year = UNKNOWN): int|false {}
18+
int $hour, ?int $min = null, ?int $sec = null,
19+
?int $mon = null, ?int $day = null, ?int $year = null): int|false {}
2020

2121
function checkdate(int $m, int $d, int $y): bool {}
2222

23-
function strftime(string $format, int $timestamp = UNKNOWN): string|false {}
23+
function strftime(string $format, ?int $timestamp = null): string|false {}
2424

25-
function gmstrftime(string $format, int $timestamp = UNKNOWN): string|false {}
25+
function gmstrftime(string $format, ?int $timestamp = null): string|false {}
2626

2727
function time(): int {}
2828

29-
function localtime(int $timestamp = UNKNOWN, bool $associative = false): array {}
29+
function localtime(?int $timestamp = null, bool $associative = false): array {}
3030

31-
function getdate(int $timestamp = UNKNOWN): array {}
31+
function getdate(?int $timestamp = null): array {}
3232

3333
function date_create(string $time = "now", ?DateTimeZone $timezone = null): DateTime|false {}
3434

@@ -104,12 +104,12 @@ function date_default_timezone_get(): string {}
104104

105105
function date_sunrise(
106106
int $time, int $retformat = SUNFUNCS_RET_STRING,
107-
float $latitude = UNKNOWN, float $longitude = UNKNOWN, float $zenith = UNKNOWN,
107+
?float $latitude = null, ?float $longitude = null, ?float $zenith = null,
108108
float $gmt_offset = 0): string|int|float|false {}
109109

110110
function date_sunset(
111111
int $time, int $retformat = SUNFUNCS_RET_STRING,
112-
float $latitude = UNKNOWN, float $longitude = UNKNOWN, float $zenith = UNKNOWN,
112+
?float $latitude = null, ?float $longitude = null, ?float $zenith = null,
113113
float $gmt_offset = 0): string|int|float|false {}
114114

115115
function date_sun_info(int $time, float $latitude, float $longitude): array {}

0 commit comments

Comments
 (0)