diff --git a/lib/SimpleSAML/Utils/Time.php b/lib/SimpleSAML/Utils/Time.php index 9898f8b503d9f327c5a98764f682bd352e1afcd3..43472e9677f5bdf36bfa8f4cf706b2f941422444 100644 --- a/lib/SimpleSAML/Utils/Time.php +++ b/lib/SimpleSAML/Utils/Time.php @@ -91,7 +91,8 @@ class Time } // parse the duration. We use a very strict pattern - $durationRegEx = '#^(-?)P(?:(?:(?:(\\d+)Y)?(?:(\\d+)M)?(?:(\\d+)D)?(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:(\\d+)(?:[.,]\d+)?S)?)?)|(?:(\\d+)W))$#D'; + $durationRegEx = '#^(-?)P(?:(?:(?:(\\d+)Y)?(?:(\\d+)M)?(?:(\\d+)D)?(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:(\\d+)'. + '(?:[.,]\d+)?S)?)?)|(?:(\\d+)W))$#D'; if (!preg_match($durationRegEx, $duration, $matches)) { throw new \InvalidArgumentException('Invalid ISO 8601 duration: '.$duration); } @@ -159,4 +160,4 @@ class Time return $timestamp; } -} \ No newline at end of file +} diff --git a/tests/lib/SimpleSAML/Utils/TimeTest.php b/tests/lib/SimpleSAML/Utils/TimeTest.php new file mode 100644 index 0000000000000000000000000000000000000000..20c35f6a4411f894a3a5175b8e8e727a658f7f47 --- /dev/null +++ b/tests/lib/SimpleSAML/Utils/TimeTest.php @@ -0,0 +1,21 @@ +<?php + +namespace SimpleSAML\Test\Utils; + +use SimpleSAML\Utils\Time; + +class TimeTest extends \PHPUnit_Framework_TestCase +{ + + /** + * Test the SimpleSAML\Utils\Time::generateTimestamp() method. + */ + public function testGenerateTimestamp() + { + // make sure passed timestamps are used + $this->assertEquals('2016-03-03T14:48:05Z', Time::generateTimestamp(1457016485)); + + // test timestamp generation for current time + $this->assertRegExp('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/', Time::generateTimestamp()); + } +}