feat(timestamp): add now() factories and a DateTimeInterface constructor - #150
Merged
Merged
Conversation
Add Timestamp::now() and Timestamp::nowUtc() so callers do not have to write `new Timestamp(time())`. A timestamp holds a UTC epoch value in milliseconds, so nowUtc() is an alias of now(). The constructor now accepts a DateTimeInterface as its first argument. This makes `new Timestamp($dateTime)` equal to Timestamp::fromDateTime(). A DateTimeInterface together with $microseconds throws InvalidArgumentException, because the second value would be discarded. The conversion from DateTimeInterface to milliseconds moves into one helper that the constructor and fromDateTime() share.
|
Tick the box to add this pull request to the merge queue (same as
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Cassandra\Timestampgets two convenience factories and a wider constructor.Why
new Cassandra\Timestamp(time())is the common way to get the current time today. A named factory reads better. The constructor accepted only integers, so callers had to remember a second entry point (fromDateTime) for a date and time object.Notes
nowUtc()is an alias ofnow(). A timestamp holds a UTC epoch value in milliseconds, so it carries no zone. The method exists because the name is what users look for.DateTimeInterfacetogether with$microsecondsthrowsInvalidArgumentException. The alternative is to discard the second value without a message.DateTimeInterfaceto milliseconds now lives in one helper. The constructor andfromDateTime()share it.$seconds, so named arguments stay compatible.Changes
src/DateTime/Timestamp.stub.php,src/DateTime/Timestamp.ctests/Unit/DateTime/TimestampTest.php— 8 new casesdocs/Cassandra/Timestamp.php(also adds the missingfromDateTimeentry),website/reference/values.md,website/guide/data-types.mdTest