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

Nanoseconds date precision #68

Open
mrceperka opened this issue Jul 14, 2023 · 1 comment
Open

Nanoseconds date precision #68

mrceperka opened this issue Jul 14, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@mrceperka
Copy link
Contributor

Library version

v1.x-dev

Description

Go serializes time to 2023-07-14T13:52:32.489932695Z.
This includes nanoseconds. PHP is not able to handle nanoseconds and JsIsoFormat fails.

Steps to reproduce

This is probably general PHP problem.

$v = '2023-07-14T13:52:32.489932695Z';
$d = DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.v\Z', $v, new DateTimeZone('UTC'));
var_dump($d); // false

Actual Behavior

DateTimeValue will say it is not valid date

Expected Behavior

Validation should pass. We can strip those nanos, cos PHP does not support them either (AI said that :D)

Addition information

AI would solve it like this (this works with u instead of v tho)

$timestamp = '2023-07-14T15:43:41.453Z'; // Millisecond precision

// Separate the fractional part
list($whole, $fraction) = explode('.', $timestamp);

// Add trailing zeros or truncate to fit 6 digits
$fraction = substr(str_pad($fraction, 6, '0'), 0, 6);

// Combine everything
$timestampMicro = "$whole.$fraction" . "Z";

// Now parse it with DateTime::createFromFormat
$date = DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $timestampMicro);

echo $date->format('Y-m-d H:i:s.u');  // Outputs '2023-07-14 15:43:41.453000'
@mrceperka mrceperka added the bug Something isn't working label Jul 14, 2023
@mabar
Copy link
Member

mabar commented Jul 14, 2023

JS format should already work with default DateTimeValue format. Go format seems to be just a bit more precise? It may work already.

But we should definitely add some tests for created datetime to check how accurate is the result

$datetime = $stringValue !== ''
&& substr($stringValue, -1) === 'Z'
? $classType::createFromFormat(self::JsIsoFormat, $stringValue, new DateTimeZone('UTC'))

yield ['2013-04-12T16:40:00.000Z', DateTimeRule::FormatIsoCompat];

https://github.com/orisai/object-mapper/tree/e974721d8c8d6008ae811c107bd8e7db942fe2fa/docs#datetime-rule

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants