Skip to main content

A flexible library to convert various time inputs into a total duration format (HH:MM:SS).

Project description

Total Time Formatter

PyPI Version

A simple yet powerful Python library to convert various time inputs into a total duration format (HH:MM:SS), where hours can exceed 24.

This tool is perfect for applications that need to calculate and display total durations, such as project time tracking, equipment runtime, or simply formatting time data consistently.

Key Features

  • Flexible Inputs: Handles datetime strings, time-only strings (HH:MM:SS), and native timedelta objects.
  • Total Hour Calculation: Correctly calculates total hours from inputs that include date information, allowing hour counts beyond 24.
  • High Precision Control: Offers precise control over how fractional seconds are handled: truncate, round up, or keep the exact original precision down to the microsecond.
  • Dependency-Free: Pure Python with no external dependencies.

Installation

Install the library directly from PyPI:

pip install total-time-formatter

How to Use

Import the main function and the precision mode constants to get started.

from total_time_formatter import format_total_hours, TRUNCATE, ROUND_UP, KEEP_PRECISION
from datetime import timedelta

1. Converting a Datetime String

When you provide a full datetime string, the library calculates the total duration from a default reference date (1900-01-01).

date_str = "1900-01-03 10:30:15" 
# Duration = 2 full days (48h) + 10h = 58 hours

duration = format_total_hours(date_str)

print(f"Input: '{date_str}'")
print(f"Total Duration: {duration}")
# Expected Output: Total Duration: 58:30:15

2. Converting a Time-Only String (Duration)

If you provide a string that already represents a duration, it will be formatted correctly.

duration_str = "48:25:10"
formatted = format_total_hours(duration_str)

print(f"Input: '{duration_str}'")
print(f"Formatted Duration: {formatted}")
# Expected Output: Formatted Duration: 48:25:10

3. Converting a timedelta Object

The function can also directly format timedelta objects.

td_object = timedelta(hours=75, minutes=5, seconds=22)
formatted = format_total_hours(td_object)

print(f"Input: {td_object}")
print(f"Formatted Duration: {formatted}")
# Expected Output: Formatted Duration: 75:05:22

4. Controlling Precision

This is where the library shines. You can control how to handle fractional seconds using the precision_mode argument.

time_with_ms = "10:20:30.789123"

# a) Truncate (default behavior)
truncated = format_total_hours(time_with_ms, precision_mode=TRUNCATE)
print(f"Truncated: {truncated}")
# Expected Output: Truncated: 10:20:30

# b) Round Up
rounded = format_total_hours(time_with_ms, precision_mode=ROUND_UP)
print(f"Rounded Up: {rounded}")
# Expected Output: Rounded Up: 10:20:31

# c) Keep Exact Precision
precise = format_total_hours(time_with_ms, precision_mode=KEEP_PRECISION)
print(f"Precise: {precise}")
# Expected Output: Precise: 10:20:30.789123

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

total_time_formatter-1.0.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

total_time_formatter-1.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file total_time_formatter-1.0.1.tar.gz.

File metadata

  • Download URL: total_time_formatter-1.0.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for total_time_formatter-1.0.1.tar.gz
Algorithm Hash digest
SHA256 b8ad2f91830a948ffc09c05fffc1e536564f1d1c2a93427d939b5e4dde1b8552
MD5 f8ef4411349f9476b1cafa46a071c1c7
BLAKE2b-256 ea69bbb1a5e2ab8fc4a87399f9c5d670e9622554f51312732da96cd32da5d9bc

See more details on using hashes here.

File details

Details for the file total_time_formatter-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for total_time_formatter-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a250fa17d7c92ff3562c7d4bdad11de7b75edc614dda5ba9a29b93024432a807
MD5 1aeeae3965f70cba4a0c9cff554496e0
BLAKE2b-256 c019472600c89cce97be3e6dfd2117ccea28d8c85ff635d84830cd3888b16c77

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