-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
DateTime Precision is lost when adding date to model factories #24214
Comments
This is expected behavior though, isnt it? A database timestamp does not have microseconds?
Your |
@laurencei We support specifying fractional seconds in datetime fields since #18847. The SQL standard uses 6 digits for this, per default, while mysql uses 0. Note that the test calls |
@sisve - yep, your right. The issue seems to be someone due to the Carbon change in v1.22. v1.21 works correctly - I can make the test pass. Switching to v1.22 causing the situation above. I found this: briannesbitt/Carbon#1234 I had a quick look and I cant work out why it's doing it though - as both use Carbon under the hood. I'll have a play later on if I get time. |
Carbon 1.22 was severely broken in many ways. Stay away from it. briannesbitt/Carbon#863 |
@sisve - yes, but it's not a break - it's actually an improvement in percission due to a PHP7 bug. The issue flows all the way forward to the latest version, and was an intended change. The strange thing is why it's dropping percission when going through the factory - I dont see an obvious reason why - so I'm going to dig in further. |
The microseconds are removed by You can specify a custom format in your model:
|
Is this a Carbon or Laravel issue? |
@tillkruss - it's an intendend change in Carbon behaviour, and so that's going to make it Laravel's problem on the way its implemented... I dont see an obvious fix though, because it would have to be a change in default behaviour in Laravel - which we might not want. I'd leave this open and see if someone has a good solution for it... |
I do not see how this is related to the linked Carbon issue. They are talking about how the some calls to static methods now have a microsecond value; but that is the expected behavior in this issue. Here the problem is that the microseconds disappears. I can reproduce this using the mysql grammar, so this does not depend on a mssql installation. I got stuck on reproducing this until I realized I had to add the field to the $dates array. This means that HasAttributes::setAttribute will convert the provided value into a string using the current query grammars; and no query grammars support microsecond precision. framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php Lines 551 to 556 in ca75ec8
|
The problem is we have always been dropping the microseconds. But it was never noticed or an issue, because Carbon was also dropping the microseconds. Now Carbon has changed behaviour and passes them through - highlighting the issue. It's not a bug, - its a change in behaviour by Carbon - and now it affects Laravel downstream. |
Technically we are no worse off than before. We've always dropped the microseconds - its not a new bug. The issue is Carbon tests will just "highlight" the issue now. So it would seem like something worth targeting for 5.7 to change and incorporate microseconds (if at all). |
It looks like we can't really support microseconds on MySQL at the moment. Saving works, but fetching doesn't: There is a four-year-old bug that removes microseconds when using |
Can this be closed then? |
Thats the only reason why I've started to use integers when I need to store datetime with microseconds. $model->setAttribute('cursor', Carbon::now()->format('Uu')); |
It will be great if everybody who experienced this issue will vote in PHP Bug №67122. It doesn't require registration and possibly will add importance for this bug. |
@tillkruss - i think it needs to stay open as it's going to come up as people upgrade their apps and run into this issue... And leaving it open might give other people a chance to come up with a creative solution... |
@laurencei I've found possible solution of this issue and posted it to the PHP bug tracker, but sadly I don't have an access to propose these changes directly to the PHP sources. |
Here is a PR which should fix this issue: php/php-src#3245 |
@a-komarev That PR may fix some related problems, but that PR alone will not fix this issue. Laravel still formats date fields using the query grammar's date format, and it doesn't support milli-/microseconds. |
There is a lot of progress with PHP bugfix: php/php-src#3257 |
@sisve I am not totally sure of that being true. |
@strongholdmedia Look at my reproduction just a few posts up. It doesn't do database calls at all, just creates an Eloquent mode, set's a date field and reads it back again. #24214 (comment) |
Ah, yes, now I remember. |
Would love to have full support of fractional seconds as well, this would include ANY date/time and timestamp methods. My use case is to fetch by created_at where there are several records a second added. Something along these lines:
I've tried a few permutations of modifying the $cast and $dates and creating custom mutators and even overriding the Model::asDateTime() functions to no avail |
Seems like the php/php-src#3257 was merged for the 7.3 release. If anyone can verify if this is fixed for the latest 7.3 RC that'd be great. I'm going to close this because this seems to be related to PHP rather than Laravel. People can still find this issue in the tracker. |
@driesvints Yes, this is working in PHP 7.3. |
Glad it finally turned out to be the same issue. |
Description:
When using a model factory with a fixed date the microseconds are lost. This means when asserting in a unit test dates do not match when they should.
Steps To Reproduce:
The text was updated successfully, but these errors were encountered: