Skip to content

Commit ec48174

Browse files
committed
Made Util::getCurrentTime() attempt DateTimeZone creation with whatever the timezone is, and fallback to GMT offset only on failure;
Changed Util::parseValue() to recognize not just dates, but also "datetime"s.
1 parent 7dd1aaa commit ec48174

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

RELEASE-1.0.0b6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Util stuff, mostly.
77
- getCurrentTime()
88
- newRequest()
99
* Util::parseValue() now supports letter notation for time (1h2m3s), not just double colon notation (01:02:03), modeled after RouterOS. Related to that is also that leading zeroes, and zero minutes and seconds are now optional (e.g. "1:" is a valid way of saying 1 hour). Sub-second information is rounded up to the nearest second on current PHP versions (future versions are expected to support sub-second information in DateInterval by allowing seconds to be a double; The code currently attempts to give DateInterval a double, falling back to rounding to a second).
10-
* Util::parseValue() now recognizes dates in the "M/j/Y" format (used across RouterOS), and translates them to a DateTime object that is midnight UTC time at the specified date.
10+
* Util::parseValue() now recognizes dates in the "M/j/Y H:i:s" format (used across RouterOS), and turns that into a DateTime object for that time (or midnight if the time part is omitted) in UTC.
1111
* Util::getAll() now throws a NotSupportedException if the arguments "follow", "follow-only" or "count-only" are used. The first two, because PHP would hang (since Client::sendSync() is used under the hood), and the last one because it's unredable in the returned output (use Util::count() instead).
1212
* Util::setMenu() can now go back to the root menu.
1313
* Client::login() consumes the !done or !fatal response even when called on an already logged in connection.

src/PEAR2/Net/RouterOS/Util.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,26 @@ public static function parseValue($value)
196196
//@codeCoverageIgnoreEnd
197197
} elseif (preg_match(
198198
'#^
199-
(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)
199+
(?<mon>jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)
200200
/
201-
(\d\d?)
201+
(?<day>\d\d?)
202202
/
203-
(\d{4})
203+
(?<year>\d{4})
204+
(?:
205+
\s+(?<time>\d{2}\:\d{2}:\d{2})
206+
)?
204207
$#uix',
205208
$value,
206209
$date
207210
)) {
208211
try {
212+
if (!isset($date['time'])) {
213+
$date['time'] = '00:00:00';
214+
}
209215
return DateTime::createFromFormat(
210-
'M/j/Y',
211-
ucfirst($date[1]) . '/' . (int)$date[2] . '/' . $date[3],
216+
'M/j/Y H:i:s',
217+
ucfirst($date['mon']) . '/' . (int)$date['day'] .
218+
"/{$date['year']} {$date['time']}",
212219
new DateTimeZone('UTC')
213220
);
214221
} catch (E $e) {
@@ -677,8 +684,9 @@ public function clearIdCache()
677684
*
678685
* Gets the current time on the router, regardless of the current menu.
679686
*
680-
* If your router uses a "manual" timezone, the resulting object will use
681-
* the "gmt-offset" as the timezone identifier.
687+
* If the timezone is one known to both RouterOS and PHP, it will be used
688+
* as the timezone identifier. Otherwise (e.g. "manual"), the current GMT
689+
* offset will be used as a timezone, without any DST awareness.
682690
*
683691
* @return DateTime The current time of the router, as a DateTime object.
684692
*/
@@ -687,20 +695,20 @@ public function getCurrentTime()
687695
$clock = $this->client->sendSync(
688696
new Request('/system/clock/print')
689697
)->current();
690-
$datetime = ucfirst($clock->getProperty('date')) . ' ' .
698+
$datetime = ucfirst(strtolower($clock->getProperty('date'))) . ' ' .
691699
$clock->getProperty('time');
692-
if ('manual' === $clock->getProperty('time-zone-name')) {
693-
$result = DateTime::createFromFormat(
694-
'M/j/Y H:i:s P',
695-
$datetime . ' ' . $clock->getProperty('gmt-offset'),
696-
new DateTimeZone('UTC')
697-
);
698-
} else {
700+
try {
699701
$result = DateTime::createFromFormat(
700702
'M/j/Y H:i:s',
701703
$datetime,
702704
new DateTimeZone($clock->getProperty('time-zone-name'))
703705
);
706+
} catch (E $e) {
707+
$result = DateTime::createFromFormat(
708+
'M/j/Y H:i:s P',
709+
$datetime . ' ' . $clock->getProperty('gmt-offset'),
710+
new DateTimeZone('UTC')
711+
);
704712
}
705713
return $result;
706714
}

0 commit comments

Comments
 (0)