Skip to main content

Time Formatting Tools

Provides methods to manipulate and format various time-based objects into human-readable strings.

Python Version PyPI Version

GitHub Build Codecov Coverage Codacy Badge

License PyPI Downloads

Why?

The datetime standard package in Python is very useful for working with times programmatically, but it only provides basic text formatting. Additionally, other packages which add time formatting methods like pendulum use non-standard datetime-like objects, and they have a lot of extra features I didn't need.

While it's not too hard to define some helper functions to format time into human-readable text, it becomes annoying when you have to copy the functions into every project you make with time formatting.

timefmt solves this by providing lightweight helper functions written entirely in Python that handle formatting and manipulating time-based objects. The code is pre-tested and easy to install with pip, meaning you can get up and going with nicely formatted time strings without any hassle.

Usage

import datetime
import timefmt  # Gives access to the auto() function (recommended)
import timefmt.dt  # Gives access to datetime-specific functions
import timefmt.td  # Gives access to timedelta-specific functions

now = datetime.datetime.now()

print("Now (short):", timefmt.dt.short(now))
print("Now (long):", timefmt.dt.long(now))

since_epoch = datetime.timedelta(seconds=now.timestamp())

print("Time since Jan. 1st, 1970 (short):", timefmt.td.short(since_epoch))
print("Time since Jan. 1st, 1970 (long):", timefmt.td.long(since_epoch))

# You can also automatically detect which type it is, like so
import random

random_choice = random.choice([now, since_epoch])

print("Unknown time format value (short):", timefmt.auto(random_choice))
print("Unknown time format value (long):", timefmt.auto(random_choice, long=True))

This prints the following:

Now (short): 11:12:12 AM
Now (long): 11:12:12 AM MST
Time since Jan. 1st, 1970 (short): 2817W 2D 18:12:12
Time since Jan. 1st, 1970 (long): 2817 weeks, 2 days, 18 hours, 12 minutes, and 12 seconds
Unknown time format value (short): 11:12:12 AM
Unknown time format value (long): 11:12:12 AM MST

Full Documentation

timefmt on Read the Docs

Metadata

Release files for timefmt 1.1.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for timefmt 1.1.11
File Size Uploaded Provenance Actions
timefmt-1.1.11.tar.gz 17.3 kB Without provenance Details for timefmt-1.1.11.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for timefmt 1.1.11
File Interpreter ABI Platform Actions
timefmt-1.1.11-py3-none-any.whl Python 3 none any Details for timefmt-1.1.11-py3-none-any.whl

Total release size: 36.4 kB

Release files / timefmt-1.1.11.tar.gz

Download URL timefmt-1.1.11.tar.gz
Size 17.3 kB
Tags Source
SHA-256 checksum
How to use checksums
6e48f8d98178bd9827838948a7b2d1283e6c47ab438da35b6fff464cf1f9dc46
BLAKE2b-256 checksum
How to use checksums
fb24ae7db4e028801ab76f6ff46d5ac3e128206f1d5383b61ea10794bbabcaf7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / timefmt-1.1.11-py3-none-any.whl

Download URL timefmt-1.1.11-py3-none-any.whl
Size 19.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
61bba2e44ed03464121d9f9709f78c1d8b79f17e598a79dd216dd28eaff17f22
BLAKE2b-256 checksum
How to use checksums
482b5aa012cc15241ee26466cd7d10969f90433626ae42015483899ee33dbe51
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Security for timefmt 1.1.11

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page