Convert timestamps to relative time phrases like '3 hours ago'
Project description
philiprehberger-timeago
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:
License
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)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30f57163dd59612f49d95552f85da8f593249aa5ce32caec74128550d2229fb4
|
|
| MD5 |
02bddb45da31ad4fb5e50a9912ecea17
|
|
| BLAKE2b-256 |
fa51689192b6a4684ef9bf96c2465f45224dda33cee0f7551ff73c11a44c646a
|
File details
Details for the file philiprehberger_timeago-0.2.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_timeago-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6dae389807c740a3d1e988c8027e0122b2a3049ec1c52c8dd57c605a16e51321
|
|
| MD5 |
975f99ddce41ed55c56ce4c2b9a80803
|
|
| BLAKE2b-256 |
8b5c80b340a6276bbce59fdc7a2e13bd3fde84de9ccc2960af987ddc5d63e346
|