22
33use Horde \Util \Util ;
44use 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