mirror of
https://bitbucket.org/jsuto/piler.git
synced 2024-11-07 23:31:58 +01:00
Introduced convert_date_string_to_ymd_by_template() function
Signed-off-by: Janos SUTO <sj@acts.hu>
This commit is contained in:
parent
7b1028d928
commit
a8f037c3e9
@ -3,6 +3,7 @@
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
include_once("webui/system/model.php");
|
||||
include_once("webui/system/misc.php");
|
||||
include_once("webui/model/health/health.php");
|
||||
|
||||
final class FormatTest extends TestCase
|
||||
@ -25,4 +26,28 @@ final class FormatTest extends TestCase
|
||||
$this->assertEquals($result, $expected_result);
|
||||
}
|
||||
|
||||
|
||||
public function providerTestConvertDateStringToYmdByTemplateValues() {
|
||||
return [
|
||||
['2021.05.01', 'y.m.d', ['0','0','0']],
|
||||
['2021.05.01', 'Y.m', ['0','0','0']],
|
||||
['2021.05.01', 'Y.m.d.e', ['0','0','0']],
|
||||
['2021.05.01', 'Y.m.d', ['2021','05','01']],
|
||||
['2021.05.01', 'Y.m.d.', ['0','0','0']],
|
||||
['2021.05.01', 'Y-m-d', ['2021','05','01']],
|
||||
['12/01/2008', 'm/d/Y', ['2008','12','01']],
|
||||
['12-01-2008', 'm-d-Y', ['2008','12','01']],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerTestConvertDateStringToYmdByTemplateValues
|
||||
*/
|
||||
|
||||
public function test_convert_date_string_to_ymd_by_template($date, $date_template, $expected_result) {
|
||||
$result = convert_date_string_to_ymd_by_template($date, $date_template);
|
||||
$this->assertEquals($result, $expected_result);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -436,6 +436,26 @@ function fetch_url($url = '') {
|
||||
}
|
||||
|
||||
|
||||
function convert_date_string_to_ymd_by_template($date_string, $date_template) {
|
||||
$Y = $m = $d = 0;
|
||||
|
||||
$s = $template_array = preg_split("/(\.|\-|\/)/", $date_template);
|
||||
sort($s);
|
||||
|
||||
$date_array = preg_split("/(\.|\-|\/)/", $date_string);
|
||||
|
||||
if($s != ['Y','d','m'] || count($template_array) != 3 || count($date_array) != 3) {
|
||||
return [$Y, $m, $d];
|
||||
}
|
||||
|
||||
while(list($k, $v) = each($template_array)) {
|
||||
$$v = $date_array[$k];
|
||||
}
|
||||
|
||||
return [$Y, $m, $d];
|
||||
}
|
||||
|
||||
|
||||
function fixup_date_condition($field = '', $date1 = 0, $date2 = 0) {
|
||||
global $session;
|
||||
|
||||
@ -449,12 +469,7 @@ function fixup_date_condition($field = '', $date1 = 0, $date2 = 0) {
|
||||
|
||||
|
||||
if($date1) {
|
||||
list($y,$m,$d) = preg_split("/(\.|\-|\/)/", $date1);
|
||||
|
||||
if(DATE_TEMPLATE == 'd/m/Y') { $a = $y; $y = $d; $d = $a; }
|
||||
|
||||
if($m == '*') { $m = 0; }
|
||||
if($d == '*') { $d = 0; }
|
||||
list($y,$m,$d) = convert_date_string_to_ymd_by_template($date1, DATE_TEMPLATE);
|
||||
|
||||
$date1 = mktime(0, 0, 0, $m, $d, $y);
|
||||
|
||||
@ -462,9 +477,7 @@ function fixup_date_condition($field = '', $date1 = 0, $date2 = 0) {
|
||||
}
|
||||
|
||||
if($date2) {
|
||||
list($y,$m,$d) = preg_split("/(\.|\-|\/)/", $date2);
|
||||
|
||||
if(DATE_TEMPLATE == 'd/m/Y') { $a = $y; $y = $d; $d = $a; }
|
||||
list($y,$m,$d) = convert_date_string_to_ymd_by_template($date2, DATE_TEMPLATE);
|
||||
|
||||
$date2 = mktime(23, 59, 59, $m, $d, $y);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user