take WordPress time zone into account

This commit is contained in:
Daniel Waxweiler 2021-05-05 10:40:46 +02:00
parent 22c0b22b8e
commit 89145fe9ed
7 changed files with 15 additions and 8 deletions

View File

@ -1,6 +1,6 @@
### [Unreleased]
#### Added
- Localise dates based on the WordPress locale
- Localise dates based on the WordPress locale and time zone
#### Changed
- Clearly list features in `readme.txt` description
#### Deprecated

View File

@ -2,8 +2,11 @@ import { DateTime } from 'luxon'
export default class DateTimeWrapper {
constructor({ locale, text }) {
this.dateTime = DateTime.fromISO(text, { locale, setZone: true })
constructor({ locale, text, timeZone }) {
if (!timeZone) {
timeZone = 'utc'
}
this.dateTime = DateTime.fromISO(text, { locale, zone: timeZone })
}
getShortDate() {

View File

@ -4,6 +4,7 @@ import { createAnchorElement } from './html-creator'
export function displayEvents({ data, document, list }) {
const locale = list.getAttribute('data-locale')
const maxEventsCount = list.getAttribute('data-maximum')
const timeZone = list.getAttribute('data-time-zone')
const events = data.events ? data.events.elements : data.group.organizedEvents.elements
const eventsCount = Math.min(maxEventsCount, events.length)
for (let i = 0; i < eventsCount; i++) {
@ -15,7 +16,7 @@ export function displayEvents({ data, document, list }) {
const br = document.createElement('br')
li.appendChild(br)
const date = Formatter.formatDate({ locale, start: events[i].beginsOn, end: events[i].endsOn })
const date = Formatter.formatDate({ locale, start: events[i].beginsOn, end: events[i].endsOn, timeZone })
const textnode = document.createTextNode(date)
li.appendChild(textnode)

View File

@ -2,9 +2,9 @@ import DateTimeWrapper from './date-time-wrapper'
export default class Formatter {
static formatDate({ locale, start, end }) {
const startDateTime = new DateTimeWrapper({ locale, text: start })
const endDateTime = new DateTimeWrapper({ locale, text: end })
static formatDate({ locale, timeZone, start, end }) {
const startDateTime = new DateTimeWrapper({ locale, text: start, timeZone })
const endDateTime = new DateTimeWrapper({ locale, text: end, timeZone })
let dateText = startDateTime.getShortDate()
dateText += ' ' + startDateTime.get24Time()
dateText += ' - '

View File

@ -30,6 +30,7 @@ class EventsListShortcut {
$groupName = $atts_with_overriden_defaults['group-name'];
$url = Settings::getUrl();
$textDomain = TEXT_DOMAIN;
$timeZone = get_option('timezone_string');
ob_start();
require dirname(__DIR__) . '/view/events-list.php';

View File

@ -31,6 +31,7 @@ class EventsListWidget extends \WP_Widget {
$groupName = isset($options['groupName']) ? $options['groupName'] : '';
$url = Settings::getUrl();
$textDomain = TEXT_DOMAIN;
$timeZone = get_option('timezone_string');
require dirname(__DIR__) . '/view/events-list.php';

View File

@ -8,6 +8,7 @@ if (!defined('ABSPATH')) {
data-url="<?php echo esc_attr($url); ?>"
data-locale="<?php echo esc_attr($locale); ?>"
data-maximum="<?php echo esc_attr($eventsCount); ?>"
data-group-name="<?php echo esc_attr($groupName); ?>">
data-group-name="<?php echo esc_attr($groupName); ?>"
data-time-zone="<?php echo esc_attr($timeZone); ?>">
<li style="display: none;"><?php echo esc_html_e('The events could not be loaded!', $textDomain); ?></li>
</ul>