-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DateTimeRegularExpressions with RFC3339 (#23)
- Loading branch information
1 parent
488e081
commit 38d7e1c
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace BrandEmbassy\DateTime; | ||
|
||
use Nette\StaticClass; | ||
|
||
/** | ||
* @final | ||
*/ | ||
class DateTimeRegularExpressions | ||
{ | ||
use StaticClass; | ||
|
||
// RFC3339 without milliseconds - non-extended | ||
// Compliant with \DateTimeInterface::RFC3339 | ||
public const RFC3339 = '#^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(Z|[+-](2[0-3]|[01][0-9]):[0-5][0-9])$#'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace BrandEmbassy\DateTime; | ||
|
||
use Nette\Utils\Strings; | ||
use PHPUnit\Framework\Assert; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @final | ||
*/ | ||
class DateTimeRegularExpressionsTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider dateTimeStringsDataProvider | ||
*/ | ||
public function testDateTimeStringMatchesPattern( | ||
bool $expectedMatch, | ||
string $pattern, | ||
string $dateTime | ||
): void { | ||
$doesMatch = Strings::match($dateTime, $pattern) !== null; | ||
|
||
Assert::assertSame($expectedMatch, $doesMatch); | ||
} | ||
|
||
|
||
/** | ||
* @return array<string, array<string, mixed>> | ||
*/ | ||
public function dateTimeStringsDataProvider(): array | ||
{ | ||
return [ | ||
'RFC3339 | Valid DateTime with specified positive timezone' => [ | ||
'expectedMatch' => true, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-31T23:59:59+01:00', | ||
], | ||
'RFC3339 | Valid DateTime with specified negative timezone' => [ | ||
'expectedMatch' => true, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-31T23:59:59-01:00', | ||
], | ||
'RFC3339 | Valid DateTime with "Zulu" (UTC offset of 00:00) timezone' => [ | ||
'expectedMatch' => true, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-31T23:59:59Z', | ||
], | ||
'RFC3339 | Invalid milliseconds' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-31T23:59:59.123Z', | ||
], | ||
'RFC3339 | Invalid characters before DateTime' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => 'abc2000-12-31T23:59:59Z', | ||
], | ||
'RFC3339 | Invalid characters after DateTime' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-31T23:59:59Zabc', | ||
], | ||
'RFC3339 | Invalid year' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '19999-12-31T23:59:59Z', | ||
], | ||
'RFC3339 | Invalid month - 0' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-0-31T23:59:59Z', | ||
], | ||
'RFC3339 | Invalid month - 13' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-13-31T23:59:59Z', | ||
], | ||
'RFC3339 | Invalid day - 0' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-0T23:59:59Z', | ||
], | ||
'RFC3339 | Invalid day - 32' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-32T23:59:59Z', | ||
], | ||
'RFC3339 | Invalid hour - 0' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-31T0:59:59Z', | ||
], | ||
'RFC3339 | Invalid hour - 24' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-31T24:59:59Z', | ||
], | ||
'RFC3339 | Invalid minute - 0' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-31T23:0:59Z', | ||
], | ||
'RFC3339 | Invalid minute - 60' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-31T23:60:59Z', | ||
], | ||
'RFC3339 | Invalid second - 0' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-31T23:59:0Z', | ||
], | ||
'RFC3339 | Invalid second - 60' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-31T23:59:60Z', | ||
], | ||
'RFC3339 | Invalid timezone hour - 0' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-31T23:59:59+0:00', | ||
], | ||
'RFC3339 | Invalid timezone hour - 24' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-31T23:59:59+24:00', | ||
], | ||
'RFC3339 | Invalid timezone minute - 0' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-31T23:59:59+01:0', | ||
], | ||
'RFC3339 | Invalid timezone minute - 60' => [ | ||
'expectedMatch' => false, | ||
'pattern' => DateTimeRegularExpressions::RFC3339, | ||
'dateTime' => '2000-12-31T23:59:59+01:60', | ||
], | ||
]; | ||
} | ||
} |