Skip to main content

Python library providing time, date and datetime periods support

Project description

Temporals

The goal of this library is to provide a minimalistic, out-of-the-box utility on top of the Python's datetime in regards to working with time and date, or both, periods.

Installation

You can either download the latest package from this repository or via pip:

pip install temporals

Documentation

Full documentation is available in the wiki of this repository.

Quickstart

The library offers 4 types of periods - time, date, wall clock and absolute. For the sake of example, we'll keep it simple here and just define a time period that we'll call our work day:

from datetime import time
from temporals.pydatetime import TimePeriod

workday = TimePeriod(start=time(8, 0), end=time(18, 0))
# You can use a membership check to see if a point in time or another period exists within one that you've already defined
lunch= time(12, 0)
print(lunch in workday)  # True
# Or you can see if two periods overlap
dentist_slot = TimePeriod(start=time(17, 30), end=time(18, 30))
print(dentist_slot.overlaps_with(workday)) # True
# You can also get the precise amount of overlap/disconnect
# both of these return a new TimePeriod object :)
print(dentist_slot.get_overlap(workday)) # 17:30:00/18:00:00
print(dentist_slot.get_disconnect(workday)) # 18:00:00/18:30:00

If 24h long day isn't what you're looking for, the DatePeriod will fill the need for having periods with no specific time info:

from datetime import date
from temporals.pydatetime import DatePeriod

vacation = DatePeriod(start=date(2025, 8, 1), end=date(2025, 8, 14))
# Each period has a `.duration` attribute which returns an instance of the Duration class:
vacation.duration # Duration(total_seconds=1123200, years=0, months=0, days=13, hours=0, minutes=0, seconds=0)
# A simpler way would be to just call the isoformat method of the Duration class:
print(vacation.duration.isoformat(fold=True)) # 'P13D'

For the greatest precision, you could use one of the two classes - WallClockPeriod or AbsolutePeriod. The difference between the two is, as you can probably guess, that the duration of the WallClockPeriod's class will be as if tracked by looking at a clock on the wall; the same for the AbsolutePeriod's class, however, will measure the absolute amount of time thath as passed, irrelevant of the time shifts that may happen throughout its duration.

from datetime import time, date
from temporals.pydatetime import TimePeriod, DatePeriod

vacation = DatePeriod(start=date(2025, 8, 1), end=date(2025, 8, 14))
# Let's be a bit more specific with our vacation
flight_duration = TimePeriod(start=time(7, 55), end=time(14, 24))
# The start of the time period above will be matched as a start to the DatePeriod and the same will be done for the end
italy_visit = vacation.to_wallclock(specific_time=flight_duration)
# WallClockPeriod(start=datetime.datetime(2025, 8, 1, 7, 55), end=datetime.datetime(2025, 8, 14, 14, 24))
# Our duration looks a bit more complete now:
print(italy_visit.duration.isoformat(fold=True)) # 'P13DT6H29M'
# that, however, doesn't necessarily make it readable, so let's update it:
sentence: str = f"Our total time spent travelling to and from, as well as our stay there, Italy, took %d days, %H hours and %M minutes"
print(italy_visit.duration.format(sentence))
# Our total time spent travelling to and from, as well as our stay there, Italy, took 13 days, 6 hours and 29 minutes

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

temporals-3.0.0.tar.gz (30.4 kB view details)

Uploaded Source

Built Distribution

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

temporals-3.0.0-py3-none-any.whl (35.6 kB view details)

Uploaded Python 3

File details

Details for the file temporals-3.0.0.tar.gz.

File metadata

  • Download URL: temporals-3.0.0.tar.gz
  • Upload date:
  • Size: 30.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.4

File hashes

Hashes for temporals-3.0.0.tar.gz
Algorithm Hash digest
SHA256 2e2522935c9c431e772de1710ec988890f0c99abe68e269a545bfabb8cc4b077
MD5 0455202bccf70e40509849dc0ba4af2e
BLAKE2b-256 015867d724d00736eb30c4fbcfb250787c089bbf3d6573705a70e55902e07bfd

See more details on using hashes here.

File details

Details for the file temporals-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: temporals-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 35.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.4

File hashes

Hashes for temporals-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0450c062f011f9536ca1bc3daddfec6e49f1184564c16cc8064b908e51d700a6
MD5 471e6d6e026975047b0acd257276353b
BLAKE2b-256 b72a7652738b57b7d7cdf47a4ecdec1313d77a86a127402e9b28165cb233f378

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