clock-pattern

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 πŸ§ͺ and decouple business logic from the OS clock.

https://github.com/adriamontoto/clock-pattern

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • βœ“
    CITATION.cff file
    Found CITATION.cff file
  • βœ“
    codemeta.json file
    Found codemeta.json file
  • βœ“
    .zenodo.json file
    Found .zenodo.json file
  • β—‹
    DOI references
  • β—‹
    Academic publication links
  • β—‹
    Committers with academic emails
  • β—‹
    Institutional organization owner
  • β—‹
    JOSS paper metadata
  • β—‹
    Scientific vocabulary similarity
    Low similarity (8.6%) to scientific vocabulary

Keywords

clock dependency-injection development domain-driven-design pattern python python3 python311 python312 python313 utilities

Keywords from Contributors

interactive ecosystem-modeling mesh interpretability profiles distribution sequences generic projection standardization
Last synced: 4 months ago · JSON representation ·

Repository

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 πŸ§ͺ and decouple business logic from the OS clock.

Basic Info
Statistics
  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • Open Issues: 0
  • Releases: 5
Topics
clock dependency-injection development domain-driven-design pattern python python3 python311 python312 python313 utilities
Created 6 months ago · Last pushed 4 months ago
Metadata Files
Readme Changelog Contributing Funding License Code of conduct Citation Codeowners Security

README.md

πŸ•°οΈ 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`: ```bash pip install clock-pattern ```

πŸ”Ό Back to top



## πŸ“š Documentation This [project's documentation](https://deepwiki.com/adriamontoto/clock-pattern) 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`](https://github.com/adriamontoto/clock-pattern/blob/master/clock_pattern/clocks/system_clock.py) clock: ```python 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`](https://github.com/adriamontoto/clock-pattern/blob/master/clock_pattern/clocks/system_clock.py): The standard clock implementation that returns the system's current datetime/date with the provided timezone. - [`clock_pattern.UtcClock`](https://github.com/adriamontoto/clock-pattern/blob/master/clock_pattern/clocks/utc_clock.py): A clock implementation that returns the system's current datetime/date in UTC. Ideal for production environments. - [`clock_pattern.clocks.testing.FixedClock`](https://github.com/adriamontoto/clock-pattern/blob/master/clock_pattern/clocks/testing/fixed_clock.py): 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`](https://github.com/adriamontoto/clock-pattern/blob/master/clock_pattern/clocks/testing/mock_clock.py): 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`](https://github.com/adriamontoto/clock-pattern/blob/master/clock_pattern/clocks/utc_clock.py) and [`MockClock`](https://github.com/adriamontoto/clock-pattern/blob/master/clock_pattern/clocks/testing/mock_clock.py), 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. ```python 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: - [`🀝 How to Contribute`](https://github.com/adriamontoto/clock-pattern/blob/master/.github/CONTRIBUTING.md) - [`🧭 Code of Conduct`](https://github.com/adriamontoto/clock-pattern/blob/master/.github/CODE_OF_CONDUCT.md) - [`πŸ” Security Policy`](https://github.com/adriamontoto/clock-pattern/blob/master/.github/SECURITY.md) _Thank you for helping make **πŸ•°οΈ Clock Pattern** package awesome! 🌟_

πŸ”Ό Back to top



## πŸ”‘ License This project is licensed under the terms of the [`MIT license`](https://github.com/adriamontoto/clock-pattern/blob/master/LICENSE.md).

πŸ”Ό Back to top

Owner

  • Login: adriamontoto
  • Kind: user
  • Location: /dev/null
  • Company: Amazon

Software Development Engineer @ Amazon

Citation (CITATION.cff)

cff-version: 1.2.0
title: Clock Pattern
message: >
  If you use this software, please cite it using the metadata below.
type: software
authors:
  - given-names: AdriΓ 
    family-names: Montoto
repository-code: https://github.com/adriamontoto/clock-pattern
url: https://pypi.org/project/clock-pattern
abstract: >
  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
  and decouple business logic from the OS clock.
keywords:
  - python
  - clock
  - dependency-injection
  - domain-driven-design
license: MIT

GitHub Events

Total
  • Release event: 3
  • Watch event: 3
  • Delete event: 19
  • Issue comment event: 24
  • Push event: 27
  • Pull request review event: 11
  • Pull request event: 39
  • Create event: 22
Last Year
  • Release event: 3
  • Watch event: 3
  • Delete event: 19
  • Issue comment event: 24
  • Push event: 27
  • Pull request review event: 11
  • Pull request event: 39
  • Create event: 22

Committers

Last synced: 5 months ago

All Time
  • Total Commits: 36
  • Total Committers: 3
  • Avg Commits per committer: 12.0
  • Development Distribution Score (DDS): 0.389
Past Year
  • Commits: 36
  • Committers: 3
  • Avg Commits per committer: 12.0
  • Development Distribution Score (DDS): 0.389
Top Committers
Name Email Commits
Adria Montoto 7****o 22
dependabot[bot] 4****] 9
adriamontoto-bot 1****t 5

Issues and Pull Requests

Last synced: 4 months ago

All Time
  • Total issues: 0
  • Total pull requests: 26
  • Average time to close issues: N/A
  • Average time to close pull requests: 4 days
  • Total issue authors: 0
  • Total pull request authors: 1
  • Average comments per issue: 0
  • Average comments per pull request: 0.85
  • Merged pull requests: 13
  • Bot issues: 0
  • Bot pull requests: 26
Past Year
  • Issues: 0
  • Pull requests: 26
  • Average time to close issues: N/A
  • Average time to close pull requests: 4 days
  • Issue authors: 0
  • Pull request authors: 1
  • Average comments per issue: 0
  • Average comments per pull request: 0.85
  • Merged pull requests: 13
  • Bot issues: 0
  • Bot pull requests: 26
Top Authors
Issue Authors
Pull Request Authors
  • dependabot[bot] (26)
Top Labels
Issue Labels
Pull Request Labels
dependencies (26) github_actions (26)

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 778 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 5
  • Total maintainers: 1
pypi.org: clock-pattern

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

  • Versions: 5
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 778 Last month
Rankings
Dependent packages count: 9.0%
Average: 29.7%
Dependent repos count: 50.5%
Maintainers (1)
Last synced: 4 months ago

Dependencies

.github/workflows/ci.yaml actions
  • actions/checkout 11bd71901bbe5b1630ceea73d27597364c9af683 composite
  • actions/download-artifact d3f86a106a0bac45b974a628896c90dbdf5c8093 composite
  • actions/setup-python a26af69be951a213d495a4c3e4e4022e16d87065 composite
  • actions/upload-artifact ea165f8d65b6e75b540449e92b4886f43607fa02 composite
  • astral-sh/setup-uv 445689ea25e0de0a23313031f5fe577c74ae45a1 composite
  • github/codeql-action/analyze ce28f5bb42b7a9f2c824e633a3f6ee835bab6858 composite
  • github/codeql-action/init ce28f5bb42b7a9f2c824e633a3f6ee835bab6858 composite
  • py-cov-action/python-coverage-comment-action 970a227e0c16ef4589a99a9970ab0ceb8c53059a composite
  • pypa/gh-action-pypi-publish 76f52bc884231f62b9a034ebfe128415bbaabdfc composite
  • python-semantic-release/python-semantic-release f9e152fb36cd2e590fe8c2bf85bbff08f7fc1c52 composite
  • re-actors/alls-green 05ac9388f0aebcb5727afa17fcccfecd6f8ec5fe composite
  • step-security/harden-runner 002fdce3c6a235733a90a27c80493a3241e56863 composite
pyproject.toml pypi
  • value-object-pattern >=0.4.0