Skip to main content

A Python library for performing arithmetic with HH:MM[:SS] formatted time strings.

Project description

hmscalc

PyPI version Python versions

A lightweight Python library for performing arithmetic on time values formatted as HH:MM or HH:MM:SS.

🚀 Features

  • Supports time addition and subtraction
  • Sum, average, min, and max of multiple time values
  • Accepts HH:MM and HH:MM:SS formatted strings
  • Handles negative durations gracefully
  • Comparison operators (==, !=, <, <=, >, >=)
  • Converts time to seconds, minutes, hours, timedelta, and dictionary/tuple formats
  • Integration with datetime.timedelta
  • Custom exceptions for invalid input
  • Fully testable across multiple Python versions via Docker

🐳 Quick Start (Docker-based)

# Build the image
docker build -t hmscalc .

# Run tests across multiple Python versions
docker run --rm hmscalc ./runtests.sh

# Run lint
docker run --rm hmscalc ./lint.sh

📦 Project Structure

hmscalc/
├── Dockerfile         # Docker setup with pyenv and poetry
├── runtests.sh        # Runs tests on multiple Python versions
├── hmscalc/           # Source code
│   ├── hms_time.py
│   └── exceptions.py
├── tests/             # Pytest-based unit tests
├── pyproject.toml     # Poetry config
├── README.md          # This file
└── LICENSE            # MIT license

📚 Usage (inside container)

Import

from hmscalc import HMSTime
# or
from hmscalc.hms_time import HMSTime

Basic Operations

a = HMSTime("1:30:15")
b = HMSTime("2:15:45")

print(a + b)            # "3:46:00"
print(a - b)            # "-0:45:30"
print(a > b)            # False
print(a.to_seconds())   # 5415
print(a.to_minutes())   # 90.25
print(a.to_hours())     # 1.504...
print(a.to_tuple())     # (1, 30, 15)
print(a.to_dict())      # {'hh': 1, 'mm': 30, 'ss': 15, 'negative': False}

Negative Values

t = HMSTime("-1:00:00")
print(t.to_seconds())   # -3600
print(t.is_negative)    # True
print(t.to_dict())      # {'hh': 1, 'mm': 0, 'ss': 0, 'negative': True}

Sum / Average / Min / Max

times = [
    HMSTime("1:30:15"),
    HMSTime("2:15:45"),
    HMSTime("0:45:30"),
]

print(HMSTime.sum(times))      # "4:31:30"
print(HMSTime.average(times))  # "1:30:30"
print(HMSTime.min(times))      # "0:45:30"
print(HMSTime.max(times))      # "2:15:45"
print(HMSTime.sum([]))         # "0:00:00"

timedelta Integration

import datetime

t = HMSTime("1:30:15")
delta = t.to_timedelta()                          # datetime.timedelta(...)
restored = HMSTime.from_timedelta(delta)          # HMSTime("1:30:15")

Error Handling

from hmscalc import InvalidTimeFormatError, NotTimeStringError

try:
    HMSTime("1:99:00")   # minute >= 60
except InvalidTimeFormatError:
    pass

try:
    HMSTime(123)         # non-string input
except NotTimeStringError:
    pass

📋 Input Rules

  • Formats: HH:MM or HH:MM:SS (hours may exceed 24 — duration model)
  • Minutes and seconds must be in the range 0–59
  • Optional leading - for negative durations
  • Input must be a string

🔍 Examples

Basic Arithmetic

HMSTime("2:30") + HMSTime("1:45")     # "4:15:00"
HMSTime("1:00:30") - HMSTime("0:30")  # "0:30:30"
HMSTime("0:00") - HMSTime("0:01")     # "-0:01:00"

Work Time Calculation

work_sessions = [
    HMSTime("2:15:30"),
    HMSTime("1:45:00"),
    HMSTime("0:30:15"),
]
total_work = HMSTime.sum(work_sessions)
print(f"Total work time: {total_work}")  # "4:30:45"

🧪 Running tests locally via Docker

# Build image
docker build -t hmscalc .

# Run matrix tests via pyenv + poetry
docker run --rm hmscalc ./runtests.sh

📄 License

This project is licensed under the MIT License.

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

hmscalc-0.3.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

hmscalc-0.3.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file hmscalc-0.3.0.tar.gz.

File metadata

  • Download URL: hmscalc-0.3.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.11.15 Linux/6.17.0-1015-azure

File hashes

Hashes for hmscalc-0.3.0.tar.gz
Algorithm Hash digest
SHA256 802897656d25febaf16e5a064e9c402e5ea14846a6efb03f14f31f15b26521cb
MD5 8eceb5ef007687353a1435b59dea18ca
BLAKE2b-256 4ab9fb52bb853eb974bd3b428364ecda3de1bad17b0866d11915cdea436bf132

See more details on using hashes here.

File details

Details for the file hmscalc-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: hmscalc-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.11.15 Linux/6.17.0-1015-azure

File hashes

Hashes for hmscalc-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 04921c0306c89c442c1478ae5f435a297090c7e1d10c6d67c24b845b5320d900
MD5 0154e6e71d4fa1baf8dc76e122e239e6
BLAKE2b-256 5a319a2623856558172dfd6b57f21f3a4954706b98699afe9bc18cf1bb851b4f

See more details on using hashes here.

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