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

Uploaded Source

Built Distribution

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

temporals-0.1.6-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for temporals-0.1.6.tar.gz
Algorithm Hash digest
SHA256 18eb349d57f010fb0072b4c40cdf8d06a340dc8e1b78361acca7dbe4cc8dcde6
MD5 11a1f770f7c40dc0b2a02c9381629e91
BLAKE2b-256 b485395a87aade016fd4732aa6a163988284696b22d356f821ca4bb04bdf0821

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for temporals-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 6b651aebb162a2e7f4b721c5baa943a73dc35b4f769cd24ba475dbdccb4af14a
MD5 87050f89c1c1b3fbe19b6e915d9345d5
BLAKE2b-256 26fc316effd907cef0129bced22ccf4261de5782de4ffc788585c29e7afeb1f1

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