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.1.0.tar.gz (28.1 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.1.0-py3-none-any.whl (33.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for temporals-3.1.0.tar.gz
Algorithm Hash digest
SHA256 3aafa79757c7ad63964811584018793c3f27fb4a41439906bb87ed4f73f59285
MD5 26156d0235767b4db03087b5c99dd2a3
BLAKE2b-256 f0182ee08731769f5c1f9f29f9366362bec731220727778bc5626e4d963ea254

See more details on using hashes here.

File details

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

File metadata

  • Download URL: temporals-3.1.0-py3-none-any.whl
  • Upload date:
  • Size: 33.1 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d918f368808324760461e4746757e0031d7618d52d6ac6a7ba60e976316cd6e
MD5 74b98452228d240f7e06bc73b46b7946
BLAKE2b-256 f9b4eb13d8ca747c31e6dfadbb79f0cfa5b0ccdfc45f797d29db478de4d01683

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