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.

:warning: As it currently stands (assume the latest release), the library has no out of the box support for "aware" datetime objects. All the calculations are done by the Python datetime library.

Quickstart

Installation

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

pip install temporals

Periods

The library offers access to three different types of periods:

  • TimePeriod

    The TimePeriod class allows you to define a period using datetime.time, and as such, is limited to just the 24 hours within a day.

  • DatePeriod

    The DatePeriod class allows you to define a period using datetime.date and work with any dates within its bounds.

  • DatetimePeriod

    This is the most complete implementation of both periods above as it contains both a date, and a time, defined using datetime.datetime.

Documentation

In-depth documentation available on the Wiki page of the repository - https://github.com/dimitarOnGithub/temporals/wiki

Examples

As mentioned above, you might find use for the different periods in different situations, let's look at the following TimePeriod example:

from datetime import time
from temporals import TimePeriod

workday = TimePeriod(start=time(8, 0), end=time(18, 0))
# Do you have an important call scheduled?
client_call = time(13, 0)
print(client_call in workday)  # True

# Or maybe you have an appointment and you want to see if it overlaps with your work hours?
appointment = TimePeriod(start=time(17, 30), end=time(18, 15))
appointment.overlaps_with(workday)  # True

# Let's see what's the overlap like, so you can plan accordingly
overlap = appointment.get_overlap(workday)  # get_overlap returns a TimePeriod
print(overlap)  # 17:30:00/18:00:00

Let's do something less day-to-day like, with this DatePeriod:

from datetime import date
from temporals import DatePeriod

time_off = DatePeriod(start=date(2024, 8, 1), end=date(2024, 8, 14))
# All roads lead to Rome
italy_visit = DatePeriod(start=date(2024, 8, 3), end=date(2024, 8, 10))

# I hope you have a good... how long again?
print(italy_visit.duration)  # P0Y0M1W0DT0H0M0S
# Wow, that sure is a mouthful, can we get it simplified?
print(italy_visit.duration.isoformat(fold=True))  # 'P1W'

Sometimes, we need both date and time to properly describe a period in time and this is the purpose DatetimePeriod serves

from datetime import datetime
from temporals import DatetimePeriod

takeoff = datetime(2024, 8, 2, 22, 11, 34)  # 22:11:34, 2nd of Aug 2024
landing = datetime(2024, 8, 3, 4, 55, 3)  # 04:55:03, 3rd of Aug 2024

flight = DatetimePeriod(start=takeoff, end=landing)
print(flight)  
# 2024-08-02T22:11:34/2024-08-03T04:55:03

# This is a PITA to read, why not try something nicer
print(flight.duration.isoformat())
# PT6H43M29S

# "Stop. Enhance. Give me a hard copy right there."
pattern = "%Hh%Mm"
formatted_time = flight.duration.format(pattern)
sentence = f"The flight lasted around {formatted_time}"
print(sentence)
# The flight lasted around 6h43m

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-1.0.0.tar.gz (26.8 kB view details)

Uploaded Source

Built Distribution

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

temporals-1.0.0-py3-none-any.whl (31.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for temporals-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4957031cc0496b474ebe1b6deb25582ef5e1724cb506e5016f6b74fbd84d6736
MD5 b1b2f4ac593a3d6fa0551281dbe6f927
BLAKE2b-256 0767b18ef0b041cd0a4935bda230340e723c80fa92d47aefcb99d50c855a8d73

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for temporals-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef282adcc990b5fe0bbaaece2d98a003ca58483368dc8f5127156325480f97a3
MD5 00b982fa04b4d711079be66896124fc6
BLAKE2b-256 e111a240312785e3d1036e9b76e424bf8d00ef72c2c6355992343b2dd2438ea4

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