Handle dates outside of strtotime()'s range in date_to_int, int_to_date

Close #9
This commit is contained in:
Joshua Ramon Enslin 2024-05-05 00:48:04 +02:00
parent fb1372d193
commit d4918dd893
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
2 changed files with 6 additions and 7 deletions

View File

@ -939,12 +939,11 @@ final class MD_STD {
*/ */
public static function date_to_int(string $date):int { public static function date_to_int(string $date):int {
// Edge case: -0000-01-01 $isNegative = substr($date, 0, 1) === '-';
if (substr($date, 0, 5) === '-0000') { $parts = explode('-', $date);
return -1 * intval(str_replace("-", "", ltrim($date, "0-")));
}
return intval(date("Ymd", self::strtotime($date))); if ($isNegative === true) return -1 * intval(implode($parts));
else return intval(implode($parts));
} }
@ -973,7 +972,7 @@ final class MD_STD {
$day = substr($dateStr, -2, 2); $day = substr($dateStr, -2, 2);
$month = substr($dateStr, -4, 2); $month = substr($dateStr, -4, 2);
$year = substr($dateStr, -8, 4); $year = substr($dateStr, 0, -4);
return match($isNegative) { return match($isNegative) {
true => '-', true => '-',

View File

@ -27,8 +27,8 @@ final class MD_STD_Test extends TestCase {
["-0022-01-01", -220101], ["-0022-01-01", -220101],
["-2022-01-01", -20220101], ["-2022-01-01", -20220101],
["-0001-01-01", -10101], ["-0001-01-01", -10101],
["-0000-01-01", -101],
["0000-01-01", 101], ["0000-01-01", 101],
["100000-01-01", 1000000101],
]; ];
$output = []; $output = [];