From b302fb405347e2ef2a3195064d6ebfc813e5078c Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Tue, 1 Apr 2025 11:15:00 +0900 Subject: [PATCH] Add CombinedDateTime class calcDaysInteral wrapper functions calcDaysIntervalNamedIndex for force using named index and returning only named index calcDaysIntervalNumIndex for force using numeric index and returning only numeric index --- .../Combined/CoreLibsCombinedDateTimeTest.php | 26 +++++++- www/lib/CoreLibs/Combined/DateTime.php | 60 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/4dev/tests/Combined/CoreLibsCombinedDateTimeTest.php b/4dev/tests/Combined/CoreLibsCombinedDateTimeTest.php index 58040c49..1833f708 100644 --- a/4dev/tests/Combined/CoreLibsCombinedDateTimeTest.php +++ b/4dev/tests/Combined/CoreLibsCombinedDateTimeTest.php @@ -1068,8 +1068,32 @@ final class CoreLibsCombinedDateTimeTest extends TestCase return_named:$return_named, include_end_date:$include_end_date, exclude_start_date:$exclude_start_date - ) + ), + 'call calcDaysInterval' ); + if ($return_named) { + $this->assertEquals( + $expected, + \CoreLibs\Combined\DateTime::calcDaysIntervalNamedIndex( + $input_a, + $input_b, + include_end_date:$include_end_date, + exclude_start_date:$exclude_start_date + ), + 'call calcDaysIntervalNamedIndex' + ); + } else { + $this->assertEquals( + $expected, + \CoreLibs\Combined\DateTime::calcDaysIntervalNumIndex( + $input_a, + $input_b, + include_end_date:$include_end_date, + exclude_start_date:$exclude_start_date + ), + 'call calcDaysIntervalNamedIndex' + ); + } } /** diff --git a/www/lib/CoreLibs/Combined/DateTime.php b/www/lib/CoreLibs/Combined/DateTime.php index d123c53a..89d1cd87 100644 --- a/www/lib/CoreLibs/Combined/DateTime.php +++ b/www/lib/CoreLibs/Combined/DateTime.php @@ -714,6 +714,66 @@ class DateTime } } + /** + * wrapper for calcDaysInterval with numeric return only + * + * @param string $start_date valid start date (y/m/d) + * @param string $end_date valid end date (y/m/d) + * @param bool $include_end_date [default=true] include end date in calc + * @param bool $exclude_start_date [default=false] include end date in calc + * @return array{0:int,1:int,2:int,3:bool} + */ + public static function calcDaysIntervalNumIndex( + string $start_date, + string $end_date, + bool $include_end_date = true, + bool $exclude_start_date = false + ): array { + $values = self::calcDaysInterval( + $start_date, + $end_date, + false, + $include_end_date, + $exclude_start_date + ); + return [ + $values[0], + $values[1], + $values[2], + $values[3], + ]; + } + + /** + * wrapper for calcDaysInterval with named return only + * + * @param string $start_date valid start date (y/m/d) + * @param string $end_date valid end date (y/m/d) + * @param bool $include_end_date [default=true] include end date in calc + * @param bool $exclude_start_date [default=false] include end date in calc + * @return array{overall:int,weekday:int,weekend:int,reverse:bool} + */ + public static function calcDaysIntervalNamedIndex( + string $start_date, + string $end_date, + bool $include_end_date = true, + bool $exclude_start_date = false + ): array { + $values = self::calcDaysInterval( + $start_date, + $end_date, + true, + $include_end_date, + $exclude_start_date + ); + return [ + 'overall' => $values['overall'], + 'weekday' => $values['weekday'], + 'weekend' => $values['weekend'], + 'reverse' => $values['reverse'], + ]; + } + /** * check if a weekend day (sat/sun) is in the given date range * Can have time too, but is not needed