Skip to main content

philiprehberger-timeago

Tests PyPI version Last updated

Convert timestamps to relative time phrases like "3 hours ago".

Installation

pip install philiprehberger-timeago

Usage

from philiprehberger_timeago import timeago, timedelta_human
from datetime import datetime, timedelta, timezone

now = datetime.now(timezone.utc)

timeago(now - timedelta(seconds=30))  # "30 seconds ago"
timeago(now - timedelta(hours=3))     # "3 hours ago"
timeago(now - timedelta(days=1))      # "yesterday"
timeago(now + timedelta(days=7))      # "in 1 week"

# Unix timestamps
timeago(1709913600)

# Duration formatting
timedelta_human(timedelta(hours=3, minutes=25))  # "3 hours, 25 minutes"

Compact age

from philiprehberger_timeago import format_age
from datetime import datetime, timedelta, timezone

now = datetime.now(timezone.utc)

format_age(now - timedelta(seconds=5))    # "5s"
format_age(now - timedelta(minutes=3))    # "3m"
format_age(now - timedelta(hours=2))      # "2h"
format_age(now - timedelta(days=4))       # "4d"
format_age(now + timedelta(hours=1))      # "-1h" (future)

Numeric mode

from philiprehberger_timeago import timeago
from datetime import datetime, timedelta, timezone

now = datetime.now(timezone.utc)

timeago(now - timedelta(seconds=5), numeric=True)  # "5 seconds ago" (not "just now")
timeago(now - timedelta(days=1), numeric=True)     # "1 day ago" (not "yesterday")
timeago(now + timedelta(days=1), numeric=True)     # "in 1 day" (not "tomorrow")

API

Function / Class Description
timeago(dt, now=None, numeric=False) Relative time phrase from datetime, date, or Unix timestamp; numeric=True forces numeric phrasing
format_age(dt, now=None) Compact age string like 5s, 3m, 2h, 4d, 1mo, 2y (future prefixed with -)
timedelta_human(td) Format a timedelta as readable duration

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

⭐ Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Release files for philiprehberger-timeago 0.2.0

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

Source distribution (sdist)

Source distribution for philiprehberger-timeago 0.2.0
File Size Uploaded
philiprehberger_timeago-0.2.0.tar.gz 183.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for philiprehberger-timeago 0.2.0
File Interpreter ABI Platform
philiprehberger_timeago-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 188.2 kB

Release files / philiprehberger_timeago-0.2.0.tar.gz

Download URL philiprehberger_timeago-0.2.0.tar.gz
Size 183.1 kB
Tags Source
SHA-256 checksum
How to use checksums
30f57163dd59612f49d95552f85da8f593249aa5ce32caec74128550d2229fb4
BLAKE2b-256 checksum
How to use checksums
fa51689192b6a4684ef9bf96c2465f45224dda33cee0f7551ff73c11a44c646a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / philiprehberger_timeago-0.2.0-py3-none-any.whl

Download URL philiprehberger_timeago-0.2.0-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6dae389807c740a3d1e988c8027e0122b2a3049ec1c52c8dd57c605a16e51321
BLAKE2b-256 checksum
How to use checksums
8b5c80b340a6276bbce59fdc7a2e13bd3fde84de9ccc2960af987ddc5d63e346
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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