field date is not ISO 8601, it stores only UTC , and w/o timezone info.

ok.. i "want" now to save with UTC too (I'm using decoupled aproach, no drupal frontend), I will have same error because

$c = new DrupalDateTime($data[$mappedField], 'UTC');
$utc = $c->format('c');  // or $utc = $c->format(DATE_ISO8601, ['timezone' => 'UTC']);
$node->$field = $utc;
$node->save();

is

2017-10-10T07:10:10+00:00    or    2017-10-10T07:10:10+0000

trying to save proper ISO 8601 thows error

SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column ...

because column is limited to 20 chars. WHY?! I don't even talk now about idea of saving date as UTC

So, i must use this
$utc = $c->format('Y-m-d\TH:i:s');

I hope I get it right

regards

Comments

Anonymous’s picture

yurii_2016 created an issue. See original summary.

Anonymous’s picture

Issue summary: View changes
Anonymous’s picture

Issue summary: View changes
Anonymous’s picture

Issue summary: View changes
Anonymous’s picture

p.s.

and that's how i output date now

'start' => DrupalDateTime::createFromFormat(DATE_ISO8601, 
          $node->$field->value.'+0000')->format('Y-m-d H:i:s',
              ['timezone' => drupal_get_user_timezone()]),

amazing, is this the correct way? I hope so

mpdonadio’s picture

There are constants to use for that:

$c = new DrupalDateTime($data[$mappedField], DATETIME_STORAGE_TIMEZONE);
$utc = $c->format(DATETIME_DATETIME_STORAGE_FORMAT);
$node->$field = $utc;
$node->save();

That should work, though in general, the ::create methods are preferred over calling the DrupalDateTime constructor directly.

Postponing this to see if this solves the problem; reopen if it doesn't. This is also potentially related to (or duplicates of)

#2716891: DateTimeIso8601::setDateTime() vs. DateTimeItem::schema(): (at least) one of them is broken

#2768651: Let TimestampItem (de)normalize to/from RFC3339 timestamps, not UNIX timestamps, for better DX

I do agree, though, that this situation isn't ideal. Even w/o changing storage, I think this should be more accommodating of timezones and formats, and handle the conversion to the storage format internally.

Anonymous’s picture

after digging date field i'm outputing date this way for now

$node->$field->date->format('Y-m-d H:i:s', ['timezone' => drupal_get_user_timezone()])

which looks sufficient.

Problem is with misleading description of date field module. Because it is not ISO 8601 ( https://en.wikipedia.org/wiki/ISO_8601 ), you don't store timezone, like 'Z' etc

Anonymous’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)