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.converter 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.2.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.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: human_seconds-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 9d706d6db47c19c6ee5a98e94e3f7fc7aaeaea6d813b823aa320f925d2f1e594
MD5 60c8a0ae657bb94a52f01f47a35fb947
BLAKE2b-256 4f594b71630bc931668418077499c1a53de8e77c21ed0205391ee93a2e7cad24

See more details on using hashes here.

File details

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

File metadata

  • Download URL: human_seconds-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0bf43272b8c93e8c00f8748b5e426357bbdcc63db6b7a895488b26a7b60a017d
MD5 73e268a845395c7523f0679f235866f3
BLAKE2b-256 6dafff3871512df02d0afdee2fb293e5d818f00f4594b26e881c758df56d708e

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