Skip to content

Commit

Permalink
Add UI datetime timezone support
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed May 8, 2022
1 parent 7ec9f44 commit 70924cc
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/Persistence/Ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Ui extends Persistence
/** @var int Default decimal count for 'atk4_money' type. */
public $currency_decimals = 2;

/** @var string */
public $timezone;
/** @var string */
public $date_format = 'M d, Y';
/** @var string */
Expand All @@ -44,6 +46,13 @@ class Ui extends Persistence
/** @var string */
public $no = 'No';

public function __construct()
{
if ($this->timezone === null) {
$this->timezone = date_default_timezone_get();
}
}

public function typecastSaveField(Field $field, $value)
{
// relax empty checks for UI render for not yet set values
Expand Down Expand Up @@ -93,10 +102,9 @@ protected function _typecastSaveField(Field $field, $value): string
$formats = ['date' => $this->date_format, 'datetime' => $this->datetime_format, 'time' => $this->time_format];
$format = $field->persist_format ?: $formats[$field->type];

// datetime only - set to persisting timezone
if ($field->type === 'datetime') {
$value = new \DateTime($value->format('Y-m-d H:i:s.u'), $value->getTimezone());
$value->setTimezone(new \DateTimeZone($field->persist_timezone));
$value->setTimezone(new \DateTimeZone($this->timezone));
}
$value = $value->format($format);
}
Expand Down Expand Up @@ -125,32 +133,24 @@ protected function _typecastLoadField(Field $field, $value)
return null;
}

$dt_class = \DateTime::class;
$tz_class = \DateTimeZone::class;
$dtClass = \DateTime::class;
$tzClass = \DateTimeZone::class;

// ! symbol in date format is essential here to remove time part of DateTime - don't remove, this is not a bug
$formats = ['date' => '!+' . $this->date_format, 'datetime' => '!+' . $this->datetime_format, 'time' => '!+' . $this->time_format];
$format = $field->persist_format ?: $formats[$field->type];

// datetime only - set from persisting timezone
$valueStr = is_object($value) ? $this->_typecastSaveField($field, $value) : $value;
if ($field->type === 'datetime') {
$value = $dt_class::createFromFormat($format, $valueStr, new $tz_class($field->persist_timezone));
if ($value === false) {
throw (new Exception('Incorrectly formatted datetime'))
->addMoreInfo('format', $format)
->addMoreInfo('value', $valueStr)
->addMoreInfo('field', $field);
}
$value->setTimezone(new $tz_class(date_default_timezone_get()));
} else {
$value = $dt_class::createFromFormat($format, $valueStr);
if ($value === false) {
throw (new Exception('Incorrectly formatted date/time'))
->addMoreInfo('format', $format)
->addMoreInfo('value', $valueStr)
->addMoreInfo('field', $field);
}
$isDatetime = $field->type === 'datetime';
$value = $dtClass::createFromFormat($format, $valueStr, $isDatetime ? new $tzClass($this->timezone) : null);
if ($value === false) {
throw (new Exception('Incorrectly formatted datetime'))
->addMoreInfo('format', $format)
->addMoreInfo('value', $valueStr)
->addMoreInfo('field', $field);
}
if ($isDatetime) {
$value->setTimezone(new $tzClass(date_default_timezone_get()));
}

$value = parent::_typecastSaveField($field, $value);
Expand Down

0 comments on commit 70924cc

Please sign in to comment.