-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[6.x] Added a new mutator cast type - time
#30931
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
Conversation
time
time
Not saying that the mutator is not useful, it might be, but why aren't you using Carbon itself for the above comparisons/checks? Using the |
What bothers me a bit is that there is not true "time representation" by itself. It is always a full date object (this is simply an intrinsic behaviour of When it comes down to it, the only difference between the |
503c20c
to
d9d2f42
Compare
I have 47 applications in my work in which I need to display different information during working hours and after hours. Since all sites have the same code section, putting functionality into a package is, in my opinion, an overhead. Therefore, I suggested adding a new type of castes "in the box." There is a TIME field type in the database and I think it’s right to work with it this way. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What bothers me a bit is that there is not true "time representation" by itself. It is always a full date object (this is simply an intrinsic behaviour of
\DateTime
itself, unrelated to carbon et al).When it comes down to it, the only difference between the
date
and thetime
cast is really thecreateFromFormat('H:i:s', …)
part.
The difference is not only in the code, but also in the database, in which the TIME type field contains only time in the format "H: i: s" meaning that this time is "today".
I think it’s right to wrap the functionality in Carbon since it may require an easy use of this feature.
For example, a simple case:
(It looks awful, but the goal is to show the opportunity.)
// Model
protected function getIsOpenedAttribute()
{
$now = now();
return $now->gte($this->open_at) && $now->lt($this->close_at);
}
// Blade
@if($model->is_opened)
@lang('Opened')
@else
@lang('Closed')
@endif
And another example:
$now = now()->startOfSecond();
$carbon = Carbon::createFromFormat('H:i:s', date('H:i:s'))->startOfSecond();
dd([
'$time === $carbon' => $now === $carbon,
'$time == $carbon' => $now == $carbon,
'$time < $carbon' => $now < $carbon,
'$time > $carbon' => $now > $carbon,
'$now' => $now,
'$carbon' => $carbon,
]);
array:6 [
"$time === $carbon" => false
"$time == $carbon" => true
"$time < $carbon" => false
"$time > $carbon" => false
"$now" => Illuminate\Support\Carbon @1577281351 {#32
date: 2019-12-25 16:42:31.0 Europe/Moscow (+03:00)
}
"$carbon" => Carbon\Carbon @1577281351 {#881
date: 2019-12-25 16:42:31.0 Europe/Moscow (+03:00)
}
]
d9d2f42
to
9e70f31
Compare
{ | ||
return $value instanceof CarbonInterface | ||
? Date::instance($value) | ||
: Date::createFromFormat('H:i:s', $value)->startOfSecond(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a microsecond reset because without it the time comparison would be wrong.
The TIME field type of the database does not store information about milliseconds, and when the now ()
functions are called in sequence, the values will be different.
The startOfSecond ()
method leads them to a single value, as a result of which the comparison will be correct.
The result of the verification showed here: #30931 (review)
If the text is not written correctly in docblock, please tell me how to. |
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions! If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response. |
@taylorotwell, this is not a bug. This is a feature addition. I got your point. But I believe that this functionality in several lines of code is not advisable to put in a package. I want to ask: do you think it's worth putting control of castes into an instance based on value-object pattern? I can do it. But is it necessary? |
Adding a new type of mutator will make it easier to work with time comparisons.
For example: