Skip to content

Commit 16ca048

Browse files
authored
Add ICU date formatting support in parseDate method
1 parent 5e1a6cf commit 16ca048

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

lib/Nag.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Horde\Util\Util;
44
use Horde\Date\Format;
5+
use Horde\Date\Formatter\IcuFormatter;
56

67
/**
78
* Nag Base Class.
@@ -168,6 +169,36 @@ public static function secondsToString($seconds)
168169
*/
169170
public static function parseDate($date, $withtime = true)
170171
{
172+
$date = trim($date);
173+
$dateFormat = $GLOBALS['prefs']->getValue('date_format_mini');
174+
$locale = $GLOBALS['language'] ?? ($GLOBALS['prefs']->getValue('language') ?? 'en_US');
175+
176+
if (!Format::isStrftimeFormat($dateFormat)) {
177+
try {
178+
$formatter = new IcuFormatter();
179+
if ($withtime && preg_match('/^(.+?)\s+(\S+)$/', $date, $parts)) {
180+
$datePart = $formatter->parse($parts[1], $dateFormat, $locale);
181+
$timeFormat = $GLOBALS['prefs']->getValue('twentyFour') ? 'HH:mm' : 'h:mm a';
182+
$timePart = $formatter->parse($parts[2], $timeFormat, $locale);
183+
184+
return new Horde_Date(
185+
['year' => (int) $datePart->format('Y'),
186+
'month' => (int) $datePart->format('n'),
187+
'mday' => (int) $datePart->format('j'),
188+
'hour' => (int) $timePart->format('G'),
189+
'min' => (int) $timePart->format('i'),
190+
'sec' => 0]
191+
);
192+
}
193+
194+
$parsed = $formatter->parse($date, $dateFormat, $locale);
195+
196+
return new Horde_Date($parsed->getTimestamp());
197+
} catch (Exception $e) {
198+
// Fall through to legacy parsing.
199+
}
200+
}
201+
171202
// strptime() is not available on Windows.
172203
if (!function_exists('strptime')) {
173204
return new Horde_Date($date);
@@ -176,7 +207,7 @@ public static function parseDate($date, $withtime = true)
176207
// strptime() is locale dependent, i.e. %p is not always matching
177208
// AM/PM. Set the locale to C to workaround this, but grab the
178209
// locale's D_FMT before that.
179-
$format = $GLOBALS['prefs']->getValue('date_format_mini');
210+
$format = $dateFormat;
180211
if ($withtime) {
181212
$format .= ' '
182213
. ($GLOBALS['prefs']->getValue('twentyFour') ? '%H:%M' : '%I:%M %p');

0 commit comments

Comments
 (0)