mirror of
https://github.com/dwaxweiler/connector-mobilizon
synced 2025-06-05 21:59:25 +02:00
use timezone of event
This commit is contained in:
@ -17,8 +17,7 @@ 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/LocalDateTime.php';
|
||||
require_once __DIR__ . '/includes/LocalDateTimeFormatter.php';
|
||||
require_once __DIR__ . '/includes/DateTimeFormatter.php';
|
||||
require_once __DIR__ . '/includes/LineFormatter.php';
|
||||
require_once __DIR__ . '/includes/GraphQlClient.php';
|
||||
require_once __DIR__ . '/includes/EventsListBlock.php';
|
||||
|
10
source/includes/DateTimeFormatter.php
Normal file
10
source/includes/DateTimeFormatter.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
final class DateTimeFormatter
|
||||
{
|
||||
public static function format(\DateTimeImmutable $dateTime, string $format) {
|
||||
$timestamp = $dateTime->getTimestamp();
|
||||
return date_i18n($format, $timestamp);
|
||||
}
|
||||
}
|
@ -53,7 +53,6 @@ class EventsListBlock {
|
||||
|
||||
$dateFormat = SiteSettings::getDateFormat();
|
||||
$timeFormat = SiteSettings::getTimeFormat();
|
||||
$timeZone = SiteSettings::getTimeZone();
|
||||
|
||||
require dirname(__DIR__) . '/view/events-list.php';
|
||||
} catch (GeneralException $e) {
|
||||
|
@ -36,7 +36,6 @@ class EventsListShortcut {
|
||||
|
||||
$dateFormat = SiteSettings::getDateFormat();
|
||||
$timeFormat = SiteSettings::getTimeFormat();
|
||||
$timeZone = SiteSettings::getTimeZone();
|
||||
|
||||
require dirname(__DIR__) . '/view/events-list.php';
|
||||
} catch (GeneralException $e) {
|
||||
|
@ -36,7 +36,6 @@ class EventsListWidget extends \WP_Widget {
|
||||
|
||||
$dateFormat = SiteSettings::getDateFormat();
|
||||
$timeFormat = SiteSettings::getTimeFormat();
|
||||
$timeZone = SiteSettings::getTimeZone();
|
||||
|
||||
require dirname(__DIR__) . '/view/events-list.php';
|
||||
} catch (GeneralException $e) {
|
||||
|
@ -3,16 +3,16 @@ namespace MobilizonConnector;
|
||||
|
||||
final class LineFormatter
|
||||
{
|
||||
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);
|
||||
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);
|
||||
|
||||
$dateText = $startDate . ' ' . $startTime;
|
||||
if ($end) {
|
||||
$endDateTime = new LocalDateTime($end, $timeZone);
|
||||
$endDate = LocalDateTimeFormatter::format($endDateTime, $dateFormat);
|
||||
$endTime = LocalDateTimeFormatter::format($endDateTime, $timeFormat);
|
||||
$endDateTime = new \DateTimeImmutable($end);
|
||||
$endDate = DateTimeFormatter::format($endDateTime, $dateFormat);
|
||||
$endTime = DateTimeFormatter::format($endDateTime, $timeFormat);
|
||||
|
||||
if ($startDate != $endDate) {
|
||||
$dateText .= ' - ';
|
||||
|
@ -1,15 +0,0 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
final class LocalDateTime {
|
||||
private $dateTime;
|
||||
|
||||
public function __construct(string $text, \DateTimeZone $timeZone) {
|
||||
$date = new \DateTimeImmutable($text);
|
||||
$this->dateTime = $date->setTimezone($timeZone);
|
||||
}
|
||||
|
||||
public function getValue() {
|
||||
return $this->dateTime;
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
final class LocalDateTimeFormatter
|
||||
{
|
||||
public static function format(LocalDateTime $dateTime, string $format) {
|
||||
$timestamp = $dateTime->getValue()->getTimestamp();
|
||||
return date_i18n($format, $timestamp);
|
||||
}
|
||||
}
|
@ -14,8 +14,4 @@ class SiteSettings {
|
||||
return get_option(self::$OPTION_NAME_TIME_FORMAT);
|
||||
}
|
||||
|
||||
public static function getTimeZone() {
|
||||
return wp_timezone();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ if (!defined('ABSPATH')) {
|
||||
<?php } ?>
|
||||
<a href="<?php echo esc_attr($event['url']); ?>"><?php echo esc_html($event['title']); ?></a>
|
||||
<br>
|
||||
<?php echo esc_html(LineFormatter::format_date_time($timeZone, $dateFormat, $timeFormat, $event['beginsOn'], $event['endsOn'])); ?>
|
||||
<?php echo esc_html(LineFormatter::format_date_time($dateFormat, $timeFormat, $event['beginsOn'], $event['endsOn'])); ?>
|
||||
<?php if (isset($event['physicalAddress'])) { ?>
|
||||
<br>
|
||||
<?php echo esc_html(LineFormatter::format_location($event['physicalAddress']['description'], $event['physicalAddress']['locality'])) ?>
|
||||
|
@ -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(new \DateTimeZone('UTC'), '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('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(new \DateTimeZone('UTC'), '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('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(new \DateTimeZone('UTC'), 'd/m/Y', 'H:i', '2021-04-15T10:30:00Z', null));
|
||||
$this->assertSame('15/04/2021 10:30', LineFormatter::format_date_time('d/m/Y', 'H:i', '2021-04-15T10:30:00Z', null));
|
||||
}
|
||||
|
||||
public function testCanLocationFormatBothParameters(): void {
|
||||
|
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use MobilizonConnector\LocalDateTime;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class LocalDateTimeTest extends TestCase {
|
||||
public function testCanGet24TimeForUsualTime(): void {
|
||||
$d = new LocalDateTime('2020-12-24T16:45:00Z', new DateTimeZone("utc"));
|
||||
$this->assertEquals(new DateTimeImmutable('2020-12-24T16:45:00Z'), $d->getValue());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user