Skip to main content

Easily manipulate datetime objects with relative time modifiers. Snap, shift, and chain operations on timezone-aware datetimes. E.g `@d-2h`

Project description

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.

[!NOTE] This package was created as a more up to date replacement for zartstrom/snaptime, which is long since abandoned.

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()

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

python_snaptime-0.1.2.dev0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

python_snaptime-0.1.2.dev0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file python_snaptime-0.1.2.dev0.tar.gz.

File metadata

  • Download URL: python_snaptime-0.1.2.dev0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.11 Linux/6.5.0-1025-azure

File hashes

Hashes for python_snaptime-0.1.2.dev0.tar.gz
Algorithm Hash digest
SHA256 b6d8e6596a434c983f86e3ae8dd78b0642d926db8a564f7306cf83242ec77d40
MD5 dba07de0bb71a0acd681afa5d49c3666
BLAKE2b-256 b1160447e9d5897e2bf32d4fb014a57fb81f8b981036838e0a9bc282c5a34e90

See more details on using hashes here.

File details

Details for the file python_snaptime-0.1.2.dev0-py3-none-any.whl.

File metadata

  • Download URL: python_snaptime-0.1.2.dev0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.11 Linux/6.5.0-1025-azure

File hashes

Hashes for python_snaptime-0.1.2.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 6ef5d91aed2a1c76b1d8551dd64a46334442725423947c091cccf2ffb45711ec
MD5 63c9cec218932c132f57fcee0b3c1738
BLAKE2b-256 50658f8ed7be72f7e0e18be8a17296f87110046156c7937ca4e1f03e5fad197e

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