Add functions for transferring dates to ints and vice versa

This commit is contained in:
Joshua Ramon Enslin 2024-05-03 17:42:32 +02:00
parent 8c1050f40a
commit 63ac1b296e
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
2 changed files with 116 additions and 0 deletions

View File

@ -928,4 +928,58 @@ final class MD_STD {
return $output;
}
/**
* Transfers a date into an integer. 0000-01-01 > 101; 2001-01-01 > 20010101.
* Needed to store negative dates (BC) in MySQL.
*
* @param string $date Date.
*
* @return integer
*/
public static function date_to_int(string $date):int {
// Edge case: -0000-01-01
if (substr($date, 0, 5) === '-0000') {
return -1 * intval(str_replace("-", "", ltrim($date, "0-")));
}
return intval(date("Ymd", strtotime($date)));
}
/**
* Transfers an integer into a date, reversing the effects of date_to_int.
* 0000-01-01 > 101; 2001-01-01 > 20010101.
* Needed to retrieve negative dates (BC) stored in MySQL.
*
* @param int $date_int Date represented as an integer.
*
* @return string
*/
public static function int_to_date(int $date_int):string {
$dateStr = (string)$date_int;
if (substr($dateStr, 0, 1) === '-') {
$isNegative = true;
$dateStr = substr($dateStr, 1);
}
else $isNegative = false;
if (strlen($dateStr) < 8) {
$dateStr = str_pad($dateStr, 8, "0", STR_PAD_LEFT);
}
$day = substr($dateStr, -2, 2);
$month = substr($dateStr, -4, 2);
$year = substr($dateStr, -8, 4);
return match($isNegative) {
true => '-',
false => '',
} . $year . '-' . $month . '-' . $day;
}
}

62
tests/MD_STD_Test.php Normal file
View File

@ -0,0 +1,62 @@
<?PHP
/**
* Tests for MD_STD.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
use PHPUnit\Framework\TestCase;
/**
* Tests for MD_STD.
*/
final class MD_STD_Test extends TestCase {
/**
* Returns sample dates and their equivalent integer values according
* to MD_STD::date_to_int().
*
* @return array<string, array{0: string, 1: integer}>
*/
public static function date_to_int_provider():array {
$values = [
["2022-01-01", 20220101],
["0022-01-01", 220101],
["-0022-01-01", -220101],
["-2022-01-01", -20220101],
["-0001-01-01", -10101],
["-0000-01-01", -101],
["0000-01-01", 101],
];
$output = [];
foreach ($values as $value) {
$output[$value[0]] = $value;
}
return $output;
}
/**
* Checks if dates can be translated to int and back.
*
* @dataProvider \MD_STD_Test::date_to_int_provider
*
* @param string $date Date to translate.
* @param integer $expectedInt Expected integer value for it.
*
* @return void
*/
public function test_date_to_int(string $date, int $expectedInt):void {
$toInt = MD_STD::date_to_int($date);
$toStr = MD_STD::int_to_date($toInt);
self::assertEquals($expectedInt, $toInt);
self::assertEquals($date, $toStr);
}
}