Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Yaml] Support parsing YAML timestamps as DateTime #14420

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

Closed
wants to merge 12 commits into from
Closed
Prev Previous commit
[Yaml] Fix usage of $timestampAsDateTime
  • Loading branch information
remi-blaise committed May 6, 2015
commit 422d8380da82b0434193c555d87f1c35c0978848
13 changes: 7 additions & 6 deletions 13 src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public static function parse($value, $exceptionOnInvalidType = false, $objectSup
*/
public static function dump($value, $exceptionOnInvalidType = false, $objectSupport = false, $timestampAsDateTime = false)
{
self::$timestampAsDateTime = $timestampAsDateTime;

switch (true) {
case is_resource($value):
if ($exceptionOnInvalidType) {
Expand All @@ -108,7 +110,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp

return 'null';
case is_object($value):
if ($value instanceof \DateTime || $value instanceof \DateTimeImmutable) {
if (self::$timestampAsDateTime && ($value instanceof \DateTime || $value instanceof \DateTimeImmutable)) {
if ($value->getTimezone()->getName() === date_default_timezone_get()) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about this. The code parsing the date might have a different default timezone. It is better to always include all info IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, if the TimeZone has been intentionally defined as for exemple +1 (in the YAML file parsed or in the PHP datas), it dumps it (because the server server timestamp is for example Europe/Paris and not +1).
If it was not defined manually, it has the default TimeZone and so the Yaml component doesn't dump the timestamp (letting it with default value in some ways).
I think personally it's the best behaviour.

Copy link
Member

Choose a reason for hiding this comment

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

What if it was explicitly defined as UTC and the server default timezone is also UTC ? You have no way to know whether the UTC timezone was explicitly chosen or no. But even worse, you have no idea how this will be parsed. So IMO, you should not drop information when dumping, assuming that the server parsing your YAML will have the same config

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok.

if ('000000' === $value->format('His')) {
return $value->format('Y-m-d');
Expand All @@ -130,7 +132,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp

return 'null';
case is_array($value):
return self::dumpArray($value, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime);
return self::dumpArray($value, $exceptionOnInvalidType, $objectSupport);
case null === $value:
return 'null';
case true === $value:
Expand Down Expand Up @@ -179,11 +181,10 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
* @param array $value The PHP array to dump
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
* @param bool $objectSupport true if object support is enabled, false otherwise
* @param bool $timestampAsDateTime true if DateTime objects must be dumped as YAML timestamps, false if DateTime objects are not supported
*
* @return string The YAML string representing the PHP array
*/
private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime)
private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport)
{
// array
$keys = array_keys($value);
Expand All @@ -193,7 +194,7 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor
) {
$output = array();
foreach ($value as $val) {
$output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime);
$output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport);
}

return sprintf('[%s]', implode(', ', $output));
Expand All @@ -202,7 +203,7 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor
// mapping
$output = array();
foreach ($value as $key => $val) {
$output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime), self::dump($val, $exceptionOnInvalidType, $objectSupport, $timestampAsDateTime));
$output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport), self::dump($val, $exceptionOnInvalidType, $objectSupport));
}

return sprintf('{ %s }', implode(', ', $output));
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.