{"action":"created","issue":{"url":"https://api.github.com/repos/briannesbitt/Carbon/issues/1248","repository_url":"https://api.github.com/repos/briannesbitt/Carbon","labels_url":"https://api.github.com/repos/briannesbitt/Carbon/issues/1248/labels{/name}","comments_url":"https://api.github.com/repos/briannesbitt/Carbon/issues/1248/comments","events_url":"https://api.github.com/repos/briannesbitt/Carbon/issues/1248/events","html_url":"https://github.com/briannesbitt/Carbon/issues/1248","id":314615191,"number":1248,"title":"Round microseconds correctly and allow rounding explicitely","user":{"login":"spawnia","id":12158000,"avatar_url":"https://avatars2.githubusercontent.com/u/12158000?v=4","gravatar_id":"","url":"https://api.github.com/users/spawnia","html_url":"https://github.com/spawnia","followers_url":"https://api.github.com/users/spawnia/followers","following_url":"https://api.github.com/users/spawnia/following{/other_user}","gists_url":"https://api.github.com/users/spawnia/gists{/gist_id}","starred_url":"https://api.github.com/users/spawnia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spawnia/subscriptions","organizations_url":"https://api.github.com/users/spawnia/orgs","repos_url":"https://api.github.com/users/spawnia/repos","events_url":"https://api.github.com/users/spawnia/events{/privacy}","received_events_url":"https://api.github.com/users/spawnia/received_events","type":"User","site_admin":false},"labels":[{"id":13460513,"url":"https://api.github.com/repos/briannesbitt/Carbon/labels/question","name":"question","color":"cc317c","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-04-16T11:46:03Z","updated_at":"2018-04-16T18:00:06Z","closed_at":null,"author_association":"NONE","body":"I am happily using Carbon as part of a Laravel project.\r\n\r\nHowever, when comparing dates in unit testing, i run into an issue: I want to test the correct serialization of dates to the database. The format i store in the database only stores second precision, so the rehydrated Carbon instance does not contain microseconds.\r\n\r\nMy testing code looks like this:\r\n````php\r\n$now = now();\r\n\r\n$myModel->some_date_field = $now;\r\n$myModel->save();\r\n\r\n$this->assertTrue($now->equalsTo($myModel->some_date_field)\r\n\r\nvar_dump($now);\r\nvar_dump($myModel->some_date_field);\r\n````\r\n\r\nThe assertion fails and the output looks like this:\r\n````\r\nobject(Illuminate\\Support\\Carbon)#3536 (3) {\r\n  [\"date\"]=>\r\n  string(26) \"2017-04-16 11:36:08.769991\"\r\n  [\"timezone_type\"]=>\r\n  int(3)\r\n  [\"timezone\"]=>\r\n  string(3) \"UTC\"\r\n}\r\nobject(Illuminate\\Support\\Carbon)#3547 (3) {\r\n  [\"date\"]=>\r\n  string(26) \"2017-04-16 11:36:08.000000\"\r\n  [\"timezone_type\"]=>\r\n  int(3)\r\n  [\"timezone\"]=>\r\n  string(3) \"UTC\"\r\n}\r\n````\r\n\r\nFirstly, i think Carbon should round the timestamp to the next higher second.\r\n\r\nSecondly, it would be nice to have a way of comparing the equality of those dates in a way that allows to ignore the precision. I see a few ways for doing that:\r\n\r\n- Explicitely round the first date like this: `$carbon->roundSeconds()`\r\n- Pass an extra parameters to the equality check, specifying precision: `$carbon->equalsTo($carbon2, Carbon::SECOND)`\r\n- Have a seperate function for comparing equality with different precision `$carbon->equalsSecondsTo($carbon2)`\r\n\r\nWhich of those would be the most useful? I would happily submit a pull request, but would like to discuss this before getting to work.\r\n\r\nI currently worked around it by calling `$carbon->second($carbon->second)` which effectively rounded the date instance."},"comment":{"url":"https://api.github.com/repos/briannesbitt/Carbon/issues/comments/381694887","html_url":"https://github.com/briannesbitt/Carbon/issues/1248#issuecomment-381694887","issue_url":"https://api.github.com/repos/briannesbitt/Carbon/issues/1248","id":381694887,"user":{"login":"spawnia","id":12158000,"avatar_url":"https://avatars2.githubusercontent.com/u/12158000?v=4","gravatar_id":"","url":"https://api.github.com/users/spawnia","html_url":"https://github.com/spawnia","followers_url":"https://api.github.com/users/spawnia/followers","following_url":"https://api.github.com/users/spawnia/following{/other_user}","gists_url":"https://api.github.com/users/spawnia/gists{/gist_id}","starred_url":"https://api.github.com/users/spawnia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spawnia/subscriptions","organizations_url":"https://api.github.com/users/spawnia/orgs","repos_url":"https://api.github.com/users/spawnia/repos","events_url":"https://api.github.com/users/spawnia/events{/privacy}","received_events_url":"https://api.github.com/users/spawnia/received_events","type":"User","site_admin":false},"created_at":"2018-04-16T18:00:06Z","updated_at":"2018-04-16T18:00:06Z","author_association":"NONE","body":"Let me clarify on the microseconds bit: Carbon should absolutely keep the microsecond precision, i totally agree. However, when storing a date in second precision, rounding the value seems like a sensible default. Cutting off the value is equivalent to getting the floor - which might be useful but seems like it would be the exception.\r\n\r\nThe `isSame...` methods do offer the kind of comparison i am looking for, i did not catch that. However, it still might be useful to have more of them. Since Carbon already has `isSameYear/Month/Day`, do you think adding Hour, Minute and Second would make sense?\r\n\r\nOn the rounding bit: Your suggestions are cool, however they do not seem obvious or intuitive. Isn't the whole point of carbon to add a nice, simple to use API for dealing with dates? Adding methods for doing `floor`, `ceil` and `round` to arbitrary precision could certainly be a nice enhancement."}}
{"actor":{"display_login":"spawnia"}}