-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Yaml] Allow dumping DateTimeInterface objects with milliseconds or microseconds #48920
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
Comments
I think configuring datetime dump format could be great, but it is maybe not easy with bitmask implementation 🤔 |
It wouldn't need any settings:
If the 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:
|
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 For instance when parsing |
I suggest parsing it as |
…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
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 havingInline
use'Y-m-d\TH:i:s.vO'
and'Y-m-d\TH:i:s.uO'
instead when theDateTimeInterface
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 theDateTimeInterface
is.Example
Currently
In:
Out:
Suggested
In:
Out:
The text was updated successfully, but these errors were encountered: