fix empty WordPress timezone_string option resulting in Invalid DateTime [#10]

This commit is contained in:
Daniel Waxweiler 2021-08-24 19:23:57 +02:00
parent cee386acd2
commit 32e87115b3
5 changed files with 29 additions and 2 deletions

View File

@ -5,6 +5,7 @@
#### Deprecated #### Deprecated
#### Removed #### Removed
#### Fixed #### Fixed
- Fix empty WordPress timezone_string option resulting in Invalid DateTime
#### Security #### Security
### [0.6.1] - 2021-07-13 ### [0.6.1] - 2021-07-13

View File

@ -6,6 +6,26 @@ test('#getShortDate usual date', t => {
t.is(d.getShortDate(), '24/12/2020') 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 => { test('#get24Time usual time', t => {
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' }) const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
t.is(d.get24Time(), '16:45') t.is(d.get24Time(), '16:45')

View File

@ -3,6 +3,12 @@ import { DateTime } from 'luxon'
export default class DateTimeWrapper { export default class DateTimeWrapper {
constructor({ locale = 'en-GB', text, timeZone = 'utc' } = {}) { 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 }) this.dateTime = DateTime.fromISO(text, { locale, zone: timeZone })
} }

View File

@ -29,7 +29,7 @@ class EventsListShortcut {
$locale = str_replace('_', '-', get_locale()); $locale = str_replace('_', '-', get_locale());
$groupName = $atts_with_overriden_defaults['group-name']; $groupName = $atts_with_overriden_defaults['group-name'];
$url = Settings::getUrl(); $url = Settings::getUrl();
$timeZone = get_option('timezone_string'); $timeZone = wp_timezone_string();
$isShortOffsetNameShown = Settings::isShortOffsetNameShown(); $isShortOffsetNameShown = Settings::isShortOffsetNameShown();
ob_start(); ob_start();

View File

@ -30,7 +30,7 @@ class EventsListWidget extends \WP_Widget {
$locale = str_replace('_', '-', get_locale()); $locale = str_replace('_', '-', get_locale());
$groupName = isset($options['groupName']) ? $options['groupName'] : ''; $groupName = isset($options['groupName']) ? $options['groupName'] : '';
$url = Settings::getUrl(); $url = Settings::getUrl();
$timeZone = get_option('timezone_string'); $timeZone = wp_timezone_string();
$isShortOffsetNameShown = Settings::isShortOffsetNameShown(); $isShortOffsetNameShown = Settings::isShortOffsetNameShown();
require dirname(__DIR__) . '/view/events-list.php'; require dirname(__DIR__) . '/view/events-list.php';