Convert seconds to a human-readable time string
Project description
Human Seconds
Convert a number of seconds into a human-readable time string.
For example, 3675 seconds becomes:
"1 hour, 1 minute and 15 seconds"
This package is ideal for scripts, applications, or tools where you want to display time in a natural, readable format.
Why?
Working with raw seconds isn’t always user-friendly. Whether you’re logging durations, displaying timers, or showing elapsed time to users, converting seconds into a clear, natural sentence improves readability and user experience.
Human Seconds handles this conversion for you cleanly, correctly, and with proper pluralisation, so you can focus on your application logic instead of formatting time.
Features
- Convert seconds into hours, minutes, and seconds
- Output a clean, human-readable string
- Correct handling of singular and plural units
- Small, lightweight, and dependency-free
Technologies
- Python >= 3.9
Installation
Install via pip:
pip install human-seconds
Usage
Basic usage
from human_seconds import SecondsToTime
time = SecondsToTime(3675)
print(time.format_to_human_readable())
# "1 hour, 1 minute and 15 seconds"
time = SecondsToTime(62)
print(time.format_to_human_readable())
# "1 minute and 2 seconds"
Accessing individual components
time = SecondsToTime(3675)
print(time.hours) # 1
print(time.minutes) # 1
print(time.seconds) # the total number of seconds
print(time.hours_minutes_and_seconds) # (1, 1, 15)
print(time.format_to_human_readable()) # "1 hour, 1 minute and 15 seconds"
Updating the total seconds via seconds
You can update the total number of seconds after initialisation. The value will be normalised automatically into hours, minutes, and seconds.
time = SecondsToTime(3675) # original: 1 hour, 1 minute, 15 seconds
time.seconds = 100 # update total seconds
print(time.hours) # 0
print(time.minutes) # 1
print(time.seconds) # the total number of seconds
print(time.hours_minutes_and_seconds) # (0, 1, 40)
print(time.format_to_human_readable()) # "1 minute and 40 seconds"
API overview
SecondsToTime(total_seconds: int)
Creates a new time object from a total number of seconds.
secondsmust be a non-negative integer
Properties
hours→ number of hoursminutes→ remaining minutesseconds→ remaining secondshours_minutes_and_seconds→(hours, minutes, seconds)tuple
Methods
format_to_human_readable()→ returns a human-readable time string
Edge cases
SecondsToTime(0).format_to_human_readable()
# ""
SecondsToTime(3600).format_to_human_readable()
# "1 hour"
Negative values are not supported.
Requirements
- Python 3.9 or higher
Development
Clone the repository:
git clone https://github.com/EgbieAndersonUku1/human_seconds.git .
cd human-seconds
Example usage
from converter import SecondsToTime
time = SecondsToTime(3675)
print(time.hours) # 1
print(time.minutes) # 1
print(time.seconds) # 15
print(time.format_to_human_readable()) # '1 hour, 1 minute and 15 seconds'
# Use the `seconds` setter to update the remaining seconds;
# the total time will be automatically normalized
time.seconds = 100
print(time.hours) # 0
print(time.minutes) # 1
print(time.seconds) # 40
print(time.format_to_human_readable()) # '1 minute and 40 seconds'
Licence
MIT Licence
Project details
Release history Release notifications | RSS feed
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 human_seconds-0.1.0.tar.gz.
File metadata
- Download URL: human_seconds-0.1.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0cfd07b80abca14935dd7b765af451cb96a86b41ee1f045672a401b36b7ca0f2
|
|
| MD5 |
e8fb550b92e7ca356bc9a4089c93e818
|
|
| BLAKE2b-256 |
dac489fbbc586647bffadd33f763bfceb5e7b0a67b3d328b58b240b298364772
|
File details
Details for the file human_seconds-0.1.0-py3-none-any.whl.
File metadata
- Download URL: human_seconds-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
847bb7f721ccecc9ed2f95c73485f08f742ee7a505d8a01b6e7ed86a759a09d3
|
|
| MD5 |
6fe504b0572d0fbdd504aebf80ba5a25
|
|
| BLAKE2b-256 |
ea24ed6a7808a0edb09ffa9fb7a8ad506c05d9e9089995a8990b9a6240dd9cf2
|