kartik-v / yii2-datecontrol

Date control module allowing separation of date formats for View & Model for Yii Framework 2.0.
http://demos.krajee.com/datecontrol
Other
55 stars 29 forks source link

Escape \ #89

Closed SanyaBeer closed 7 years ago

SanyaBeer commented 8 years ago

I have some strange issue with russian locale.. I am use DateControl with default setting. Config settings:

'datecontrol' =>  [
            'class' => 'kartik\datecontrol\Module',
            'saveSettings' => [
                Module::FORMAT_DATE => 'php:U', // saves as unix timestamp
                Module::FORMAT_TIME => 'php:U',
                Module::FORMAT_DATETIME => 'php:U',
            ],
            'saveTimezone' => 'UTC',
            'autoWidget' => true,
        ]

And form usage:

echo $form->field($model, 'date_start')->widget(DateControl::classname(), [
            'type'=>DateControl::FORMAT_DATE,
        ]);

Date format get from Yii formatter, by default it was "medium". Function "convertDateIcuToPhp(.....)" in \yii\helpers\BaseFormatConverter: "IntlDateFormatter()" give to us this pattern "d MMM y 'г'." (in pattern 'г' - it is like Year on russian :-)), after this we have some manipulation and convertDateIcuToPhp return to us this pattern: "j M Y \г."; Now DateControl::displayFormat = "j M Y \г."; And after assets:

var datecontrol_c694ba13 = {
"idSave":"operation-date_start",
"url":"\/datecontrol\/parse\/convert",
"type":"date",
"saveFormat":"U",
"dispFormat":"j M Y \\г.",
"saveTimezone":"UTC",
.......
};

We have two "\". Value in field set in "1 Май 2016 \г."

I have problem because with this setting, yii2-datecontrol/controllers/ParseController::actionConvert() return empty value.

Enrica-r commented 7 years ago

Datecontrol wraps Kartik's 'datePicker' widget which wraps bootstrap-datepicker.

This bootstrap datepicker has a set of formats components which it understands. Kartik's datePicker widget converts most used PHP format patterns to the requested format. Unfortunately bootstrap-datepicker doesn't know about Russian year. see: https://bootstrap-datepicker.readthedocs.io/en/latest/options.html#format

Also PHP doesn't know about this. see http://php.net/manual/de/function.date.php. But PHP has a command to convert Gregorian calendar to Julian calendar 'gregoriantojd'.

INTL library with ICU format is the most complex formatter which exists. There is such a know how of date, time, number, currency etc. formats.

As I understand it's not only a formatting issue. There must be a calculation rule like conversion from Julian to Gregorian calendar or Japanese to Gregorian calendar etc. This is complex. Best solution could be to remove the "r".

kartik-v commented 7 years ago

Yes as @Enrica-r says. There unfortunately cannot be enhancement for complex locale scripts as that may bloat this library. Solution is to use simpler date formats with just numeric part of the date-time parts (rather than expecting text translations) as much as possible.

I am closing this currently based on above.