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

Carbon loses microsecond information #1073

Closed
t-tomek opened this issue Jan 17, 2018 · 8 comments
Closed

Carbon loses microsecond information #1073

t-tomek opened this issue Jan 17, 2018 · 8 comments
Labels

Comments

@t-tomek
Copy link

t-tomek commented Jan 17, 2018

I've found an issue with microseconds (Carbon v1.22.1, PHP v7.0.22):

$date = DateTime::createFromFormat('Y-m-d\TH:i:s.uT', '2017-07-29T13:57:27.123456Z');
echo($date->format('Y-m-d H:i:s.u e')); // 2017-07-29 13:57:27.123456 Z

$date = Carbon::createFromFormat('Y-m-d\TH:i:s.uT', '2017-07-29T13:57:27.123456Z');
echo($date->format('Y-m-d H:i:s.u e')); // 2017-07-29 13:57:27.000000 Z
Glavic added a commit to Glavic/Carbon that referenced this issue Feb 5, 2018
@Glavic
Copy link
Collaborator

Glavic commented Feb 5, 2018

On PHP 7.0.22 (Windows x64) and Carbon 1.22.1 I get the identical result for both cases.

public function testIssue1073()
{
	$date = \DateTime::createFromFormat('Y-m-d\TH:i:s.uT', '2017-07-29T13:57:27.123456Z');
	$d1 = $date->format('Y-m-d H:i:s.u e');
	
	$date = Carbon::createFromFormat('Y-m-d\TH:i:s.uT', '2017-07-29T13:57:27.123456Z');
	$d2 = $date->format('Y-m-d H:i:s.u e');
	
	$this->assertTrue($d1 === $d2);
}

@t-tomek
Copy link
Author

t-tomek commented Feb 5, 2018

Just found out why this is not working for me.

It's related with setlocale('pl-PL.utf8'); as PHP has problem with decimal point :| see Bug #67127

@t-tomek
Copy link
Author

t-tomek commented Feb 6, 2018

Sorry, I copied wrong command it should be setlocale(LC_ALL, 'pl_PL.UTF-8');

But be aware that you have to have this locale installed on your system, otherwise locale won't be changed.

@Glavic
Copy link
Collaborator

Glavic commented Feb 17, 2018

@t-tomek: I have tested your configuration, and indeed it doesn't. It doesn't work for any locale which has comma for decimal point, even for my sl_SI. If you set only LC_TIME in setlocale() it will work.
Based that this is php bug I don't see what we can do? Closed the issue for the time being. If any new info is added, I will gladly reopen. TY

@kylekatarnls
Copy link
Collaborator

The initial issue says DateTime works for the same input. We should check if we have nothing related to the dot in our code.

@kylekatarnls kylekatarnls reopened this Feb 17, 2018
@kylekatarnls
Copy link
Collaborator

kylekatarnls commented Feb 26, 2018

Confirmed, new static($dt->format('Y-m-d H:i:s.u'), $dt->getTimezone()) loose microseconds.

I will try to fix it.

@kylekatarnls
Copy link
Collaborator

This is a PHP problem:

$date = new \DateTime('2017-07-29T13:57:27.123456Z');
echo $date->format('Y-m-d H:i:s.u e')."\n";

setlocale(LC_ALL, 'fr');

$date = new \DateTime('2017-07-29T13:57:27.123456Z');
echo $date->format('Y-m-d H:i:s.u e');

Outputs:

2017-07-29 13:57:27.123456 Z
2017-07-29 13:57:27.000000 Z

This is a big problem, since createFromFormat call new DateTime to wrap a Carbon object around the DateTime.

@Glavic @briannesbitt If you have some idea. I don't know how we could work around this.

kylekatarnls added a commit to kylekatarnls/Carbon that referenced this issue Feb 27, 2018
kylekatarnls added a commit to kylekatarnls/Carbon that referenced this issue Feb 27, 2018
kylekatarnls added a commit to kylekatarnls/Carbon that referenced this issue Feb 27, 2018
@kylekatarnls
Copy link
Collaborator

This is a fix to keep microseconds with coma decimal separator locales and work around https://bugs.php.net/bug.php?id=67127: #1128

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants