Search by

hryvinskyi / magento2-clock

hryvinskyi

One PSR-20 clock for Magento 2 in the store's timezone: the current store's clock, the clock of any store view, and the UTC instant they share, which a test replaces to set the time.

Package info

github.com/hryvinskyi/magento2-clock

Type:magento2-module

pkg:composer/hryvinskyi/magento2-clock

Statistics

Installs: 35

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-11-30 18:52 UTC

This package is auto-updated.

Last update: 2026-11-30 20:09:25 UTC


README

One PSR-20 clock for Magento 2 in the store's timezone. A module that needs "now" asks for Psr\Clock\ClockInterface and gets the time of the store it works in, so a date counted from it (the day a request was made, the day an allowance rolls over) is the day the store's customers live.

What it does for a store

  • The current store's clock. Psr\Clock\ClockInterface resolves to the clock of the store view that is current when it is asked, on every call, so it follows a store emulation. The current store is the store view Magento resolved for the request, the admin store in the admin; on the command line and in cron, without an emulation, it is the default store view.
  • Any store's clock. StoreClocksInterface::forStore($storeId) is the clock of one store view, for work done for a store from cron, the command line or the admin.
  • The instant itself. UtcClockInterface is the current moment in UTC, to the second. Every store clock shifts this one instant into a timezone, so replacing it sets the time for all of them. A test replaces this one service to set the time.
  • Where the timezone comes from. The store clock's timezone is general/locale/timezone (Stores › Configuration › General › General › Locale Options › Timezone, a website-scope setting): a store view takes its website's value, else the default scope's.
  • A clock never fails. A store view that no longer exists, and store 0, take the default scope's timezone. A value that is empty, or one PHP does not know as a timezone identifier, gives UTC. Nothing is logged, because a clock is read on every call and would flood the log.

Requirements

  • Magento Open Source or Adobe Commerce, PHP 8.1 to 8.5 (the range composer.json allows).
  • psr/clock ^1.0, magento/framework and magento/module-store.

Installation

Run from the Magento root.

composer require hryvinskyi/magento2-clock
bin/magento module:enable Hryvinskyi_Clock
bin/magento setup:upgrade
bin/magento setup:di:compile  # production mode only
bin/magento cache:flush

The module has no settings of its own and creates no table. It reads the store's timezone setting.

Using the clock

use Hryvinskyi\Clock\Api\StoreClocksInterface;
use Psr\Clock\ClockInterface;

public function __construct(
    private readonly ClockInterface $clock,
    private readonly StoreClocksInterface $storeClocks
) {
}

// Now in the timezone of the store that is current: the admin store in the admin, else the default store view.
$now = $this->clock->now();

// Now in the timezone of store view 3, for example in a cron job that works for each store.
$storeNow = $this->storeClocks->forStore(3)->now();

Limits

  • Cron and the command line run in the default store view's timezone unless the code emulates a store. A job that works for a store passes through forStore() or emulates the store.
  • A moment kept in the database should stay UTC. The store's timezone is for counting days and for showing a time, not for storing one.
  • A store view that is deleted after work was recorded for it counts its days in the default scope's timezone from then on.
  • The current store's clock throws Magento\Framework\Exception\NoSuchEntityException when the current store view itself no longer exists.

Registrations

Where What
preference Hryvinskyi\Clock\Api\UtcClockInterface Hryvinskyi\Clock\Model\SystemClock
preference Hryvinskyi\Clock\Api\StoreClocksInterface Hryvinskyi\Clock\Model\StoreClocks
preference Psr\Clock\ClockInterface Hryvinskyi\Clock\Model\CurrentStoreClock

The module registers no route, command, cron job, event or plugin.

Extension point

Both interfaces are @api. To set the time in an integration test, replace the UTC clock with one that tells a fixed instant, and put the original back when the test ends. Every store clock follows.

<preference for="Hryvinskyi\Clock\Api\UtcClockInterface" type="Vendor\Module\Test\Fixture\FixedUtcClock"/>

Tests

  • Unit: vendor/bin/phpunit -c app/code/Hryvinskyi/Clock/phpunit.xml.dist, run from the Magento root.
  • Integration: the platform's harness in dev/tests/integration, one run at a time against its database. cd dev/tests/integration && php -d memory_limit=4G ../../../vendor/bin/phpunit -c phpunit.xml ../../../app/code/Hryvinskyi/Clock/Test/Integration.
  • Static analysis: PHPStan 2.x at level 10 with the package's phpstan.neon.dist (phpstan analyse -c app/code/Hryvinskyi/Clock/phpstan.neon.dist --level=10), and vendor/bin/phpcs --standard=app/code/Hryvinskyi/Clock/phpcs.xml.dist app/code/Hryvinskyi/Clock.