Skip to main content

The Clock Pattern is a Python package that turns time into an injectable dependency.

Project description

🕰️ Clock Pattern

CI Pipeline Coverage Pipeline Package Version Supported Python Versions Package Downloads Project Documentation

The Clock Pattern is a Python 🐍 package that turns time into an injectable dependency 🧩. By replacing ad-hoc datetime.now() calls with a swappable Clock interface 🕰️ you unlock deterministic tests 🧪, decouple business logic from the OS clock, and gain the freedom to swap in high-precision or logical clocks without touching domain code.

Table of Contents

🔼 Back to top



📥 Installation

You can install Clock Pattern using pip:

pip install clock-pattern

🔼 Back to top



📚 Documentation

This project's documentation is powered by DeepWiki, which provides a comprehensive overview of the Clock Pattern and its usage.

🔼 Back to top



💻 Utilization

The Clock Pattern library is designed to be straightforward. Simply import the desired clock and use its now() or today() methods to get the current datetime/date. This approach allows for easy dependency injection and testing.

Here is a basic example of how to use the SystemClock clock:

from datetime import timezone

from clock_pattern import SystemClock

clock = SystemClock(timezone=timezone.utc)
print(clock.now())
# >>> 2025-06-16 13:57:26.210964+00:00

🔼 Back to top



📚 Available Clocks

The package offers several clock implementations to suit different needs:

  • clock_pattern.SystemClock: The standard clock implementation that returns the system's current datetime/date with the provided timezone.
  • clock_pattern.UtcClock: A clock implementation that returns the system's current datetime/date in UTC. Ideal for production environments.
  • clock_pattern.clocks.testing.FixedClock: A clock that always returns a fixed, preset datetime/date. It is perfect for basic testing as it allows you to control the datetime/date within your test environment, ensuring deterministic results.
  • clock_pattern.clocks.testing.MockClock: A clock that allows you to mock the system clock. It is perfect for more complex testing as it allows you to control the datetime/date within your test environment and if or not the methods are called or not.

🔼 Back to top



🎄 Real-Life Case: Christmas Detector Service

Below is an example of a real-life scenario where Clock Pattern can create clean and testable code. We have a ChristmasDetectorService that checks if the curren date falls within a specific Christmas holiday range. Using the Clock Pattern, in this case UtcClock and MockClock, we can decouple the service from the python datetime.now() and datetime.today() functions, making it easy to test for different dates without changing the system's time.

from datetime import date

from clock_pattern import Clock, UtcClock
from clock_pattern.clocks.testing import MockClock


class ChristmasDetectorService:
    def __init__(self, clock: Clock) -> None:
        self.clock = clock
        self.christmas_start = date(year=2024, month=12, day=24)
        self.christmas_end = date(year=2025, month=1, day=6)

    def is_christmas(self) -> bool:
        return self.christmas_start <= self.clock.today() <= self.christmas_end


clock = UtcClock()
christmas_detector_service = ChristmasDetectorService(clock=clock)

print(christmas_detector_service.is_christmas())
# >>> False


def test_christmas_detector_is_christmas() -> None:
    clock = MockClock()
    christmas_detector_service = ChristmasDetectorService(clock=clock)

    today = date(year=2024, month=12, day=25)
    clock.prepare_today_method_return_value(today=today)

    assert christmas_detector_service.is_christmas() is True
    clock.assert_today_method_was_called_once()


def test_christmas_detector_is_not_christmas() -> None:
    clock = MockClock()
    christmas_detector_service = ChristmasDetectorService(clock=clock)

    today = date(year=2025, month=1, day=7)
    clock.prepare_today_method_return_value(today=today)

    assert christmas_detector_service.is_christmas() is False
    clock.assert_today_method_was_called_once()

🔼 Back to top



🤝 Contributing

We love community help! Before you open an issue or pull request, please read:

Thank you for helping make 🕰️ Clock Pattern package awesome! 🌟

🔼 Back to top



🔑 License

This project is licensed under the terms of the MIT license.

🔼 Back to top

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

clock_pattern-0.6.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

clock_pattern-0.6.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file clock_pattern-0.6.0.tar.gz.

File metadata

  • Download URL: clock_pattern-0.6.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for clock_pattern-0.6.0.tar.gz
Algorithm Hash digest
SHA256 4a0f77972bd12baeb7761b1029682a1a29705da25943d97e53ca275ab9b6e61a
MD5 2f481190d27bfb7bb834c5d82c4790dd
BLAKE2b-256 305e81d1fa0d82f27a5487f356f2b470dae06f3702fe08cc820cf905f8ea52c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for clock_pattern-0.6.0.tar.gz:

Publisher: ci.yaml on adriamontoto/clock-pattern

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file clock_pattern-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: clock_pattern-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for clock_pattern-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d7ca6dd4394dfe324dfba3d9647f91d147fc993eaf853dfa394969a23a65b860
MD5 77b2cf370f1b88a4eaec0ac06b6ec59f
BLAKE2b-256 2149c68f9fb505e51cf7c3c91dac3ccf38d5d064acf6633331e9f12b0776eb1f

See more details on using hashes here.

Provenance

The following attestation bundles were made for clock_pattern-0.6.0-py3-none-any.whl:

Publisher: ci.yaml on adriamontoto/clock-pattern

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page