Skip to content

Commit

Permalink
RFC3339 DateTime format as a default + support for milliseconds (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikvoda authored Dec 16, 2020
1 parent c36207f commit e8610ae
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 80 deletions.
65 changes: 50 additions & 15 deletions src/DateTimeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BrandEmbassy\DateTime;

use BrandEmbassy\DateTime\Format\Rfc3339ExtendedFormat;
use DateTime;
use DateTimeInterface;
use DateTimeZone;
Expand All @@ -10,68 +11,102 @@

final class DateTimeFormatter
{
public static function format(DateTimeInterface $dateTime, string $format): string
public static function formatAs(DateTimeInterface $dateTime, string $format): string
{
return $dateTime->format($format);
}


public static function formatTimestamp(int $timestamp, string $format): string
public static function formatTimestampAs(int $timestamp, string $format): string
{
$dateTimeImmutable = DateTimeFromTimestamp::create($timestamp);

return self::format($dateTimeImmutable, $format);
return self::formatAs($dateTimeImmutable, $format);
}


public static function formatAsAtom(DateTimeInterface $dateTime): string
public static function format(DateTimeInterface $dateTime): string
{
return self::format($dateTime, DateTime::ATOM);
return self::formatAs($dateTime, DateTime::RFC3339);
}


public static function formatTimestampAsAtom(int $timestamp): string
public static function formatWithMilliseconds(DateTimeInterface $dateTime): string
{
return self::formatAs($dateTime, Rfc3339ExtendedFormat::getOutputFormat());
}


public static function formatTimestamp(int $timestamp): string
{
$dateTimeImmutable = DateTimeFromTimestamp::create($timestamp);

return self::formatAsAtom($dateTimeImmutable);
return self::format($dateTimeImmutable);
}


public static function formatInTimezone(
public static function formatTimestampIncludingMilliseconds(int $timestampIncludingMilliseconds): string
{
$dateTimeImmutable = DateTimeFromTimestamp::createIncludingMilliseconds($timestampIncludingMilliseconds);

return self::formatWithMilliseconds($dateTimeImmutable);
}


public static function formatInTimezoneAs(
DateTimeInterface $dateTime,
DateTimeZone $dateTimeZone,
string $format
): string {
assert(method_exists($dateTime, 'setTimezone'));

return self::format($dateTime->setTimezone($dateTimeZone), $format);
return self::formatAs($dateTime->setTimezone($dateTimeZone), $format);
}


public static function formatInTimezoneAsAtom(DateTimeInterface $dateTime, DateTimeZone $dateTimeZone): string
public static function formatInTimezone(DateTimeInterface $dateTime, DateTimeZone $dateTimeZone): string
{
assert(method_exists($dateTime, 'setTimezone'));

return self::formatAsAtom($dateTime->setTimezone($dateTimeZone));
return self::format($dateTime->setTimezone($dateTimeZone));
}


public static function formatTimestampInTimezone(
public static function formatInTimezoneWithMilliseconds(
DateTimeInterface $dateTime,
DateTimeZone $dateTimeZone
): string {
assert(method_exists($dateTime, 'setTimezone'));

return self::formatWithMilliseconds($dateTime->setTimezone($dateTimeZone));
}


public static function formatTimestampInTimezoneAs(
int $timestamp,
DateTimeZone $dateTimeZone,
string $format
): string {
$dateTimeImmutable = DateTimeFromTimestamp::create($timestamp);

return self::formatInTimezone($dateTimeImmutable, $dateTimeZone, $format);
return self::formatInTimezoneAs($dateTimeImmutable, $dateTimeZone, $format);
}


public static function formatTimestampInTimezoneAsAtom(int $timestamp, DateTimeZone $dateTimeZone): string
public static function formatTimestampInTimezone(int $timestamp, DateTimeZone $dateTimeZone): string
{
$dateTimeImmutable = DateTimeFromTimestamp::create($timestamp);

return self::formatInTimezoneAsAtom($dateTimeImmutable, $dateTimeZone);
return self::formatInTimezone($dateTimeImmutable, $dateTimeZone);
}


public static function formatTimestampWithMillisecondsInTimezone(
int $timestampIncludingMilliseconds,
DateTimeZone $dateTimeZone
): string {
$dateTimeImmutable = DateTimeFromTimestamp::createIncludingMilliseconds($timestampIncludingMilliseconds);

return self::formatInTimezoneWithMilliseconds($dateTimeImmutable, $dateTimeZone);
}
}
65 changes: 48 additions & 17 deletions src/DateTimeFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,45 @@
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use PHPStan\Testing\TestCase;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;

final class DateTimeFormatterTest extends TestCase
{
private const DATETIME_IN_ATOM = '2017-05-10T12:13:14+02:00';
private const DATETIME_WITHOUT_MILLISECONDS = '2017-05-10T12:13:14+02:00';
private const DATETIME_WITH_MILLISECONDS = '2017-05-10T12:13:14.000+02:00';


/**
* @dataProvider getDateTimeToFormat
*/
public function testFormatAsAtom(DateTimeInterface $dateTime): void
public function testFormat(DateTimeInterface $dateTime): void
{
$formattedDateTime = DateTimeFormatter::formatAsAtom($dateTime);
$formattedDateTime = DateTimeFormatter::format($dateTime);

Assert::assertSame(self::DATETIME_IN_ATOM, $formattedDateTime);
Assert::assertSame(self::DATETIME_WITHOUT_MILLISECONDS, $formattedDateTime);
}


/**
* @dataProvider getDateTimeToFormat
*/
public function testFormatInTimezone(DateTimeInterface $dateTime): void
public function testFormatWithMilliseconds(DateTimeInterface $dateTime): void
{
$formattedDateTime = DateTimeFormatter::formatWithMilliseconds($dateTime);

Assert::assertSame(self::DATETIME_WITH_MILLISECONDS, $formattedDateTime);
}


/**
* @dataProvider getDateTimeToFormat
*/
public function testFormatInTimezoneAs(DateTimeInterface $dateTime): void
{
$expectedUtcFormat = '2017-05-10T10:13:14+00:00';

$formattedDateTime = DateTimeFormatter::formatInTimezone(
$formattedDateTime = DateTimeFormatter::formatInTimezoneAs(
$dateTime,
new DateTimeZone('UTC'),
DateTime::ATOM
Expand All @@ -45,11 +57,11 @@ public function testFormatInTimezone(DateTimeInterface $dateTime): void
/**
* @dataProvider getDateTimeToFormat
*/
public function testFormatInTimezoneAsAtom(DateTimeInterface $dateTime): void
public function testFormatInTimezone(DateTimeInterface $dateTime): void
{
$expectedUtcFormat = '2017-05-10T10:13:14+00:00';

$formattedDateTime = DateTimeFormatter::formatInTimezoneAsAtom($dateTime, new DateTimeZone('UTC'));
$formattedDateTime = DateTimeFormatter::formatInTimezone($dateTime, new DateTimeZone('UTC'));

Assert::assertSame($expectedUtcFormat, $formattedDateTime);
}
Expand All @@ -61,31 +73,39 @@ public function testFormatInTimezoneAsAtom(DateTimeInterface $dateTime): void
public function getDateTimeToFormat(): array
{
return [
'immutable' => [new DateTimeImmutable(self::DATETIME_IN_ATOM)],
'muttable' => [new DateTime(self::DATETIME_IN_ATOM)],
'immutable' => [new DateTimeImmutable(self::DATETIME_WITHOUT_MILLISECONDS)],
'muttable' => [new DateTime(self::DATETIME_WITHOUT_MILLISECONDS)],
];
}


public function testFormatTimestamp(): void
{
$formattedDateTime = DateTimeFormatter::formatTimestamp(1496237560, 'Y-m-d');
$formattedDateTime = DateTimeFormatter::formatTimestampAs(1496237560, 'Y-m-d');

Assert::assertSame('2017-05-31', $formattedDateTime);
}


public function testFormatTimestampAsAtom(): void
{
$formattedDateTime = DateTimeFormatter::formatTimestampAsAtom(1496237560);
$formattedDateTime = DateTimeFormatter::formatTimestamp(1496237560);

Assert::assertSame('2017-05-31T13:32:40+00:00', $formattedDateTime);
}


public function testFormatTimestampInTimezone(): void
public function testFormatTimestampWithMillis(): void
{
$formattedDateTime = DateTimeFormatter::formatTimestampInTimezone(
$formattedDateTime = DateTimeFormatter::formatTimestampIncludingMilliseconds(1496237560456);

Assert::assertSame('2017-05-31T13:32:40.456+00:00', $formattedDateTime);
}


public function testFormatTimestampInTimezoneAs(): void
{
$formattedDateTime = DateTimeFormatter::formatTimestampInTimezoneAs(
1496237560,
new DateTimeZone('Europe/Prague'),
DateTime::ATOM
Expand All @@ -95,13 +115,24 @@ public function testFormatTimestampInTimezone(): void
}


public function testFormatTimestampInTimezoneAsAtom(): void
public function testFormatTimestampInTimezone(): void
{
$formattedDateTime = DateTimeFormatter::formatTimestampInTimezoneAsAtom(
$formattedDateTime = DateTimeFormatter::formatTimestampInTimezone(
1496237560,
new DateTimeZone('Europe/Prague')
);

Assert::assertSame('2017-05-31T15:32:40+02:00', $formattedDateTime);
}


public function testFormatTimestampWithMillisecondsInTimezone(): void
{
$formattedDateTime = DateTimeFormatter::formatTimestampWithMillisecondsInTimezone(
1496237560456,
new DateTimeZone('Europe/Prague')
);

Assert::assertSame('2017-05-31T15:32:40.456+02:00', $formattedDateTime);
}
}
20 changes: 13 additions & 7 deletions src/DateTimeFromString.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BrandEmbassy\DateTime;

use BrandEmbassy\DateTime\Format\Rfc3339ExtendedFormat;
use DateTime;
use DateTimeImmutable;
use DateTimeZone;
Expand All @@ -13,7 +14,7 @@ final class DateTimeFromString
/**
* @throws InvalidDateTimeStringException
*/
public static function create(string $format, string $dateTimeString): DateTimeImmutable
public static function createFromFormat(string $format, string $dateTimeString): DateTimeImmutable
{
$dateTime = DateTimeImmutable::createFromFormat($format, $dateTimeString);

Expand All @@ -24,19 +25,24 @@ public static function create(string $format, string $dateTimeString): DateTimeI
}


/**
* @throws InvalidDateTimeStringException
*/
public static function createFromAtom(string $dateTimeAtomString): DateTimeImmutable
public static function create(string $dateTimeStringInRfc3339): DateTimeImmutable
{
return self::createFromFormat(DateTime::RFC3339, $dateTimeStringInRfc3339);
}


public static function createWithMilliseconds(string $dateTimeStringInRfc3339ExtendedString): DateTimeImmutable
{
return self::create(DateTime::ATOM, $dateTimeAtomString);
$format = Rfc3339ExtendedFormat::getInputFormat();

return self::createFromFormat($format, $dateTimeStringInRfc3339ExtendedString);
}


/**
* @throws InvalidDateTimeStringException
*/
public static function createWithTimezone(
public static function createWithTimezoneFromFormat(
string $format,
string $dateTimeString,
DateTimeZone $timezone
Expand Down
Loading

0 comments on commit e8610ae

Please sign in to comment.