From 063458b2c9e03ed50b8954318deb850a00146e58 Mon Sep 17 00:00:00 2001 From: Daniel Waxweiler Date: Sun, 21 May 2023 16:11:24 +0200 Subject: [PATCH] fix offset --- source/includes/DateTimeWrapper.php | 14 +++++++------- source/includes/Formatter.php | 4 ++-- tests/DateTimeWrapperTest.php | 12 ++++++------ tests/FormatterTest.php | 6 +++--- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/source/includes/DateTimeWrapper.php b/source/includes/DateTimeWrapper.php index 30fde7a..9c5a578 100644 --- a/source/includes/DateTimeWrapper.php +++ b/source/includes/DateTimeWrapper.php @@ -18,17 +18,17 @@ final class DateTimeWrapper { $this->timeZone = new \DateTimeZone($timeZone); } + public function get24Time(): string { + $formatter = \IntlDateFormatter::create($this->locale, \IntlDateFormatter::NONE, \IntlDateFormatter::SHORT, $this->timeZone); + return $formatter->format($this->dateTime); + } + public function getShortDate(): string { $formatter = \IntlDateFormatter::create($this->locale, \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE, $this->timeZone); return $formatter->format($this->dateTime); } - public function getOffset(): string { - return $this->timeZone->getOffset($this->dateTime); - } - - public function get24Time(): string { - $formatter = \IntlDateFormatter::create($this->locale, \IntlDateFormatter::NONE, \IntlDateFormatter::SHORT, $this->timeZone); - return $formatter->format($this->dateTime); + public function getTimeZoneName(): string { + return $this->timeZone->getName(); } } diff --git a/source/includes/Formatter.php b/source/includes/Formatter.php index 88816db..783df52 100644 --- a/source/includes/Formatter.php +++ b/source/includes/Formatter.php @@ -8,7 +8,7 @@ final class Formatter $dateText = $startDateTime->getShortDate(); $dateText .= ' ' . $startDateTime->get24Time(); if (!$end && $isShortOffsetNameShown) { - $dateText .= ' (' . $startDateTime->getOffset() . ')'; + $dateText .= ' (' . $startDateTime->getTimeZoneName() . ')'; } if ($end) { $endDateTime = new DateTimeWrapper($end, $locale, $timeZone); @@ -20,7 +20,7 @@ final class Formatter } $dateText .= $endDateTime->get24Time(); if ($isShortOffsetNameShown) { - $dateText .= ' (' . $endDateTime->getOffset() . ')'; + $dateText .= ' (' . $endDateTime->getTimeZoneName() . ')'; } } return $dateText; diff --git a/tests/DateTimeWrapperTest.php b/tests/DateTimeWrapperTest.php index 09b07e7..7f173a2 100644 --- a/tests/DateTimeWrapperTest.php +++ b/tests/DateTimeWrapperTest.php @@ -5,6 +5,11 @@ use MobilizonConnector\DateTimeWrapper; use PHPUnit\Framework\TestCase; final class DateTimeWrapperTest extends TestCase { + public function testCanGet24TimeForUsualTime(): void { + $d = new DateTimeWrapper('2020-12-24T16:45:00Z'); + $this->assertSame('16:45', $d->get24Time()); + } + public function testCanGetShortDateForUsualDate(): void { $d = new DateTimeWrapper('2020-12-24T16:45:00Z'); $this->assertSame('24/12/2020', $d->getShortDate()); @@ -35,13 +40,8 @@ final class DateTimeWrapperTest extends TestCase { $this->assertSame('24/12/2020', $d->getShortDate()); } - public function testCanGet24TimeForUsualTime(): void { - $d = new DateTimeWrapper('2020-12-24T16:45:00Z'); - $this->assertSame('16:45', $d->get24Time()); - } - public function testCanGetShortOffsetNameForUsualTime(): void { $d = new DateTimeWrapper('2020-12-24T16:45:00Z'); - $this->assertSame('0', $d->getOffset()); // TODO was UTC + $this->assertSame('UTC', $d->getTimeZoneName()); } } diff --git a/tests/FormatterTest.php b/tests/FormatterTest.php index 80d4bd3..56ed230 100644 --- a/tests/FormatterTest.php +++ b/tests/FormatterTest.php @@ -11,7 +11,7 @@ final class FormatterTest extends PHPUnit\Framework\TestCase } public function testCanDateFormatOneDateWithOffset(): void { - $this->assertSame('15/04/2021 10:30 - 15:30 (0)', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z', true)); + $this->assertSame('15/04/2021 10:30 - 15:30 (UTC)', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z', true)); } public function testCanDateFormatOneDateWithTimeZoneOffset(): void { @@ -23,7 +23,7 @@ final class FormatterTest extends PHPUnit\Framework\TestCase } public function testCanDateFormatTwoDatesWithOffset(): void { - $this->assertSame('15/04/2021 10:30 - 16/04/2021 15:30 (0)', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', '2021-04-16T15:30:00Z', true)); + $this->assertSame('15/04/2021 10:30 - 16/04/2021 15:30 (UTC)', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', '2021-04-16T15:30:00Z', true)); } public function testCanDateFormatWhenSecondDateIsNull(): void { @@ -31,7 +31,7 @@ final class FormatterTest extends PHPUnit\Framework\TestCase } public function testCanDateFormatWhenSecondDateIsNullWithOffset(): void { - $this->assertSame('15/04/2021 10:30 (0)', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', null, true)); + $this->assertSame('15/04/2021 10:30 (UTC)', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', null, true)); } public function testCanLocationFormatBothParameters(): void {