Skip to content

Support parsing YAML timestamps as DateTime #11838

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
stof opened this issue Sep 3, 2014 · 10 comments
Closed

Support parsing YAML timestamps as DateTime #11838

stof opened this issue Sep 3, 2014 · 10 comments

Comments

@stof
Copy link
Member

stof commented Sep 3, 2014

Currently, YAML timestamps are pased into integers representing a Unix timestamp:

case preg_match(self::getTimestampRegex(), $scalar):

however, this is not very handy:

  • it does not allow knowing that a YAML timestamp was provided (as it parses the same than an integer)
  • dumping again the parsed data would not generate the same data (it will dump an integer, not a timestamp).

Dumping a YAML timestamp is actually impossible currently (if you format it as a YAML timestamp manually in a PHP string, it will be dumped as a string because the dumper knows that the string needs to be escaped).

So here is my proposal:

  • make the dumper accept DateTimeInterface objects and dumping them as Yaml timestamps (they are currently dumped as PHP objects like other objects, or more likely throwing an exception as the object dumping is forbidden by default)
  • add an option in the parser to parse timestamps as DateTime objects rather than integers. this option would be disabled by default for BC reasons, but I suggest that 3.0 could change it to become enabled by default.

What do you think about it ?

@stof stof added Feature and removed Feature labels Sep 3, 2014
@xabbuh
Copy link
Member

xabbuh commented Sep 4, 2014

@stof This is basically the same as #8580, isn't it?

@stof
Copy link
Member Author

stof commented Sep 4, 2014

Yeah, it is exactly the same (with more detauls in the message). I was quite sure I already suggested that. I don't know why I haven't seen it again when looking for it yesterday

@alexpott
Copy link
Contributor

Why not dump something like !!php/datetime and use this whilst parsing? Then you get no BC break.

@fabpot
Copy link
Member

fabpot commented Sep 24, 2014

@alexpott Great idea!

@stof
Copy link
Member Author

stof commented Sep 24, 2014

@alexpott my goal is being able to parse standard YAML files in a way which is useful in PHP, and re-dumped in a compatible way. Currently, parsing and redumping a file containing a YAML timestamp breaks it by dumping a number.

@lemonlab
Copy link
Contributor

@Pitoune, @mehdichaouch and me will try to work on this issue.

@stof
Copy link
Member Author

stof commented Jan 3, 2015

@francoismartin have you done any progress on this point ?

@remi-blaise
Copy link
Contributor

I will try to submit a patch.

@remi-blaise
Copy link
Contributor

Do I depreciate using the value false for the option ?

@xabbuh
Copy link
Member

xabbuh commented Feb 17, 2016

see #17836

fabpot added a commit that referenced this issue Feb 18, 2016
…buh)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[Yaml] support to parse and dump DateTime objects

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #6275, #8580, #11838, #14420
| License       | MIT
| Doc PR        | TODO

Commits
-------

7e1c6c4 [Yaml] support to parse and dump DateTime objects
@fabpot fabpot closed this as completed Feb 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants