Skip to main content

python-snaptime

PyPI - Version PyPI - Python Version GitHub Actions Workflow Status Codacy coverage Codacy grade

Inspired by Splunk's relative time modifiers, python-snaptime will transform datetime objects using relative time modifiers.

For example, @d-2h will give you two hours ago from the start of the day.

  • Use snaptime strings to get relative dates/times for a given datetime.
  • Timezone aware.
  • Effortlessly handles daylight savings using pendulum.
  • Can snap backwards in time to the nearest second, minute, hour, day, week, month, quarter or year.
  • Can add/subtract microseconds, milliseconds, seconds, minutes, hours, days, weeks, months, quarters or years.
  • Chain snaps together as needed e.g @d-12h+10m@h.
  • Use either a snaptime string, or use Python to define snaptimes (see advanced example).
  • Fully type annotated for IDE completion.

Snaptime strings

Unit Strings Supports Snapping? Supports Delta?
MICROSECOND us, microsecond, microseconds ❌ ✅
MILLISECOND ms, millisecond, milliseconds ❌ ✅
SECOND s, sec, secs, second, seconds ✅ ✅
MINUTE m, min, mins, minute, minutes ✅ ✅
HOUR h, hr, hrs, hour, hours ✅ ✅
DAY d, day, days ✅ ✅
WEEK w, week, weeks ✅ ✅
MONTH mon, month, months ✅ ✅
QUARTER q, qtr, qtrs, quarter, quarters ✅ ✅
YEAR y, yr, yrs, year, years ✅ ✅

Examples

Timezones

Using pendulum timezones are handled easily.

>>> import pendulum
>>> from python_snaptime import snap

>>> dtm = pendulum.datetime(2024, 12, 30, 18, 0, 0)
>>> snap(dtm, "@d-12h")
DateTime(2024, 12, 29, 12, 0, 0, tzinfo=Timezone('UTC'))
>>> import pendulum
>>> from python_snaptime import snap

>>> dtm = pendulum.datetime(2024, 12, 30, 18, 0, 0, tz=pendulum.timezone("Europe/London"))
>>> snap(dtm, "@d-12h")
DateTime(2024, 12, 29, 12, 0, 0, tzinfo=Timezone('Europe/London'))

DST

pendulum makes working around DST easy

>>> import pendulum
>>> from python_snaptime import snap

>>> dtm = pendulum.datetime(2024, 10, 27, 1, 59, 59, tz="Europe/London", fold=0)
>>> snap(dtm, "+1s")
DateTime(2024, 10, 27, 1, 0, 0, tzinfo=Timezone('Europe/London'))  # pre-transition
>>> import pendulum
>>> from python_snaptime import snap

>>> dtm = pendulum.datetime(2024, 10, 27, 1, 59, 59, tz="Europe/London", fold=1)
>>> snap(dtm, "+1s")
DateTime(2024, 10, 27, 2, 0, 0, tzinfo=Timezone('Europe/London'))  # post-transition (default)

datetime

Don't care about timezones/want to use builtin datetime.datetime?

>>> from datetime import datetime
>>> from python_snaptime import snap

>>> dtm = datetime(2024, 12, 30, 18, 0, 0)
>>> snap(dtm, "@d-12h")
datetime.datetime(2024, 12, 29, 12, 0)

Can also work with builtin timezone aware datetimes

>>> from datetime import datetime
>>> from zoneinfo import ZoneInfo
>>> from python_snaptime import snap

>>> dtm = datetime(2024, 12, 30, 18, 0, 0, tzinfo=ZoneInfo("Europe/London"))
>>> snap(dtm, "@d-12h")
datetime.datetime(2024, 12, 29, 12, 0, tzinfo=Timezone('Europe/London'))

Installation

pypi

# using poetry
poetry add python-snaptime

# using pip
pip install python-snaptime

Usage

import pendulum
from python_snaptime import snap

snapped_datetime = snap(pendulum.now(), "@d-2h+10m")

Advanced

You can programmatically calculate snaptimes without a snaptime string, e.g the equivalent of @d-2h+10m is:

import pendulum

from python_snaptime.handlers import handle_timesnapping
from python_snaptime.models import Action, Snaptime, Unit


def main():
    datetime = pendulum.now()
    time_snapping_operations = [
        Snaptime(action=Action.SNAP, unit=Unit.DAY),
        Snaptime(action=Action.SUB, unit=Unit.HOUR, time_int=2),
        Snaptime(action=Action.ADD, unit=Unit.MINUTE, time_int=10),
    ]
    for operation in time_snapping_operations:
        datetime = handle_timesnapping(operation, datetime)
    print(datetime)  # `@d-2h+10m`


if __name__ == "__main__":
    main()

Metadata

Release files for python-snaptime 0.1.2

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

Source distribution (sdist)

Source distribution for python-snaptime 0.1.2
File Size Uploaded
python_snaptime-0.1.2.tar.gz 8.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-snaptime 0.1.2
File Interpreter ABI Platform
python_snaptime-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 15.8 kB

Release files / python_snaptime-0.1.2.tar.gz

Download URL python_snaptime-0.1.2.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
baf0414da2cc8a57640c8d14236d14b14f971649d1245fca8d6a7ffe23b47675
BLAKE2b-256 checksum
How to use checksums
d790056e4784d661d35f5e5e104c01afc6c3330bb5dfdf14fcb902a6c79658c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.11 Linux/6.5.0-1025-azure

Release files / python_snaptime-0.1.2-py3-none-any.whl

Download URL python_snaptime-0.1.2-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
00c2dd993dee05d2c4bf716c4cd7bafe8615de00a9be7ba9eb9f61b096cb18ad
BLAKE2b-256 checksum
How to use checksums
6aef002e43645a9b46692e66fb64d7762a0bc105690ad0f5e20dcd0512d79bca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.11 Linux/6.5.0-1025-azure
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