Skip to content

feat(timestamp): add now() factories and a DateTimeInterface constructor - #150

Merged
CodeLieutenant merged 1 commit into
trunkfrom
feat/timestamp-now-and-datetime-constructor
Aug 9, 2026
Merged

feat(timestamp): add now() factories and a DateTimeInterface constructor#150
CodeLieutenant merged 1 commit into
trunkfrom
feat/timestamp-now-and-datetime-constructor

Conversation

@CodeLieutenant

Copy link
Copy Markdown
Member

What

Cassandra\Timestamp gets two convenience factories and a wider constructor.

$ts = Cassandra\Timestamp::now();
$ts = Cassandra\Timestamp::nowUtc();          // alias of now()
$ts = new Cassandra\Timestamp(new DateTime()); // same result as fromDateTime()

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 of now(). 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.
  • A DateTimeInterface together with $microseconds throws InvalidArgumentException. The alternative is to discard the second value without a message.
  • The conversion from DateTimeInterface to milliseconds now lives in one helper. The constructor and fromDateTime() share it.
  • The first parameter keeps the name $seconds, so named arguments stay compatible.

Changes

  • src/DateTime/Timestamp.stub.php, src/DateTime/Timestamp.c
  • tests/Unit/DateTime/TimestampTest.php — 8 new cases
  • docs/Cassandra/Timestamp.php (also adds the missing fromDateTime entry), website/reference/values.md, website/guide/data-types.md

Test

php -d extension=out/DebugPHP8.4NTS/cassandra.dylib ./vendor/bin/pest tests/Unit/DateTime/
Tests: 2 skipped, 62 passed (96 assertions)

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.
@mergify

mergify Bot commented Aug 9, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@CodeLieutenant CodeLieutenant self-assigned this Aug 9, 2026
@CodeLieutenant
CodeLieutenant merged commit 0af9f5d into trunk Aug 9, 2026
35 checks passed
@CodeLieutenant
CodeLieutenant deleted the feat/timestamp-now-and-datetime-constructor branch August 9, 2026 23:42
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

Successfully merging this pull request may close these issues.

1 participant