Download the PHP package tiny-blocks/time without Composer
On this page you can find all versions of the php package tiny-blocks/time. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tiny-blocks/time
More information about tiny-blocks/time
Files in tiny-blocks/time
Package time
Short Description Models time as immutable value objects for PHP: instants, durations, periods, timezones, and time-of-day, all UTC-normalized.
License MIT
Homepage https://github.com/tiny-blocks/time
Informations about the package time
Time
- Overview
- Installation
- How to use
- Instant
- Creating from the current moment
- Creating from a string
- Creating from a database timestamp
- Creating from Unix seconds
- Adding and subtracting time
- Adding and subtracting months and years
- Measuring distance between instants
- Comparing instants
- Emitting with sub-second precision
- Serializing with the mapper
- Duration
- Creating durations
- Arithmetic
- Division
- Comparing durations
- Converting to other units
- MonotonicClock
- Reading the current nanoseconds
- Measuring an elapsed window
- Stopwatch
- Starting a stopwatch
- Reading the elapsed interval
- Reading the elapsed interval more than once
- Period
- Creating from two instants
- Creating from a start and duration
- Getting the duration
- Checking if an instant is contained
- Detecting overlap
- DayOfWeek
- Deriving from an Instant
- Checking weekday or weekend
- Calculating forward distance
- TimeOfDay
- Creating from components
- Creating from a string
- Deriving from an Instant
- Named constructors
- Comparing times
- Measuring distance between times
- Converting to other representations
- LocalDate
- Creating from components
- Creating from a string
- Today in a timezone
- Projecting an Instant
- Anchoring a date to the timeline
- Comparing dates
- Day arithmetic
- Month and year arithmetic
- Serializing with the mapper
- Timezone
- Creating from an identifier
- Creating a UTC timezone
- Converting to DateTimeZone
- Timezones
- Creating from objects
- Creating from strings
- Getting all timezones
- Finding a timezone by identifier
- Finding a timezone by identifier with UTC fallback
- Checking if a timezone exists in the collection
- Getting all identifiers as strings
- Value equality
- Checking equality
- Checking equality across factories
- Checking equality of a nested value object
- Instant
- License
- Contributing
Overview
Models time as immutable value objects for PHP, including instants, durations, periods, timezones, time-of-day, local
dates, and day-of-week. All instants are normalized to UTC with microsecond precision, with strict parsing, formatting,
and arithmetic operations. Declared as final readonly class for language-level immutability, with structural equality
provided by the tiny-blocks value-object contract.
Installation
How to use
The library provides immutable value objects for representing points in time, quantities of time, and time intervals. All instants are normalized to UTC internally.
Instant
An Instant represents a single point on the timeline, always stored in UTC with microsecond precision.
Creating from the current moment
Captures the current moment with microsecond precision, normalized to UTC.
Creating from a string
Parses a date-time string with an explicit UTC offset. The value is normalized to UTC regardless of the original offset.
The offset accepts the ISO 8601 zulu designator (Z) as an alias for +00:00, with or without fractional seconds.
Creating from a database timestamp
Parses a database date-time string as UTC, with or without microsecond precision (e.g. MySQL DATETIME
or DATETIME(6)).
Also supports timestamps without fractional seconds:
Creating from Unix seconds
Creates an Instant from a Unix timestamp in seconds.
Adding and subtracting time
Returns a new Instant shifted forward or backward by a Duration.
Adding and subtracting months and years
Re-anchors the instant in a timezone (UTC when omitted), shifts the local calendar date by whole months or years with
the same day clamping as LocalDate, and normalizes the result back to UTC. The local time of day, including
microseconds, is preserved.
Passing a timezone re-anchors the shift in that zone. When the resulting local wall time does not exist because of a spring-forward gap, PHP shifts it forward by the gap.
Measuring distance between instants
Returns the absolute Duration between two Instant objects.
The result is always non-negative regardless of direction:
Comparing instants
Provides strict temporal ordering between two Instant instances.
Emitting with sub-second precision
By default toIso8601() emits seconds only. Pass a Precision value to include fractional seconds in the output.
Existing callers that omit the argument are unaffected.
Serializing with the mapper
Instant carries a #[ScalarCodec], so tiny-blocks/mapper rebuilds it from an ISO 8601 string and writes it back to
the same form, with no mapping to register. As a field on a larger object the value stays a scalar at second precision,
matching the default of toIso8601().
Duration
A Duration represents an immutable, unsigned quantity of time measured in seconds. It has no reference point on the
timeline. It expresses only "how much" time.
Creating durations
All factories reject negative values:
Arithmetic
Subtraction that would produce a negative result throws an exception:
Division
Returns the number of times one Duration fits wholly into another. The result is truncated toward zero:
Division by a zero Duration throws an exception:
Comparing durations
Converting to other units
Conversions truncate toward zero when the duration is not an exact multiple:
MonotonicClock
A MonotonicClock exposes a high-resolution counter for measuring elapsed time, conceptually distinct from Duration:
Duration is a wall-clock quantity measured in whole seconds, while a monotonic reading is an opaque nanosecond counter
whose absolute value carries no calendar meaning and is only useful as the delta between two readings on the same clock.
The default SystemMonotonicClock implementation is backed by PHP's hrtime(true).
Reading the current nanoseconds
Returns the current monotonic reading as an integer nanosecond count. The value has no calendar meaning. Treat it as an opaque counter and only compare it to another reading from the same clock.
Measuring an elapsed window
Subtract two successive readings on the same clock to obtain the elapsed interval in nanoseconds. Readings are guaranteed to be non-decreasing.
Stopwatch
A Stopwatch separates the act of measuring from the value being measured. It captures a starting reading from a
MonotonicClock and exposes the accumulated interval as an Elapsed value object. The clock is injected explicitly so
the time source stays under the caller's control, and reading the interval is idempotent: invoking elapsed() more than
once returns successive measurements from the same starting reading.
Elapsed is a pure value object expressed in nanoseconds. It is distinct from Duration, which models wall-clock
seconds, and nanosecond and second granularities are kept in separate types so the intent of each measurement stays
explicit at the call site.
Starting a stopwatch
Captures the current reading of the provided monotonic clock and returns a stopwatch anchored to that moment.
Reading the elapsed interval
Returns an Elapsed measuring the interval between the starting reading and the current reading of the same clock.
toMilliseconds() converts the nanosecond count to milliseconds rounded to two decimal places.
Reading the elapsed interval more than once
The starting reading is captured once and never changes. Each call to elapsed() returns a new Elapsed measured from
that same anchor, so successive calls report a non-decreasing series of intervals.
Period
A Period represents a half-open time interval [from, to) between two UTC instants. The start is inclusive and the
end is exclusive.
Creating from two instants
The start must be strictly before the end:
Creating from a start and duration
Getting the duration
Checking if an instant is contained
The check is inclusive at the start and exclusive at the end:
Detecting overlap
Two half-open intervals [A, B) and [C, D) overlap when A < D and C < B:
Adjacent periods do not overlap:
DayOfWeek
A DayOfWeek represents a day of the week following ISO 8601, where Monday is 1 and Sunday is 7.
Deriving from an Instant
Checking weekday or weekend
Calculating forward distance
Returns the number of days forward from one day to another, always in the range [0, 6]. The distance is measured
forward through the week:
TimeOfDay
A TimeOfDay represents a time of day (hour and minute) without date or timezone context. Values range from 00:00 to
23:59.
Creating from components
Creating from a string
Parses a string in HH:MM or HH:MM:SS format. When seconds are present, they are discarded:
Also accepts the HH:MM:SS format commonly returned by databases:
Deriving from an Instant
Extracts the time of day from an Instant in UTC:
Named constructors
Comparing times
Measuring distance between times
Returns the Duration between two times. The second time must be after the first:
Converting to other representations
LocalDate
A LocalDate is a value object representing a calendar date (year, month, day) without time and without timezone. Dates
are always in the proleptic Gregorian calendar and restricted to the range 0001–9999.
Creating from components
Creating from a string
Accepts only the canonical ISO 8601 date format YYYY-MM-DD. Any other format raises InvalidLocalDate.
Today in a timezone
Projecting an Instant
Anchoring a date to the timeline
atTime is the inverse of Instant::toLocalDate. A LocalDate and a TimeOfDay are both civil values, so the
timezone is what turns them into a point on the timeline.
Daylight saving leaves two civil times that are not one-to-one with the timeline, and both resolve deterministically. A time inside the spring-forward gap does not exist, and it maps to the same instant as the first civil time after the gap. A time inside the fall-back overlap happens twice, and it maps to the earlier of the two, the one still on the pre-transition offset.
Comparing dates
Day arithmetic
Month and year arithmetic
Shifts the year and month, then clamps the day to the last valid day of the target month. A negative count shifts in the opposite direction.
Because the day is clamped, the shift is not associative. Adding a month and then subtracting it does not always restore the original day.
Serializing with the mapper
LocalDate carries a #[ScalarCodec], so tiny-blocks/mapper rebuilds it from a YYYY-MM-DD string and writes it
back to the same date-only form, instead of widening to a full datetime.
Timezone
A Timezone is a value object representing a single valid IANA timezone identifier.
Creating from an identifier
Creating a UTC timezone
Converting to DateTimeZone
Timezones
An immutable collection of Timezone objects.
Creating from objects
Creating from strings
Getting all timezones
Returns all Timezone objects in the collection:
Finding a timezone by identifier
Searches for a specific IANA identifier within the collection. Returns null if not found.
Finding a timezone by identifier with UTC fallback
Searches for a specific IANA identifier within the collection. Returns UTC if not found.
Checking if a timezone exists in the collection
Getting all identifiers as strings
Returns all timezone identifiers as plain strings:
Value equality
Every type of this library that implements the value-object contract compares by value, never by instance. A value object that carries another as a property inherits that, because the comparison walks the properties structurally.
Checking equality
Two instances of the same value are equal and share the same hash code, however each one was written.
Checking equality across factories
The factory that built the instance is not part of the value, so instances from different factories are equal.
Checking equality of a nested value object
A value object holding another compares by value, because the comparison recurses into each property.
License
Time is licensed under MIT.
Contributing
Please follow the contributing guidelines to contribute to the project.
All versions of time with dependencies
tiny-blocks/collection Version ^2.6
tiny-blocks/mapper Version ^3.2
tiny-blocks/value-object Version ^5.0