Skip to content
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

[Yaml] Allow dumping DateTimeInterface objects with milliseconds or microseconds #48920

Closed
dustinwilson opened this issue Jan 8, 2023 · 4 comments

Comments

@dustinwilson
Copy link
Contributor

dustinwilson commented Jan 8, 2023

Description

At present the Yaml component will parse date formats such as 2023-01-08T16:18:25.213Z but it can't dump them, instead only using the 'c' format which doesn't contain milliseconds. I would like to suggest having Inline use 'Y-m-d\TH:i:s.vO' and 'Y-m-d\TH:i:s.uO' instead when the DateTimeInterface object being dumped contains milliseconds or microseconds respectively. It wouldn't require any additional flags but instead have it intelligently choose which format to output based on how precise the DateTimeInterface is.

Example

Currently

In:

date: 2023-01-08T16:18:25Z
date_with_milliseconds: 2023-01-08T16:18:25.213Z
date_with_microseconds: 2023-01-08T16:18:25.718800Z

Out:

date: 2023-01-08T16:18:25+00:00
date_with_milliseconds: 2023-01-08T16:18:25+00:00
date_with_microseconds: 2023-01-08T16:18:25+00:00

Suggested

In:

date: 2023-01-08T16:18:25Z
date_with_milliseconds: 2023-01-08T16:18:25.213Z
date_with_microseconds: 2023-01-08T16:18:25.718800Z

Out:

date: 2023-01-08T16:18:25+00:00
date_with_milliseconds: 2023-01-08T16:18:25.213+00:00
date_with_microseconds: 2023-01-08T16:18:25.718800+00:00
@carsonbot carsonbot added the Yaml label Jan 8, 2023
@xabbuh xabbuh added the Feature label Jan 8, 2023
@alamirault
Copy link
Contributor

I think configuring datetime dump format could be great, but it is maybe not easy with bitmask implementation 🤔

@dustinwilson
Copy link
Contributor Author

dustinwilson commented Jan 10, 2023

It wouldn't need any settings:

I would like to suggest having Inline use 'Y-m-d\TH:i:s.vO' and 'Y-m-d\TH:i:s.uO' instead when the DateTimeInterface object being dumped contains milliseconds or microseconds respectively. It wouldn't require any additional flags but instead have it intelligently choose which format to output based on how precise the DateTimeInterface is.

If the DateTimeInterface has millisecond precision it would output milliseconds in the yaml file. If it has microseconds it would output microsecond precision. The way it is currently there is potential data loss when writing to a yaml file.

There's probably a better way to do this, but this was the first idea that came to mind right now:

function getFormat($date) {
    $test = rtrim($date->format('u'), '0');
    if ($test === '') {
        return 'c';
    } elseif (strlen($test) < 4) {
        return 'Y-m-d\TH:i:s.vP';
    }
    return 'Y-m-d\TH:i:s.uP';
}

echo getFormat(new \DateTime('2023-01-09T01:02:03Z')) . "\n";
echo getFormat(new \DateTime('2023-01-09T01:02:03.4Z')) . "\n";
echo getFormat(new \DateTime('2023-01-09T01:02:03.456Z')) . "\n";
echo getFormat(new \DateTime('2023-01-09T01:02:03.4567Z')) . "\n";
echo getFormat(new \DateTime('2023-01-09T01:02:03.456789Z')) . "\n";

Result:

c
Y-m-d\TH:i:s.vP
Y-m-d\TH:i:s.vP
Y-m-d\TH:i:s.uP
Y-m-d\TH:i:s.uP

@dustinwilson
Copy link
Contributor Author

I started work on a pull request for this and had everything just about ready to go. But, I realized that something would also need to be done about parsing dates when not using Yaml::PARSE_DATETIME. This part would be a breaking change, though. It'd either change the type of what is output from the parser or have it need to be a much bigger number.

For instance when parsing 2023-01-09T01:02:03.4567Z it would need to be parsed as either 1673226123.4567 or 1673226123456700.

@stof
Copy link
Member

stof commented Jan 30, 2023

I suggest parsing it as 1673226123.4567. Parsing it as 1673226123456700 would be totally unusable as the "unit" of the timestamp would be different for different decimals.

fabpot pushed a commit to dustinwilson/symfony that referenced this issue Feb 4, 2023
fabpot added a commit that referenced this issue Feb 4, 2023
…nds in dates (dustinwilson)

This PR was squashed before being merged into the 6.3 branch.

Discussion
----------

[Yaml] Feature #48920  Allow milliseconds and microseconds in dates

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Feature #48920
| License       | MIT
| Doc PR        | forthcoming

Allows Yaml to parse dates with milliseconds or microseconds and to dump them as well.

Commits
-------

8afb4c7 [Yaml] Feature #48920  Allow milliseconds and microseconds in dates
@fabpot fabpot closed this as completed Feb 4, 2023
bitgandtter pushed a commit to timgchile/symfony that referenced this issue Feb 15, 2023
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