Skip to content

Commit

Permalink
Added DateTimeRegularExpressions with RFC3339 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
milan-zelenka authored Jul 12, 2022
1 parent 488e081 commit 38d7e1c
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/DateTimeRegularExpressions.php
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])$#';
}
141 changes: 141 additions & 0 deletions src/DateTimeRegularExpressionsTest.php
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',
],
];
}
}

0 comments on commit 38d7e1c

Please sign in to comment.