Skip to content

Use setLenient(false) when parsing localized date(time) #14519

@CauanCabral

Description

@CauanCabral

This is a (multiple allowed):

  • bug

  • enhancement

  • feature-discussion (RFC)

  • CakePHP Version: 4.0.

  • Platform and Target: All.

What you did

At I18n\DateFormatTrait we have methods to parse strings of date and datetime into objects.

What happened

When passing invalid dates for some locales, the parser silent fail and return a non-predictive date object.

What you expected to happen

Parsing should fail.

I think it should expect string localized representing valid dates/datetimes.

Propose

Disable, or allow us to disable, the lenient mode from IntlDateFormatter, as pointed in PHP documentation.

Code sample:

// If you don't want to setup pt_BR locale, use a string with '25-12-2000' (25 of december)
// It will be parsed as 2002-01-12
Cake\I18n\I18n::setLocale('pt_BR');

/**
 * Day: 04
 * Month: 21   <-- This is invalid
 * Year: 1980
 */
$invalid = '04/21/1980';
$result = Cake\I18n\Date::parseDate($invalid);

// Expected: null|false
// Result: Cake\I18n\Date Object
// (
//     [time] => 1981-09-04 00:00:00.000000+00:00
//     [timezone] => UTC
//     [fixedNowTime] => 2020-04-29T13:50:04.505974+00:00
// )
debug($result);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions