Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer timezone in DateTime field handler #126

Merged
merged 2 commits into from
Mar 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Drupal/Driver/Fields/Drupal8/DatetimeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Drupal\Driver\Fields\Drupal8;

use DateTime;
use DateTimeZone;

/**
* Datetime field handler for Drupal 8.
*/
Expand All @@ -11,14 +14,22 @@ class DatetimeHandler extends AbstractHandler {
* {@inheritdoc}
*/
public function expand($values) {
$siteTimezone = new DateTimeZone(\Drupal::config('system.date')->get('timezone.default'));
$storageTimezone = new DateTimeZone(DATETIME_STORAGE_TIMEZONE);
foreach ($values as $key => $value) {
if (strpos($value, "relative:") !== FALSE) {
$relative = trim(str_replace('relative:', '', $value));
// Get time, convert to ISO 8601 date in GMT/UTC, remove TZ offset.
$values[$key] = substr(gmdate('c', strtotime($relative)), 0, 19);
}
else {
$values[$key] = str_replace(' ', 'T', $value);
// A Drupal install has a default site timezone, but nonetheless
// uses UTC for internal storage. If no timezone is specified in a date
// field value by the step author, assume the default timezone of
// the Drupal install, and therefore transform it into UTC for storage.
$date = new DateTime($value, $siteTimezone);
$date->setTimezone($storageTimezone);
$values[$key] = $date->format('Y-m-d\TH:i:s');
}
}
return $values;
Expand Down