Skip to content

Commit f938b58

Browse files
committed
Modified Util::escapeValue()'s DateTime behavior.
1 parent b1eecfb commit f938b58

File tree

4 files changed

+55
-22
lines changed

4 files changed

+55
-22
lines changed

RELEASE-1.0.0b6

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ Util stuff, mostly.
22

33
* __BREAKING CHANGES:__
44
- Util::edit() is no longer an alias of Util::set(). It's now its own method that can modify or unset a single specified property.
5+
- Util::escapeValue() now converts DateTime objects into a string having the "M/d/Y H:i:s" format (used across RouterOS), or just "M/d/Y" if the time is exactly midnight, and the timezone is UTC. The old behaviour can be achieved by "manually" adding the DateTime to "new DateTime('@0')".
56
* New Util methods:
67
- comment()
78
- getCurrentTime()
89
- newRequest()
910
* 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 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.
11+
* Util::parseValue() now recognizes dates in the "M/d/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.
1112
* 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).
1213
* Util::setMenu() can now go back to the root menu.
1314
* Util::find() now works even when the underlying Client streams responses.

src/PEAR2/Net/RouterOS/Util.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,11 @@ public static function appendScript(
381381
* Turns any native PHP value into an equivalent whole value that can be
382382
* inserted as part of a RouterOS script.
383383
*
384-
* DateTime and DateInterval objects will be casted to RouterOS' "time"
385-
* type. A DateTime object will be converted to a time relative to the UNIX
386-
* epoch time. Note that if a DateInterval does not have the "days" property
387-
* ("a" in formatting), then its months and years will be ignored, because
388-
* they can't be unambiguously converted to a "time" value.
384+
* DateInterval objects will be casted to RouterOS' "time" type.
385+
*
386+
* DateTime objects will be casted to a string following the "M/d/Y H:i:s"
387+
* format. If the time is exactly midnight (including microseconds), and
388+
* the timezone is UTC, the string will include only the "M/d/Y" date.
389389
*
390390
* Unrecognized types (i.e. resources and other objects) are casted to
391391
* strings.
@@ -425,21 +425,17 @@ public static function escapeValue($value)
425425
case 'object':
426426
if ($value instanceof DateTime) {
427427
$usec = $value->format('u');
428-
if ('000000' === $usec) {
429-
unset($usec);
430-
}
431-
$unixEpoch = new DateTime('@0');
432-
$value = $unixEpoch->diff($value);
428+
$usec = '000000' === $usec ? '' : '.' . $usec;
429+
$value = '00:00:00.000000 UTC' === $value->format('H:i:s.u e')
430+
? $value->format('M/d/Y')
431+
: $value->format('M/d/Y H:i:s') . $usec;
433432
}
434433
if ($value instanceof DateInterval) {
435434
if (false === $value->days || $value->days < 0) {
436435
$value = $value->format('%r%dd%H:%I:%S');
437436
} else {
438437
$value = $value->format('%r%ad%H:%I:%S');
439438
}
440-
if (strpos('.', $value) === false && isset($usec)) {
441-
$value .= '.' . $usec;
442-
}
443439
break;
444440
}
445441
//break; intentionally omitted
@@ -706,7 +702,7 @@ public function getCurrentTime()
706702
'gmt-offset'
707703
) as $clockPart) {
708704
$clockParts[$clockPart] = $clock->getProperty($clockPart);
709-
if (is_resource($clockParts[$clockPart])) {
705+
if (Stream::isStream($clockParts[$clockPart])) {
710706
$clockParts[$clockPart] = stream_get_contents(
711707
$clockParts[$clockPart]
712708
);

tests/Misc/ConnectionlessTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,12 +992,12 @@ public function providerUtilParseValue()
992992
return array(
993993
//// This will be moved into a separate "legacy" test,
994994
//// once PHP supports fractional secons in DateInterval...
995-
//'legacy sub-second' => array(
995+
//'0s1ms2us3ns' => array(
996996
// '0s1ms2us3ns',
997997
// new DateInterval('PT0S')
998998
//),
999999
//// ...and this will be moved to a separate "current" test.
1000-
//'current sub-secong' => array(
1000+
//'0s1ms2us3ns' => array(
10011001
// '0s1ms2us3ns',
10021002
// new DateInterval('PT0.001002003S')
10031003
//),

tests/Util/Unsafe.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ public function testExecArgTypes()
581581
)->getAllOfType(Response::TYPE_DATA);
582582
$this->util->remove(TEST_QUEUE_NAME);
583583
$this->assertCount(1, $results);
584-
$this->assertSame('time', $results->getProperty('comment'));
584+
$this->assertSame('str', $results->getProperty('comment'));
585585

586586
$this->util->exec(
587587
'add name=$name target=0.0.0.0/0 comment=[:typeof $comment]',
@@ -738,7 +738,7 @@ public function testExecArgValues()
738738
)->getAllOfType(Response::TYPE_DATA);
739739
$this->util->remove(TEST_QUEUE_NAME);
740740
$this->assertCount(1, $results);
741-
$this->assertSame('00:00:00.000001', $results->getProperty('comment'));
741+
$this->assertSame('Jan/01/1970 00:00:00.000001', $results->getProperty('comment'));
742742

743743
$this->util->exec(
744744
'add name=$name target=0.0.0.0/0 comment=$comment',
@@ -755,14 +755,50 @@ public function testExecArgValues()
755755
)->getAllOfType(Response::TYPE_DATA);
756756
$this->util->remove(TEST_QUEUE_NAME);
757757
$this->assertCount(1, $results);
758-
$this->assertSame('1d00:00:01', $results->getProperty('comment'));
758+
$this->assertSame('Jan/02/1970 00:00:01', $results->getProperty('comment'));
759+
760+
$this->util->exec(
761+
'add name=$name target=0.0.0.0/0 comment=$comment',
762+
array(
763+
'name' => TEST_QUEUE_NAME,
764+
'comment' => new DateTime(
765+
'1970-01-10 00:00:00',
766+
new DateTimezone(TEST_TIMEZONE)
767+
)
768+
)
769+
);
770+
$results = $this->client->sendSync(
771+
$printRequest
772+
)->getAllOfType(Response::TYPE_DATA);
773+
$this->util->remove(TEST_QUEUE_NAME);
774+
$this->assertCount(1, $results);
775+
$this->assertSame('Jan/10/1970 00:00:00', $results->getProperty('comment'));
776+
777+
$datePrime = new DateTime(
778+
'1970-01-10 12:34:56',
779+
new DateTimezone('UTC')
780+
);
781+
$unixEpoch = new DateTime('@0', new DateTimezone('UTC'));
782+
$this->util->exec(
783+
'add name=$name target=0.0.0.0/0 comment=$comment',
784+
array(
785+
'name' => TEST_QUEUE_NAME,
786+
'comment' => $unixEpoch->diff($datePrime)
787+
)
788+
);
789+
$results = $this->client->sendSync(
790+
$printRequest
791+
)->getAllOfType(Response::TYPE_DATA);
792+
$this->util->remove(TEST_QUEUE_NAME);
793+
$this->assertCount(1, $results);
794+
$this->assertSame('1w2d12:34:56', $results->getProperty('comment'));
759795

760796
$this->util->exec(
761797
'add name=$name target=0.0.0.0/0 comment=$comment',
762798
array(
763799
'name' => TEST_QUEUE_NAME,
764800
'comment' => new DateTime(
765-
'1970-01-10 01:02:03',
801+
'1970-01-02 00:00:00',
766802
new DateTimezone('UTC')
767803
)
768804
)
@@ -772,7 +808,7 @@ public function testExecArgValues()
772808
)->getAllOfType(Response::TYPE_DATA);
773809
$this->util->remove(TEST_QUEUE_NAME);
774810
$this->assertCount(1, $results);
775-
$this->assertSame('1w2d01:02:03', $results->getProperty('comment'));
811+
$this->assertSame('Jan/02/1970', $results->getProperty('comment'));
776812

777813
$this->util->exec(
778814
'add name=$name target=0.0.0.0/0 comment=$comment',

0 commit comments

Comments
 (0)