Skip to content

Error date time to save #11306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

raumatbel
Copy link
Contributor

@raumatbel raumatbel commented Oct 9, 2017

Description

Save date time correctly in different timezone and locale

Problem

The function$formatter->parse($date)always return false if the date doesn't contains time and the function new \DateTime($date) return the error "Invalid input datetime format of value".

Solution

Before of the function$formatter->parse($date) it checks if the string contains time or not. If it doesn't contains, it concat the value 00:00 to the string "date".

Fixed Issues (if relevant)

  1. Set product as new "from" and "to" not being interpreted correctly #10580: Set product as new "from" and "to" not being interpreted correctly
  2. Error: Invalid input datetime format of value '25/07/+00201717' #10485: Error: Invalid input datetime format of value '25/07/+00201717

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

\IntlDateFormatter::SHORT,
];
protected $_allowedFormats
= [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this formatting back to normal & consistent with other code.

if (\Zend_Locale::isLocale($locale)) {
$localeDate = $locale;
}
$date = $formatter->parse($date) ?: (new \Zend_Date(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zend_Locale and Zend_Date must not be used, there was a separate refactoring effort to get rid of them and some static tests are checking they are not used.

There should be some intl or PHP library equivalents.

Copy link
Contributor Author

@raumatbel raumatbel Oct 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orlangur I have used this because the format date is in Zend

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orlangur What can you recommend me to continue with this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only native intl classes, like \DateTime, \Locale etc.

@@ -3,38 +3,47 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like some new lines are automatically added by your IDE, please avoid this.

@@ -218,6 +238,7 @@ public function scopeTimeStamp($scope = null)
@date_default_timezone_set($timezone);
$date = date('Y-m-d H:i:s');
@date_default_timezone_set($currentTimezone);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like some new lines are automatically added by your IDE, please avoid this.

@magento-engcom-team magento-engcom-team changed the base branch from develop to 2.3-develop October 20, 2017 15:32
@okorshenko okorshenko modified the milestones: October 2017, November 2017 Nov 1, 2017
@magento-engcom-team magento-engcom-team added bugfix Fixed in 2.3.x The issue has been fixed in 2.3 release line labels Nov 7, 2017
@okorshenko
Copy link
Contributor

Hi @raumatbel
Could you please fix broken tests so that we can proceed with PR acceptance?
Thank you

@raumatbel
Copy link
Contributor Author

Hi @okorshenko, I have been very busy but in this week I'm going to try solve the problem with the tests

@gwharton
Copy link
Contributor

gwharton commented Nov 8, 2017

Hi, Once approved, can these changes be merged into 2.1 and 2.2 aswell?

@raumatbel
Copy link
Contributor Author

That is the idea. Firstly I want solve this problem in the main branch and after I do the backports.

@gwharton
Copy link
Contributor

gwharton commented Nov 8, 2017

Hi there @raumatbel

I'm new to github so don't know the process. Thanks for letting me know. I've just seen so many issues reported under 2.1/2.2, fixed in 2.3-develop and then get forgotten. I didnt know if stuff gets automatically backported, or whether a new PR has to be created etc... Anyway, I see you have it in hand.

Thanks

@raumatbel
Copy link
Contributor Author

Welcome @gwharton.
Normally if the issue is reported in the 3 branches, you have to do 3 PR, one for every branch for that the magento team can merged directly.

$date = $formatter->parse($date) ?: (new \DateTime($date))->getTimestamp();

$localeDate = !empty($locale) ? $locale : null;
$date = $formatter->parse($date) ?: (new \Zend_Date(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not solved: #11306 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to solve this problem, but it's not easy and it will take me a while fix it.

@@ -150,6 +158,7 @@ public function getDateTimeFormat($type)
}

/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor bad smelling code instead of suppressing warning whenever possible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This annotation needs to be removed and corresponding code refactored (unlike the PHPMD.CouplingBetweenObjects it should be pretty easy).

@gwharton
Copy link
Contributor

I don't know if it is related, but there is another timezone/time/formatting bug in #10663

Copy link
Contributor

@orlangur orlangur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for improving implementation using only native PHP classes!

After changes are applied, please force push as a single commit.

@@ -150,6 +158,7 @@ public function getDateTimeFormat($type)
}

/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This annotation needs to be removed and corresponding code refactored (unlike the PHPMD.CouplingBetweenObjects it should be pretty easy).

@@ -174,7 +183,13 @@ public function date($date = null, $locale = null, $useTimezone = true, $include
$timeType,
new \DateTimeZone($timezone)
);

//Know if the date contains time or not
if (!preg_match('/(.*).(\d{2}):(\d{2})/', $date)) {
Copy link
Contributor

@orlangur orlangur Nov 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at logic of this method I believe it should be rewritten into

if ($includeTime && !preg_match('\d{2}:\d{2}', $date))

(regexp note: no need in brackets, first part seems to be useless)
(comment could be removed with such condition)

@orlangur
Copy link
Contributor

@raumatbel would be nice to rebase against latest 2.3-develop and check if #10754 (comment) is true (we can do it on our side during testing as well).


//Know if the date contains time or not
if (!preg_match('/(.*).(\d{2}):(\d{2})/', $date)) {
$date .= " 00:00:00";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With $timeType set to \IntlDateFormatter::SHORT
"00:00:00" should be changed to "0:00am" to be parsed

http://php.net/manual/en/class.intldateformatter.php

@raumatbel raumatbel force-pushed the FR#ErrorDateTimeDevelop branch from 0d3b92e to c77a9de Compare November 29, 2017 19:53
@magento-engcom-team magento-engcom-team added the Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release label Dec 12, 2017
@okorshenko okorshenko removed the 2.3.x label Dec 14, 2017
@orlangur
Copy link
Contributor

Hi @raumatbel, according to QA report #10485 is still reproduced after these changes. Could you please check if it works for you?

@raumatbel
Copy link
Contributor Author

Hi @orlangur I have checked the issue again. Without my changes the product I can not save it due to the problem of the dates, but with my changes it works fine. Do you can check again? If it doesn't work, can you indicate the steps to reproduce?

@okorshenko okorshenko modified the milestones: December 2017, January 2018 Jan 8, 2018
@magento-engcom-team
Copy link
Contributor

@raumatbel

Steps to reproduce:
Login to Magento admin
Change admin locale to the United Kingdom.
Try to create new product
Put "Set product as new from" on creating product page.
-Try to save the product.

Expected result:
Product is saved successfully as no changes are made from the previous state of the product

Actual result
An error Invalid input DateTime format of value '25/07/+00201717' appears and the product is not saved

@okorshenko
Copy link
Contributor

Hi @raumatbel
we are closing this PR due to inactivity. If you would like to proceed with this PR, feel free reopen it. Thank you

@okorshenko okorshenko closed this Jan 26, 2018
@raumatbel
Copy link
Contributor Author

raumatbel commented Jan 29, 2018

Hi @okorshenko , I apologize for the delay in answering but I'm very busy this weeks. This weekend I'm going to review again. I reopen PR

@raumatbel raumatbel reopened this Jan 29, 2018
@raumatbel
Copy link
Contributor Author

raumatbel commented Feb 3, 2018

@okorshenko @orlangur I have been reviewing and I have checked that with this PR this error is fixed. I attach the screenshots in the branch 2.3-develop and my branch with my solution.

Settings account
account

Product in branch 2.3-develop
branch 2 3 develop - product

Product with PR
fr errordatetimedevelop - product

@magento-engcom-team
Copy link
Contributor

@raumatbel thank you for contributing. Please accept Community Contributors team invitation here to gain extended permissions for this repository.

@pablobae
Copy link

pablobae commented Feb 8, 2018

This fix works! Good job, @raumatbel

@raumatbel
Copy link
Contributor Author

@magento-engcom-team I had already permissions in this repository as "member".

@SamB-GB
Copy link

SamB-GB commented Mar 30, 2018

This problem is still present in 2.2.3

@rmakara
Copy link

rmakara commented Apr 18, 2018

This problem appears in 2.1.9. Could somebody backport it? :) I would really appreciate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Award: complex bugfix Fixed in 2.3.x The issue has been fixed in 2.3 release line Progress: needs update Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet
Development

Successfully merging this pull request may close these issues.