beberlei / assert

Thin assertion library for use in libraries and business-model
Other
2.41k stars 186 forks source link

date does not work with variable precision of microseconds #293

Open bendavies opened 4 years ago

bendavies commented 4 years ago

Hi,

Assertion::date('2019-10-31 14:08:46.429', 'Y-m-d H:i:s.u') will not work formatting the resulting date will not be the same as the input string: https://github.com/beberlei/assert/blob/99508be011753690fe108ded450f5caaae180cfa/lib/Assert/Assertion.php#L2396

2019-10-31 14:08:46.429 will be formatted as 2019-10-31 14:08:46.429000. These dates are equivalent.

A different method is needed to compare the dates.

A better check would be

$errors = \DateTime::getLastErrors();

if (0 < $errors['warning_count'] || 0 < $errors['error_count']) {
 //invalid
}
mvanduijker commented 1 year ago

Same issue with DATE_RFC3339 format.

If input is '2021-10-20T10:00:00Z' which is valid, but the output of $dateTime->format will be '2021-10-20T10:00:00+00:00' so the assertion fails unexpectedly.