Download the PHP package vaibhavpandeyvpz/samay without Composer
On this page you can find all versions of the php package vaibhavpandeyvpz/samay. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vaibhavpandeyvpz/samay
More information about vaibhavpandeyvpz/samay
Files in vaibhavpandeyvpz/samay
Package samay
Short Description A lightweight, PSR-20 compliant clock library with zero dependencies other than the PSR-20 interface.
License MIT
Homepage https://github.com/vaibhavpandeyvpz/samay
Informations about the package samay
Samay
A lightweight, PSR-20 compliant clock library with zero dependencies other than the PSR-20 interface.
Samay (
समय) - Sanskrit for "Time", representing the flow of moments and the measurement of temporal events.
Features
- ✅ PSR-20 Compliant - Full implementation of the PSR-20 ClockInterface specification
- ✅ Zero Dependencies - Only requires PSR-20 interface (psr/clock)
- ✅ System Clock - Get current system time with microsecond precision
- ✅ Local Clock - Timezone-aware clock for specific timezones
- ✅ UTC Clock - Convenient UTC timezone clock
- ✅ Frozen Clock - Deterministic clock for testing scenarios
- ✅ Modern PHP - Built for PHP 8.2+ with modern language features
- ✅ 100% Test Coverage - Fully tested with comprehensive test suite
Installation
Install via Composer:
Quick Start
Usage
SystemClock
The SystemClock class provides access to the current system time. It implements the PSR-20 ClockInterface and returns a DateTimeImmutable instance representing the current moment.
Features
- Returns the current system time with microsecond precision
- Each call to
now()returns the current time (time progresses) - Uses the system's default timezone
- Returns immutable
DateTimeImmutableinstances
Example: Using SystemClock in a Service
FrozenClock
The FrozenClock class provides a deterministic clock that always returns the same time. This is extremely useful for testing scenarios where you need predictable, reproducible results.
Construction Options
Example: Using FrozenClock in Tests
LocalClock
The LocalClock class provides a timezone-aware clock that returns the current time in a specific timezone. It accepts either a timezone string or a DateTimeZone object.
Features
- Returns the current system time converted to the specified timezone
- Accepts timezone as string or
DateTimeZoneobject - Each call to
now()returns the current time (time progresses) - Returns immutable
DateTimeImmutableinstances with the correct timezone
Example: Using LocalClock for Regional Services
UtcClock
The UtcClock class extends LocalClock and provides a convenient way to always get the current time in UTC. It's equivalent to using LocalClock with 'UTC' timezone.
Features
- Always returns time in UTC timezone
- Extends
LocalClockfor consistency - No constructor parameters required
- Ideal for applications that need UTC time
Example: Using UtcClock for Timestamp Generation
Dependency Injection
Since all clock implementations (SystemClock, LocalClock, UtcClock, and FrozenClock) implement Psr\Clock\ClockInterface, you can easily inject them into your services:
Timezone Handling
All clock implementations handle timezones appropriately:
Microsecond Precision
All clocks support microsecond precision:
PSR-20 Compliance
This library fully implements the PSR-20 ClockInterface specification:
All clock implementations (SystemClock, LocalClock, UtcClock, and FrozenClock) implement this interface, ensuring compatibility with any PSR-20 compliant code.
Requirements
- PHP 8.2 or higher
- PSR-20 interface (psr/clock)
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
For issues, questions, or contributions, please visit the GitHub repository.