Skip to main content

Convert timestamps to relative time phrases like '3 hours ago'

Project description

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

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

philiprehberger_timeago-0.2.0.tar.gz (183.1 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_timeago-0.2.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_timeago-0.2.0.tar.gz.

File metadata

  • Download URL: philiprehberger_timeago-0.2.0.tar.gz
  • Upload date:
  • Size: 183.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for philiprehberger_timeago-0.2.0.tar.gz
Algorithm Hash digest
SHA256 30f57163dd59612f49d95552f85da8f593249aa5ce32caec74128550d2229fb4
MD5 02bddb45da31ad4fb5e50a9912ecea17
BLAKE2b-256 fa51689192b6a4684ef9bf96c2465f45224dda33cee0f7551ff73c11a44c646a

See more details on using hashes here.

File details

Details for the file philiprehberger_timeago-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_timeago-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6dae389807c740a3d1e988c8027e0122b2a3049ec1c52c8dd57c605a16e51321
MD5 975f99ddce41ed55c56ce4c2b9a80803
BLAKE2b-256 8b5c80b340a6276bbce59fdc7a2e13bd3fde84de9ccc2960af987ddc5d63e346

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