Skip to content
Snippets Groups Projects
Commit 6cd7527e authored by Matthieu Scarset's avatar Matthieu Scarset
Browse files

feat: Cleaner timezone code

parent 62b5a049
No related branches found
Tags 2.1.4
No related merge requests found
......@@ -176,6 +176,48 @@ abstract class CalendarViewBase extends DefaultStyle implements CalendarViewInte
return $date_fields;
}
/**
* Determine timezone relative to a given date field or to the current user.
*
* @param \Drupal\views\Plugin\views\field\EntityField $field
* (optional) A given date field.
*
* @return string The timezone, as a string.
*/
public function getTimezone(EntityField $field = NULL) {
$timezone = $this->dateConfig->get('timezone')['default'];
// Get user's timezone, if enabled.
if ($this->dateConfig->get('timezone.user.configurable')) {
$timezone = $this->currentUser->getTimeZone() ?: $timezone;
}
// Get field overridden timezone.
if ($field && isset($field->options['settings']['timezone_override'])) {
$timezone = $field->options['settings']['timezone_override'] ?: $timezone;
}
return $timezone;
}
/**
* Calculate time offset between two timezones.
*
* @param string $time
* A date/time string compatible with \DateTime. It is used as the
* reference for computing the offset, which can vary based on the time
* zone rules.
* @param string $timezone
* The time zone that $time is in.
*
* @return int
* The computed offset in seconds.
*
* @see \Drupal\datetime\Plugin\views\filter\Date::getOffset()
*/
public function getTimezoneOffset(string $time, string $timezone) {
$tz = new \DateTimeZone($timezone);
return $tz->getOffset(new \DateTime($time, $tz));
}
/**
* {@inheritDoc}
*/
......@@ -420,41 +462,6 @@ abstract class CalendarViewBase extends DefaultStyle implements CalendarViewInte
return parent::render();
}
/**
* Determine what timezone to use before rendering.
*
* @param \Drupal\views\Plugin\views\field\EntityField $field
* (optional) A given date field.
* @param \Drupal\Core\Session\AccountInterface $account
* (optional) A given user session.
*
* @return array
* The timezone and the offset values.
*/
public function getTimezoneData(EntityField $field = NULL, AccountInterface $account = NULL) {
$account = $account ?? $this->currentUser;
$timezone = 'UTC';
if (!empty($field->options['settings']['timezone_override'])) {
$timezone = $field->options['settings']['timezone_override'];
}
elseif (($user_timezone = $account->getTimezone()) && !empty($user_timezone)) {
$timezone = $user_timezone;
}
elseif (($system_timezone = $this->dateConfig->get('timezone')['default']) && !empty($system_timezone)) {
$timezone = $system_timezone;
}
$tz = new \DateTimeZone($timezone);
$time_now = new \DateTime('now', $tz);
$offset = $tz->getOffset($time_now);
return [
'timezone' => $timezone,
'offset' => $offset,
];
}
/**
* Get the value out of a view Result for a given date field.
*
......@@ -489,18 +496,22 @@ abstract class CalendarViewBase extends DefaultStyle implements CalendarViewInte
$values['end_value'] = ($this->ensureTimestampValue($values['end_value'] ?? $values['value']));
// Get offset to fix start/end datetime values.
$timezone = $this->getTimezoneData($field);
$values['value'] += $timezone['offset'] ?? 0;
$values['end_value'] += $timezone['offset'] ?? 0;
$timezone = $this->getTimezone($field);
$offset = $this->getTimezoneOffset('now', $timezone);
$values['value'] += $offset;
$values['end_value'] += $offset;
// Get first item value to reorder multiday events in cells.
$all_values = $field->getValue($row);
$all_values = \is_array($all_values) ? $all_values : [$all_values];
$first_value = reset($all_values);
// Transform ISO8601 to timestamp.
if (!ctype_digit($first_value)) {
$first_instance_date = new DateTimePlus($first_value);
$first_value = $first_instance_date->getTimestamp();
}
$values['first_instance'] = (int) $first_value;
// Expose the date field if other modules need it in preprocess.
......@@ -518,7 +529,6 @@ abstract class CalendarViewBase extends DefaultStyle implements CalendarViewInte
$start = $values['value'];
$end = $values['end_value'] ?? $start;
$title_string = $start && ($start !== $end) ? '@title from @start to @end (@timezone)' : '@field: @date (@timezone)';
$tz = $timezone['timezone'] ?? 'UTC';
$values['title'] = $this->t($title_string, [
'@title' => $entity->label(),
......@@ -526,7 +536,7 @@ abstract class CalendarViewBase extends DefaultStyle implements CalendarViewInte
'@date' => $this->dateFormatter->format($start, 'long'),
'@start' => $this->dateFormatter->format($start, 'short'),
'@end' => $this->dateFormatter->format($end, 'short'),
'@timezone' => $tz,
'@timezone' => $timezone,
]);
return $values;
......@@ -551,9 +561,8 @@ abstract class CalendarViewBase extends DefaultStyle implements CalendarViewInte
return;
}
$timezone = $this->getTimezoneData();
/** @var \Drupal\Core\Datetime\DrupalDateTime $now */
$now = new DrupalDateTime('', $timezone['timezone'] ?? NULL);
$now = new DrupalDateTime('', $this->getTimezone());
$start_day = clone $now;
$start_day->setTimestamp($start);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment