From 6d0e528c38fa8b70f195844aa0a13fe79e6ab0bf Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 29 Jul 2022 11:02:25 +0900 Subject: [PATCH] Combined\Datetime date/number to weekday conversion Convert functions for date or weekday number to weekday name or date to weekday number --- 4dev/tests/CoreLibsCombinedDateTimeTest.php | 161 +++++++++++++++++++- 4dev/tests/CoreLibsConvertStringsTest.php | 5 + www/admin/class_test.datetime.php | 16 ++ www/lib/CoreLibs/Combined/DateTime.php | 50 +++++- 4 files changed, 228 insertions(+), 4 deletions(-) diff --git a/4dev/tests/CoreLibsCombinedDateTimeTest.php b/4dev/tests/CoreLibsCombinedDateTimeTest.php index 67583a34..2dd24809 100644 --- a/4dev/tests/CoreLibsCombinedDateTimeTest.php +++ b/4dev/tests/CoreLibsCombinedDateTimeTest.php @@ -13,7 +13,6 @@ use PHPUnit\Framework\TestCase; */ final class CoreLibsCombinedDateTimeTest extends TestCase { - /** * timestamps * @@ -618,13 +617,169 @@ final class CoreLibsCombinedDateTimeTest extends TestCase * @param array $expected * @return void */ - public function testCalcDaysInterval(string $input_a, string $input_b, bool $flag, $expected): void - { + public function testCalcDaysInterval( + string $input_a, + string $input_b, + bool $flag, + $expected + ): void { $this->assertEquals( $expected, \CoreLibs\Combined\DateTime::calcDaysInterval($input_a, $input_b, $flag) ); } + + /** + * Undocumented function + * + * @return array + */ + public function weekdayNumberProvider(): array + { + return [ + '0 invalid' => [0, null, 'Inv',], + '0 invalid long' => [0, true, 'Invalid',], + '1 short' => [1, null, 'Mon',], + '1 long' => [1, true, 'Monday',], + '2 short' => [2, null, 'Tue',], + '2 long' => [2, true, 'Tuesday',], + '3 short' => [3, null, 'Wed',], + '3 long' => [3, true, 'Wednesday',], + '4 short' => [4, null, 'Thu',], + '4 long' => [4, true, 'Thursday',], + '5 short' => [5, null, 'Fri',], + '5 long' => [5, true, 'Friday',], + '6 short' => [6, null, 'Sat',], + '6 long' => [6, true, 'Saturday',], + '7 short' => [7, null, 'Sun',], + '7 long' => [7, true, 'Sunday',], + '8 invalid' => [8, null, 'Inv',], + '8 invalid long' => [8, true, 'Invalid',], + ]; + } + + /** + * int weekday number to string weekday + * + * @covers ::setWeekdayNameFromIsoDow + * @dataProvider weekdayNumberProvider + * @testdox weekdayListProvider $input (short $flag) will be $expected [$_dataName] + * + * @param int $input + * @param bool|null $flag + * @param string $expected + * @return void + */ + public function testSetWeekdayNameFromIsoDow( + int $input, + ?bool $flag, + string $expected + ): void { + if ($flag === null) { + $output = \CoreLibs\Combined\DateTime::setWeekdayNameFromIsoDow($input); + } else { + $output = \CoreLibs\Combined\DateTime::setWeekdayNameFromIsoDow($input, $flag); + } + $this->assertEquals( + $expected, + $output + ); + } + + /** + * Undocumented function + * + * @return array + */ + public function weekdayDateProvider(): array + { + return [ + 'invalid date' => ['2022-02-31', -1], + '1: monday' => ['2022-07-25', 1], + '2: tuesday' => ['2022-07-26', 2], + '3: wednesday' => ['2022-07-27', 3], + '4: thursday' => ['2022-07-28', 4], + '5: friday' => ['2022-07-29', 5], + '6: saturday' => ['2022-07-30', 6], + '7: sunday' => ['2022-07-31', 7], + ]; + } + + /** + * date to weekday number + * + * @covers ::setWeekdayNumberFromDate + * @dataProvider weekdayDateProvider + * @testdox setWeekdayNumberFromDate $input will be $expected [$_dataName] + * + * @param string $input + * @param int $expected + * @return void + */ + public function testSetWeekdayNumberFromDate( + string $input, + int $expected + ): void { + $this->assertEquals( + $expected, + \CoreLibs\Combined\DateTime::setWeekdayNumberFromDate($input) + ); + } + + /** + * Undocumented function + * + * @return array + */ + public function weekdayDateNameProvider(): array + { + return [ + 'invalid date short' => ['2022-02-31', null, 'Inv'], + 'invalid date long' => ['2022-02-31', true, 'Invalid'], + 'Mon short' => ['2022-07-25', null, 'Mon'], + 'Monday long' => ['2022-07-25', true, 'Monday'], + 'Tue short' => ['2022-07-26', null, 'Tue'], + 'Tuesday long' => ['2022-07-26', true, 'Tuesday'], + 'Wed short' => ['2022-07-27', null, 'Wed'], + 'Wednesday long' => ['2022-07-27', true, 'Wednesday'], + 'Thu short' => ['2022-07-28', null, 'Thu'], + 'Thursday long' => ['2022-07-28', true, 'Thursday'], + 'Fri short' => ['2022-07-29', null, 'Fri'], + 'Friday long' => ['2022-07-29', true, 'Friday'], + 'Sat short' => ['2022-07-30', null, 'Sat'], + 'Saturday long' => ['2022-07-30', true, 'Saturday'], + 'Sun short' => ['2022-07-31', null, 'Sun'], + 'Sunday long' => ['2022-07-31', true, 'Sunday'], + ]; + } + + /** + * date to weekday name + * + * @covers ::setWeekdayNameFromDate + * @dataProvider weekdayDateNameProvider + * @testdox setWeekdayNameFromDate $input (short $flag) will be $expected [$_dataName] + * + * @param string $input + * @param bool|null $flag + * @param string $expected + * @return void + */ + public function testSetWeekdayNameFromDate( + string $input, + ?bool $flag, + string $expected + ): void { + if ($flag === null) { + $output = \CoreLibs\Combined\DateTime::setWeekdayNameFromDate($input); + } else { + $output = \CoreLibs\Combined\DateTime::setWeekdayNameFromDate($input, $flag); + } + $this->assertEquals( + $expected, + $output + ); + } } // __END__ diff --git a/4dev/tests/CoreLibsConvertStringsTest.php b/4dev/tests/CoreLibsConvertStringsTest.php index 9f054928..7270ec2a 100644 --- a/4dev/tests/CoreLibsConvertStringsTest.php +++ b/4dev/tests/CoreLibsConvertStringsTest.php @@ -13,6 +13,11 @@ use PHPUnit\Framework\TestCase; */ final class CoreLibsConvertStringsTest extends TestCase { + /** + * Undocumented function + * + * @return array + */ public function splitFormatStringProvider(): array { // 0: input diff --git a/www/admin/class_test.datetime.php b/www/admin/class_test.datetime.php index cf07f2c7..7a09af51 100644 --- a/www/admin/class_test.datetime.php +++ b/www/admin/class_test.datetime.php @@ -139,6 +139,22 @@ foreach ($compare_dates as $compare_date) { . DgS::printAr(DateTime::calcDaysInterval($compare_date[0], $compare_date[1], true)) . "
"; } +// test date conversion +$dow = 2; +print "DOW[$dow]: " . DateTime::setWeekdayNameFromIsoDow($dow) . "
"; +print "DOW[$dow],long: " . DateTime::setWeekdayNameFromIsoDow($dow, true) . "
"; +$date = '2022-7-22'; +print "DATE-dow[$date]: " . DateTime::setWeekdayNameFromDate($date) . "
"; +print "DATE-dow[$date],long: " . DateTime::setWeekdayNameFromDate($date, true) . "
"; +print "DOW-date[$date]: " . DateTime::setWeekdayNumberFromDate($date) . "
"; +$dow = 11; +print "DOW[$dow];invalid: " . DateTime::setWeekdayNameFromIsoDow($dow) . "
"; +print "DOW[$dow],long;invalid: " . DateTime::setWeekdayNameFromIsoDow($dow, true) . "
"; +$date = '2022-70-242'; +print "DATE-dow[$date];invalid: " . DateTime::setWeekdayNameFromDate($date) . "
"; +print "DATE-dow[$date],long;invalid: " . DateTime::setWeekdayNameFromDate($date, true) . "
"; +print "DOW-date[$date];invalid: " . DateTime::setWeekdayNumberFromDate($date) . "
"; + // error message print $log->printErrorMsg(); diff --git a/www/lib/CoreLibs/Combined/DateTime.php b/www/lib/CoreLibs/Combined/DateTime.php index 5e7aa04b..728f5e0e 100644 --- a/www/lib/CoreLibs/Combined/DateTime.php +++ b/www/lib/CoreLibs/Combined/DateTime.php @@ -1,7 +1,7 @@ 7) { + return $long ? 'Invalid' : 'Inv'; + } + return date($long ? 'l' : 'D', strtotime("Sunday +{$isodow} days")); + } + + /** + * Get the day of week Name from date + * + * @param string $date Any valid date + * @param bool $long Default false 'Mon', if true 'Monday' + * @return string Day of week string either short 'Mon' or long 'Monday' + */ + public static function setWeekdayNameFromDate(string $date, bool $long = false): string + { + if (!self::checkDate($date)) { + return $long ? 'Invalid' : 'Inv'; + } + return date($long ? 'l' : 'D', strtotime($date)); + } + + /** + * Get the day of week Name from date + * + * @param string $date Any valid date + * @return int ISO Weekday number 1: Monday, 7: Sunday, -1 for invalid date + */ + public static function setWeekdayNumberFromDate(string $date): int + { + if (!self::checkDate($date)) { + return -1; + } + return (int)date('N', strtotime($date)); + } + /** * splits & checks date, wrap around for check_date function *