[php][bug] Fixes exceptions with API's with too high date-time nanosecond precision (#7943)

* [php] Fixes problems with API's with too high date-time nanosecond precision

* update samples

Co-authored-by: William Cheng <wing328hk@gmail.com>
This commit is contained in:
NickUfer 2020-11-23 15:30:57 +01:00 committed by GitHub
parent 33f4827a06
commit 6f6822a1b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View File

@ -306,7 +306,15 @@ class ObjectSerializer
// be interpreted as a missing field/value. Let's handle
// this graceful.
if (!empty($data)) {
try {
return new \DateTime($data);
} catch (\Exception $exception) {
// Some API's return a date-time with too high nanosecond
// precision for php's DateTime to handle. This conversion
// (string -> unix timestamp -> DateTime) is a workaround
// for the problem.
return (new \DateTime())->setTimestamp(strtotime($data));
}
} else {
return null;
}

View File

@ -316,7 +316,15 @@ class ObjectSerializer
// be interpreted as a missing field/value. Let's handle
// this graceful.
if (!empty($data)) {
try {
return new \DateTime($data);
} catch (\Exception $exception) {
// Some API's return a date-time with too high nanosecond
// precision for php's DateTime to handle. This conversion
// (string -> unix timestamp -> DateTime) is a workaround
// for the problem.
return (new \DateTime())->setTimestamp(strtotime($data));
}
} else {
return null;
}