From 32e87115b3b281949db7903807b93a6d0e9d4b40 Mon Sep 17 00:00:00 2001 From: Daniel Waxweiler Date: Tue, 24 Aug 2021 19:23:57 +0200 Subject: [PATCH] fix empty WordPress timezone_string option resulting in Invalid DateTime [#10] --- source/connector-mobilizon/changelog.txt | 1 + .../front/date-time-wrapper-test.js | 20 +++++++++++++++++++ .../front/date-time-wrapper.js | 6 ++++++ .../includes/events-list-shortcut.php | 2 +- .../includes/events-list-widget.php | 2 +- 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/source/connector-mobilizon/changelog.txt b/source/connector-mobilizon/changelog.txt index 1a6fa65..7dd309b 100644 --- a/source/connector-mobilizon/changelog.txt +++ b/source/connector-mobilizon/changelog.txt @@ -5,6 +5,7 @@ #### Deprecated #### Removed #### Fixed +- Fix empty WordPress timezone_string option resulting in Invalid DateTime #### Security ### [0.6.1] - 2021-07-13 diff --git a/source/connector-mobilizon/front/date-time-wrapper-test.js b/source/connector-mobilizon/front/date-time-wrapper-test.js index 716786e..c7e6141 100644 --- a/source/connector-mobilizon/front/date-time-wrapper-test.js +++ b/source/connector-mobilizon/front/date-time-wrapper-test.js @@ -6,6 +6,26 @@ test('#getShortDate usual date', t => { t.is(d.getShortDate(), '24/12/2020') }) +test('#getShortDate usual date with timezone string', t => { + const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z', timeZone: 'Europe/Rome' }) + t.is(d.getShortDate(), '24/12/2020') +}) + +test('#getShortDate usual date with fixed offset', t => { + const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z', timeZone: 'UTC+02:00' }) + t.is(d.getShortDate(), '24/12/2020') +}) + +test('#getShortDate usual date with fixed offset without UTC prefix', t => { + const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z', timeZone: '+02:00' }) + t.is(d.getShortDate(), '24/12/2020') +}) + +test('#getShortDate usual date with empty time zone', t => { + const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z', timeZone: '' }) + t.is(d.getShortDate(), '24/12/2020') +}) + test('#get24Time usual time', t => { const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' }) t.is(d.get24Time(), '16:45') diff --git a/source/connector-mobilizon/front/date-time-wrapper.js b/source/connector-mobilizon/front/date-time-wrapper.js index 5bd9db4..60a5185 100644 --- a/source/connector-mobilizon/front/date-time-wrapper.js +++ b/source/connector-mobilizon/front/date-time-wrapper.js @@ -3,6 +3,12 @@ import { DateTime } from 'luxon' export default class DateTimeWrapper { constructor({ locale = 'en-GB', text, timeZone = 'utc' } = {}) { + if (!timeZone) { + timeZone = 'utc' + } + if (timeZone.includes(':') && timeZone.substring(0, 3).toUpperCase() !== 'UTC') { + timeZone = 'UTC' + timeZone + } this.dateTime = DateTime.fromISO(text, { locale, zone: timeZone }) } diff --git a/source/connector-mobilizon/includes/events-list-shortcut.php b/source/connector-mobilizon/includes/events-list-shortcut.php index b0d00ec..bb14cce 100644 --- a/source/connector-mobilizon/includes/events-list-shortcut.php +++ b/source/connector-mobilizon/includes/events-list-shortcut.php @@ -29,7 +29,7 @@ class EventsListShortcut { $locale = str_replace('_', '-', get_locale()); $groupName = $atts_with_overriden_defaults['group-name']; $url = Settings::getUrl(); - $timeZone = get_option('timezone_string'); + $timeZone = wp_timezone_string(); $isShortOffsetNameShown = Settings::isShortOffsetNameShown(); ob_start(); diff --git a/source/connector-mobilizon/includes/events-list-widget.php b/source/connector-mobilizon/includes/events-list-widget.php index 4a8a0cd..b061515 100644 --- a/source/connector-mobilizon/includes/events-list-widget.php +++ b/source/connector-mobilizon/includes/events-list-widget.php @@ -30,7 +30,7 @@ class EventsListWidget extends \WP_Widget { $locale = str_replace('_', '-', get_locale()); $groupName = isset($options['groupName']) ? $options['groupName'] : ''; $url = Settings::getUrl(); - $timeZone = get_option('timezone_string'); + $timeZone = wp_timezone_string(); $isShortOffsetNameShown = Settings::isShortOffsetNameShown(); require dirname(__DIR__) . '/view/events-list.php';