configure am/pm time-format as user preference (#2789)
This commit is contained in:
33
src/Utils/DateFormatConverter.php
Normal file
33
src/Utils/DateFormatConverter.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
class DateFormatConverter
|
||||
{
|
||||
/**
|
||||
* This defines the mapping between PHP date format (key) and ICU date format (value).
|
||||
* https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $formatConvertRules = [
|
||||
// hours
|
||||
'h' => 'hh', 'H' => 'HH',
|
||||
// minutes
|
||||
'i' => 'mm',
|
||||
// am/pm to AM/PM
|
||||
'A' => 'a'
|
||||
];
|
||||
|
||||
public function convert(string $format): string
|
||||
{
|
||||
return strtr($format, self::$formatConvertRules);
|
||||
}
|
||||
}
|
||||
@@ -75,21 +75,12 @@ class LocaleFormats
|
||||
/**
|
||||
* Returns the format which is used by the form component to handle datetime values.
|
||||
*
|
||||
* @deprecated since 1.16
|
||||
* @return string
|
||||
*/
|
||||
public function getDateTimeTypeFormat(): string
|
||||
{
|
||||
return $this->formats->getDateTimeTypeFormat($this->getLocale());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the format which is used by the Javascript component to handle datetime values.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDateTimePickerFormat(): string
|
||||
{
|
||||
return $this->formats->getDateTimePickerFormat($this->getLocale());
|
||||
return $this->formats->getDateTypeFormat($this->getLocale()) . ' HH:mm';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,14 +122,4 @@ class LocaleFormats
|
||||
{
|
||||
return $this->formats->getDurationFormat($this->getLocale());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether this locale uses the 24 hour format.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isTwentyFourHours(): bool
|
||||
{
|
||||
return $this->formats->isTwentyFourHours($this->getLocale());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,10 @@ final class LocaleFormatter
|
||||
* @var string
|
||||
*/
|
||||
private $dateTimeFormat = null;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $dateTypeFormat = null;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
@@ -54,10 +58,6 @@ final class LocaleFormatter
|
||||
* @var string
|
||||
*/
|
||||
private $timeFormat = null;
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $isTwentyFourHour = null;
|
||||
|
||||
public function __construct(LanguageFormattings $formats, string $locale)
|
||||
{
|
||||
@@ -216,6 +216,15 @@ final class LocaleFormatter
|
||||
return $date->format($this->dateFormat);
|
||||
}
|
||||
|
||||
private function getDateTypeFormat(): string
|
||||
{
|
||||
if (null === $this->dateTypeFormat) {
|
||||
$this->dateTypeFormat = $this->localeFormats->getDateTypeFormat();
|
||||
}
|
||||
|
||||
return $this->dateTypeFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DateTime|string $date
|
||||
* @return string
|
||||
@@ -239,13 +248,15 @@ final class LocaleFormatter
|
||||
|
||||
/**
|
||||
* @param DateTime|string $date
|
||||
* @param string $timeFormat
|
||||
* @param bool $stripMidnight
|
||||
* @return bool|false|string
|
||||
*/
|
||||
public function dateTimeFull($date, bool $stripMidnight = false)
|
||||
public function dateTimeFull($date, string $timeFormat, bool $stripMidnight = false)
|
||||
{
|
||||
if (null === $this->dateTimeTypeFormat) {
|
||||
$this->dateTimeTypeFormat = $this->localeFormats->getDateTimeTypeFormat();
|
||||
$converter = new DateFormatConverter();
|
||||
$this->dateTimeTypeFormat = $this->getDateTypeFormat() . ' ' . $converter->convert($timeFormat);
|
||||
}
|
||||
|
||||
if (!$date instanceof DateTime) {
|
||||
@@ -298,7 +309,7 @@ final class LocaleFormatter
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function time($date)
|
||||
public function time($date, string $format = null)
|
||||
{
|
||||
if (null === $this->timeFormat) {
|
||||
$this->timeFormat = $this->localeFormats->getTimeFormat();
|
||||
@@ -308,7 +319,7 @@ final class LocaleFormatter
|
||||
$date = new DateTime($date);
|
||||
}
|
||||
|
||||
return $date->format($this->timeFormat);
|
||||
return $date->format($format ?? $this->timeFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -342,22 +353,4 @@ final class LocaleFormatter
|
||||
{
|
||||
return $this->formatIntl($dateTime, ($short ? 'EE' : 'EEEE'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $twentyFour
|
||||
* @param mixed $twelveHour
|
||||
* @return mixed
|
||||
*/
|
||||
public function hour24($twentyFour, $twelveHour)
|
||||
{
|
||||
if (null === $this->isTwentyFourHour) {
|
||||
$this->isTwentyFourHour = $this->localeFormats->isTwentyFourHours();
|
||||
}
|
||||
|
||||
if (true === $this->isTwentyFourHour) {
|
||||
return $twentyFour;
|
||||
}
|
||||
|
||||
return $twelveHour;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,8 +18,8 @@ class MomentFormatConverter
|
||||
{
|
||||
/**
|
||||
* This defines the mapping between PHP ICU date format (key) and moment.js date format (value)
|
||||
* For ICU formats see http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax
|
||||
* For Moment formats see http://momentjs.com/docs/#/displaying/format/.
|
||||
* For ICU formats see https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax
|
||||
* For Moment formats see http://momentjs.com/docs/#/displaying/format/
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
@@ -34,6 +34,8 @@ class MomentFormatConverter
|
||||
'ZZZZZ' => 'Z', 'ZZZ' => 'ZZ',
|
||||
// letter 'T'
|
||||
'\'T\'' => 'T',
|
||||
// am/pm to AM/PM
|
||||
'a' => 'A',
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user