diff --git a/source/connector-mobilizon.php b/source/connector-mobilizon.php index 2a62b8e..7c853e1 100644 --- a/source/connector-mobilizon.php +++ b/source/connector-mobilizon.php @@ -17,7 +17,8 @@ require_once __DIR__ . '/includes/Api.php'; require_once __DIR__ . '/includes/EventsCache.php'; require_once __DIR__ . '/includes/Settings.php'; require_once __DIR__ . '/includes/SiteSettings.php'; -require_once __DIR__ . '/includes/DateTimeFormatter.php'; +require_once __DIR__ . '/includes/LocalDateTime.php'; +require_once __DIR__ . '/includes/LocalDateTimeFormatter.php'; require_once __DIR__ . '/includes/LineFormatter.php'; require_once __DIR__ . '/includes/GraphQlClient.php'; require_once __DIR__ . '/includes/EventsListBlock.php'; diff --git a/source/includes/DateTimeFormatter.php b/source/includes/DateTimeFormatter.php deleted file mode 100644 index 88a55de..0000000 --- a/source/includes/DateTimeFormatter.php +++ /dev/null @@ -1,10 +0,0 @@ -getTimestamp(); - return date_i18n($format, $timestamp); - } -} diff --git a/source/includes/EventsListBlock.php b/source/includes/EventsListBlock.php index c0fa566..f91084c 100644 --- a/source/includes/EventsListBlock.php +++ b/source/includes/EventsListBlock.php @@ -53,6 +53,7 @@ class EventsListBlock { $dateFormat = SiteSettings::getDateFormat(); $timeFormat = SiteSettings::getTimeFormat(); + $timeZone = SiteSettings::getTimeZone(); require dirname(__DIR__) . '/view/events-list.php'; } catch (GeneralException $e) { diff --git a/source/includes/EventsListShortcut.php b/source/includes/EventsListShortcut.php index 082b164..9a26a77 100644 --- a/source/includes/EventsListShortcut.php +++ b/source/includes/EventsListShortcut.php @@ -36,6 +36,7 @@ class EventsListShortcut { $dateFormat = SiteSettings::getDateFormat(); $timeFormat = SiteSettings::getTimeFormat(); + $timeZone = SiteSettings::getTimeZone(); require dirname(__DIR__) . '/view/events-list.php'; } catch (GeneralException $e) { diff --git a/source/includes/EventsListWidget.php b/source/includes/EventsListWidget.php index f038702..e4fbd88 100644 --- a/source/includes/EventsListWidget.php +++ b/source/includes/EventsListWidget.php @@ -36,6 +36,7 @@ class EventsListWidget extends \WP_Widget { $dateFormat = SiteSettings::getDateFormat(); $timeFormat = SiteSettings::getTimeFormat(); + $timeZone = SiteSettings::getTimeZone(); require dirname(__DIR__) . '/view/events-list.php'; } catch (GeneralException $e) { diff --git a/source/includes/LineFormatter.php b/source/includes/LineFormatter.php index 42a6c7b..897ad25 100644 --- a/source/includes/LineFormatter.php +++ b/source/includes/LineFormatter.php @@ -3,16 +3,16 @@ namespace MobilizonConnector; final class LineFormatter { - public static function format_date_time(string $dateFormat, string $timeFormat, string $start, ?string $end): string { - $startDateTime = new \DateTimeImmutable($start); - $startDate = DateTimeFormatter::format($startDateTime, $dateFormat); - $startTime = DateTimeFormatter::format($startDateTime, $timeFormat); + public static function format_date_time(\DateTimeZone $timeZone, string $dateFormat, string $timeFormat, string $start, ?string $end): string { + $startDateTime = new LocalDateTime($start, $timeZone); + $startDate = LocalDateTimeFormatter::format($startDateTime, $dateFormat); + $startTime = LocalDateTimeFormatter::format($startDateTime, $timeFormat); $dateText = $startDate . ' ' . $startTime; if ($end) { - $endDateTime = new \DateTimeImmutable($end); - $endDate = DateTimeFormatter::format($endDateTime, $dateFormat); - $endTime = DateTimeFormatter::format($endDateTime, $timeFormat); + $endDateTime = new LocalDateTime($end, $timeZone); + $endDate = LocalDateTimeFormatter::format($endDateTime, $dateFormat); + $endTime = LocalDateTimeFormatter::format($endDateTime, $timeFormat); if ($startDate != $endDate) { $dateText .= ' - '; diff --git a/source/includes/LocalDateTime.php b/source/includes/LocalDateTime.php new file mode 100644 index 0000000..b8c3264 --- /dev/null +++ b/source/includes/LocalDateTime.php @@ -0,0 +1,15 @@ +dateTime = $date->setTimezone($timeZone); + } + + public function getValue() { + return $this->dateTime; + } +} diff --git a/source/includes/LocalDateTimeFormatter.php b/source/includes/LocalDateTimeFormatter.php new file mode 100644 index 0000000..78e8ae1 --- /dev/null +++ b/source/includes/LocalDateTimeFormatter.php @@ -0,0 +1,10 @@ +getValue()->getTimestamp(); + return date_i18n($format, $timestamp); + } +} diff --git a/source/includes/SiteSettings.php b/source/includes/SiteSettings.php index 8b779f2..1b91059 100644 --- a/source/includes/SiteSettings.php +++ b/source/includes/SiteSettings.php @@ -14,4 +14,8 @@ class SiteSettings { return get_option(self::$OPTION_NAME_TIME_FORMAT); } + public static function getTimeZone() { + return wp_timezone(); + } + } diff --git a/source/view/events-list.php b/source/view/events-list.php index 7cf046c..8bd3bc6 100644 --- a/source/view/events-list.php +++ b/source/view/events-list.php @@ -15,7 +15,7 @@ if (!defined('ABSPATH')) {
- +
diff --git a/tests/LineFormatterTest.php b/tests/LineFormatterTest.php index fd58bf3..4294b66 100644 --- a/tests/LineFormatterTest.php +++ b/tests/LineFormatterTest.php @@ -25,15 +25,15 @@ function date_i18n(string $format, int $timestamp) { final class LineFormatterTest extends TestCase { public function testCanDateFormatOneDate(): void { - $this->assertSame('15/04/2021 10:30 - 15:30', LineFormatter::format_date_time('d/m/Y', 'H:i', '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z')); + $this->assertSame('15/04/2021 10:30 - 15:30', LineFormatter::format_date_time(new \DateTimeZone('UTC'), 'd/m/Y', 'H:i', '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z')); } public function testCanDateFormatTwoDates(): void { - $this->assertSame('15/04/2021 10:30 - 16/04/2021 15:30', LineFormatter::format_date_time('d/m/Y', 'H:i', '2021-04-15T10:30:00Z', '2021-04-16T15:30:00Z')); + $this->assertSame('15/04/2021 10:30 - 16/04/2021 15:30', LineFormatter::format_date_time(new \DateTimeZone('UTC'), 'd/m/Y', 'H:i', '2021-04-15T10:30:00Z', '2021-04-16T15:30:00Z')); } public function testCanDateFormatWhenSecondDateIsNull(): void { - $this->assertSame('15/04/2021 10:30', LineFormatter::format_date_time('d/m/Y', 'H:i', '2021-04-15T10:30:00Z', null)); + $this->assertSame('15/04/2021 10:30', LineFormatter::format_date_time(new \DateTimeZone('UTC'), 'd/m/Y', 'H:i', '2021-04-15T10:30:00Z', null)); } public function testCanLocationFormatBothParameters(): void { diff --git a/tests/LocalDateTimeTest.php b/tests/LocalDateTimeTest.php new file mode 100644 index 0000000..19e15aa --- /dev/null +++ b/tests/LocalDateTimeTest.php @@ -0,0 +1,12 @@ +assertEquals(new DateTimeImmutable('2020-12-24T16:45:00Z'), $d->getValue()); + } +}