Skip to main content

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.

  • seconds must be a non-negative integer

Properties

  • hours → number of hours
  • minutes → remaining minutes
  • seconds → remaining seconds
  • hours_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 human_seconds.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

human_seconds-0.1.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

human_seconds-0.1.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file human_seconds-0.1.1.tar.gz.

File metadata

  • Download URL: human_seconds-0.1.1.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

Hashes for human_seconds-0.1.1.tar.gz
Algorithm Hash digest
SHA256 451b354bb0c5f3f1e86c09cb42f68074dd096df8598c18f0031392d8a950c196
MD5 71908966e75ccae83a5be5dd61c1625e
BLAKE2b-256 7f445118108157f10a5de2982694b5ff2c52b825551b723758cb33be363c0d4b

See more details on using hashes here.

File details

Details for the file human_seconds-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: human_seconds-0.1.1-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

Hashes for human_seconds-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 82b79c8aacc82ff8d7c2d42c2189f33d13ba0659c47e82d7910572432dd2ca5e
MD5 79ec49b895188d7891a6e95bab4a9397
BLAKE2b-256 538a3b32ba7e7a8c2e0b76ee6e054fcf17a7b25065f2c1fa23cd4a14ca7039e0

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