CooperDateTime
in package
implements
Stringable, JsonSerializable
A CASC offset date-time (CASC.md §6.6) -- `1979-05-27T07:32:00Z`, `...00.120+02:00` -- matching Elixir's `DateTime`: an instant at microsecond resolution, normalized to UTC, together with how many fractional digits were written (`$precision`, at most 6).
It was once a bare DateTimeImmutable, which holds the microseconds
but not their count, so .120 came back as .12, .0 as no fraction
at all, and .5 and .50 -- different values to the reference's
strict equality -- were the same. toDateTimeImmutable() hands the
instant to code that wants PHP's own type.
Table of Contents
Interfaces
- Stringable
- JsonSerializable
Properties
- $instant : DateTimeImmutable
- The instant, in UTC.
- $precision : int
Methods
- __construct() : mixed
- __toString() : string
- Space-separated, as Elixir's `DateTime` prints -- the form an interpolated `"@{when}"` produces in every Cooper implementation.
- equals() : bool
- jsonSerialize() : string
- Encodes as the ISO 8601 form (`toIso8601()`).
- parse() : self
- Parses `YYYY-MM-DDTHH:MM:SS[.fff]` followed by `Z` or `±HH:MM`.
- toDateTimeImmutable() : DateTimeImmutable
- The instant as PHP's own type, in `$timezone` (default: UTC).
- toIso8601() : string
- fraction() : string
Properties
$instant read-only
The instant, in UTC.
public
DateTimeImmutable
$instant
$precision read-only
public
int
$precision
= 0
Methods
__construct()
public
__construct(DateTimeInterface $instant[, int $precision = 0 ]) : mixed
Parameters
- $instant : DateTimeInterface
-
the instant, in any zone; held in UTC
- $precision : int = 0
-
how many fractional digits to render,
0..6
Tags
__toString()
Space-separated, as Elixir's `DateTime` prints -- the form an interpolated `"@{when}"` produces in every Cooper implementation.
public
__toString() : string
Use toIso8601() for the T-separated form.
Return values
string —YYYY-MM-DD HH:MM:SS[.fff]Z
equals()
public
equals(mixed $other) : bool
Parameters
- $other : mixed
-
the value to compare against
Return values
bool —true for the same instant written with the same
precision -- Elixir's strict equality on DateTime
jsonSerialize()
Encodes as the ISO 8601 form (`toIso8601()`).
public
jsonSerialize() : string
Return values
string —the T-separated form
parse()
Parses `YYYY-MM-DDTHH:MM:SS[.fff]` followed by `Z` or `±HH:MM`.
public
static parse(string $text) : self
Fractional digits past the sixth are truncated, as Elixir's
DateTime.from_iso8601/1 truncates them -- never rounded up into
the next second.
Parameters
- $text : string
-
e.g.
"1979-05-27T07:32:00.120+02:00"
Tags
Return values
self —the instant, in UTC
toDateTimeImmutable()
The instant as PHP's own type, in `$timezone` (default: UTC).
public
toDateTimeImmutable([DateTimeZone|null $timezone = null ]) : DateTimeImmutable
Parameters
- $timezone : DateTimeZone|null = null
-
the zone to express the instant in
Return values
DateTimeImmutable —the same instant
toIso8601()
public
toIso8601() : string
Return values
string —the ISO 8601 form in UTC, YYYY-MM-DDTHH:MM:SS[.fff]Z
fraction()
private
fraction() : string
Return values
string —. and the first $precision microsecond digits, or
nothing at precision 0