Problem/Motivation

Creating a DrupalDateTime using the method createFromFormat I noticed that I get an exception "The created date does not match the input value" whenever i use a reset character (!|) as defined in php DateTime and referenced in the method's documentation.
From what i can see it makes the "reset" options completely unusable, without any acceptable workaround.

To reproduce this is very simple:

$format = '!dmy';
$date_str = '011195';
$date = \DateTime::createFromFormat($format, $date_str); // works
$date = \Drupal\Core\Datetime\DrupalDateTime::createFromFormat($format, $date_str);  // fails
$date = DateTimePlus::createFromFormat($format, $date_str, 'UTC'); // also fails

I did some more tests here:

$date_str = '01.11.95 09:12:00';

// Failing
$format = '!d.m.y H:i';
$date = DateTimePlus::createFromFormat($format, $date_str); 
// Failing
$format = '!d.m.y H:i:s';
$date = DateTimePlus::createFromFormat($format, $date_str); 

// Working
$format = 'd.m.y H:i:s';
$date = DateTimePlus::createFromFormat($format, $date_str); 

Proposed resolution

TBD

Remaining tasks

TBD

User interface changes

None

API changes

None

Data model changes

None

Comments

esolitos created an issue. See original summary.

esolitos’s picture

Issue summary: View changes

code styling

esolitos’s picture

Priority: Major » Normal
Status: Active » Closed (works as designed)

I see some strage things in DateTimePlus::createFromFormat().
For example on line 230 there's a $date instanceof DateTimePlus, however right before there's an opposite check, so this will always be false.

Anyhow, I believe the bug is easy to spot: me being tired and not really reading carefully the documentation.
Hopefully this issue will be useful for someone else doing my same mistake: as stated in the $setting parameter reference the function $date->format($format) doesn't consider any of the "special characters" (ref.) so any input format that contains one of those characters will not validate, hence the documented $setting 'validate_format'=>FALSE will do the trick.