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.2.tar.gz (5.2 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.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: total_time_formatter-1.0.2.tar.gz
  • Upload date:
  • Size: 5.2 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.2.tar.gz
Algorithm Hash digest
SHA256 80c33f21650ea608d84c66685ec7b611efd276ed056a2e374f0d2e1ab020bdac
MD5 70fabab618d153d5e808e4ed0f310d3d
BLAKE2b-256 10f8f11ade0adbf20c4ec5d67a5035ca6ac37802b05f8cd8dad0720264ff2dea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for total_time_formatter-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9ff33e0eee64b0b82ff1281eb3740ad8c4aec0ffaff5a90f090d408c09c250e8
MD5 ab0898748e4937f9976111a9c1443299
BLAKE2b-256 fcad486680abf69443d43df5d9dbe184197d1da35c99f9c8981f7711e209ba8a

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