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

createFromTime() fails when seconds parameter > 99 #1731

Closed
cjcox17 opened this issue May 23, 2019 · 4 comments
Closed

createFromTime() fails when seconds parameter > 99 #1731

cjcox17 opened this issue May 23, 2019 · 4 comments

Comments

@cjcox17
Copy link

cjcox17 commented May 23, 2019

Hello,

Anytime that the seconds parameter is greater than 99 the function fails. However, 99 and below works properly.

I encountered an issue with the following code:

Carbon::createFromTime(0,0,100)->toTimeString()

Carbon version: nesbot/carbon v2.17.1

PHP version: v7.1.23

I expected to get:

00:01:40

But I actually get:

Error: The separation symbol could not be found
Trailing data

Try here

https://try-carbon.herokuapp.com/?embed&theme=xcode&border=silver&options-color=rgba(120,120,120,0.5)&input=Carbon%3A%3AcreateFromTime(0%2C0%2C100)-%3EtoTimeString()%3B

Thanks!

@cjcox17
Copy link
Author

cjcox17 commented May 23, 2019

Test:

>>> Carbon\Carbon::createFromTime(0,0,1)->toTimeString();
=> "00:00:01"
>>> Carbon\Carbon::createFromTime(0,0,99)->toTimeString();
=> "00:01:39"
>>> Carbon\Carbon::createFromTime(0,0,100)->toTimeString();
InvalidArgumentException with message 'The separation symbol could not be found
Trailing data'

@cjcox17
Copy link
Author

cjcox17 commented May 23, 2019

The above function may not have been designed to provide that kind of functionality and if that's the case I think a more detailed exception should be thrown.

In retrospect,

Carbon\Carbon::createFromTimeStamp(100)->toTimeString();

Works just as well when needing only the time string.

@kylekatarnls
Copy link
Collaborator

Hello,

This is in fact due to the DateTime behavior:

echo DateTime::createFromFormat('H:i:s', '00:00:01')->format('H:i:s'); // "00:00:01"
echo DateTime::createFromFormat('H:i:s', '00:00:99')->format('H:i:s'); // "00:01:39"
echo DateTime::createFromFormat('H:i:s', '00:00:100')->format('H:i:s'); // fails as "s" format accepts only 2 digits.

That means, if we change something here, it would be for a more strict validation (error if $seconds > 59) but we won't accept 100, as createFromTime is supposed to take a "time" meaning a valid set of hour/minute/second.

I was about to suggest what you commented meanwhile. Using createFromTimeStamp or some Carbon::today()->addSeconds(100) is really better because it explicitly seconds unit from a start point, not a time.

@kylekatarnls
Copy link
Collaborator

I kept the limits to [0; 99] but added specific exceptions messages for the next release.

kylekatarnls added a commit to akalongman/Carbon that referenced this issue Jun 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants