Better time handling in rcube_utils::clean_datestr()

pull/5284/head^2
Aleksander Machniak 8 years ago
parent ec1525a1e6
commit 906cf101c3

@ -810,15 +810,15 @@ class rcube_utils
$date = trim($date); $date = trim($date);
// try to fix dd/mm vs. mm/dd discrepancy, we can't do more here // try to fix dd/mm vs. mm/dd discrepancy, we can't do more here
if (preg_match('/^(\d{1,2})[.\/-](\d{1,2})[.\/-](\d{4})(.*)$/', $date, $m)) { if (preg_match('/^(\d{1,2})[.\/-](\d{1,2})[.\/-](\d{4})(\s.*)?$/', $date, $m)) {
$mdy = $m[2] > 12 && $m[1] <= 12; $mdy = $m[2] > 12 && $m[1] <= 12;
$day = $mdy ? $m[2] : $m[1]; $day = $mdy ? $m[2] : $m[1];
$month = $mdy ? $m[1] : $m[2]; $month = $mdy ? $m[1] : $m[2];
$date = sprintf('%04d-%02d-%02d%s', intval($m[3]), $month, $day, $m[4] ?: '00:00:00'); $date = sprintf('%04d-%02d-%02d%s', $m[3], $month, $day, $m[4] ?: ' 00:00:00');
} }
// I've found that YYYY.MM.DD is recognized wrong, so here's a fix // I've found that YYYY.MM.DD is recognized wrong, so here's a fix
else if (preg_match('/^(\d{4})\.(\d{1,2})\.(\d{1,2})(.*)$/', $date)) { else if (preg_match('/^(\d{4})\.(\d{1,2})\.(\d{1,2})(\s.*)?$/', $date, $m)) {
$date = str_replace('.', '-', $date) . ($m[4] ?: '00:00:00'); $date = sprintf('%04d-%02d-%02d%s', $m[1], $m[2], $m[3], $m[4] ?: ' 00:00:00');
} }
return $date; return $date;

@ -322,13 +322,24 @@ class Framework_Utils extends PHPUnit_Framework_TestCase
'20130422' => '2013-04-22', '20130422' => '2013-04-22',
'1900-10-10' => '1900-10-10', '1900-10-10' => '1900-10-10',
'01-01-1900' => '1900-01-01', '01-01-1900' => '1900-01-01',
'01/30/1960' => '1960-01-30' '01/30/1960' => '1960-01-30',
'1960.12.11 01:02:00' => '1960-12-11',
); );
foreach ($test as $datetime => $ts) { foreach ($test as $datetime => $ts) {
$result = rcube_utils::anytodatetime($datetime); $result = rcube_utils::anytodatetime($datetime);
$this->assertSame($ts, $result ? $result->format('Y-m-d') : false, "Error parsing date: $datetime"); $this->assertSame($ts, $result ? $result->format('Y-m-d') : false, "Error parsing date: $datetime");
} }
$test = array(
'12/11/2013 01:02:00' => '2013-11-12 01:02:00',
'1960.12.11 01:02:00' => '1960-12-11 01:02:00',
);
foreach ($test as $datetime => $ts) {
$result = rcube_utils::anytodatetime($datetime);
$this->assertSame($ts, $result ? $result->format('Y-m-d H:i:s') : false, "Error parsing date: $datetime");
}
} }
/** /**

Loading…
Cancel
Save