A flexible library to convert various time inputs into a total duration format (HH:MM:SS).
Project description
Total Time Formatter
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
datetimestrings, time-only strings (HH:MM:SS), and nativetimedeltaobjects. - 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file total_time_formatter-1.0.0.tar.gz.
File metadata
- Download URL: total_time_formatter-1.0.0.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5058d3d96baf9b568c9a21694be0c26ae48179e66e7eb2aec8271765bcbf2883
|
|
| MD5 |
5decc43fd341f22eb6372aa047021c5d
|
|
| BLAKE2b-256 |
6c868eebf948b9dda54483c8b97de66242a1377cbfb2054b4f3fcc0a3de04f25
|
File details
Details for the file total_time_formatter-1.0.0-py3-none-any.whl.
File metadata
- Download URL: total_time_formatter-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
133b40ad3d0b2e44027aaf135aaf1eefa3eb6dec972adb12058da9e1d9da2aaa
|
|
| MD5 |
26c95dc2e535fd2a2e581334900213c1
|
|
| BLAKE2b-256 |
c14e26ed52cfed357c91cdfc39e2448ae8670b33e6bd2d7d26bbc95ccffd8e89
|