Skip to main content

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

Project description

Time Formatting Tools

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

Python Version PyPI Version

GitHub Build Coveralls Coverage 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

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

timefmt-1.1.8.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

timefmt-1.1.8-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file timefmt-1.1.8.tar.gz.

File metadata

  • Download URL: timefmt-1.1.8.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for timefmt-1.1.8.tar.gz
Algorithm Hash digest
SHA256 af9ca5438d0c253fc72194fc965e99fccfdb36dc8a372fffece0dc52040f47ba
MD5 c4b6810c64338dfc01c85c791bcaa659
BLAKE2b-256 5874893738dc17bef209cea52e962af1aaf18c64029d53f621a5b24fa3788307

See more details on using hashes here.

File details

Details for the file timefmt-1.1.8-py3-none-any.whl.

File metadata

  • Download URL: timefmt-1.1.8-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for timefmt-1.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 32cda7aeaf8fc1c10328b27de054e04ccc61aa8345135f7c069c84903965f196
MD5 7cf6e8e430f4c5305c9cd610c32f1d31
BLAKE2b-256 99782c75ef1ab38de651766f82b1b30c8f267f569eeab249a924cd1879dafb52

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page