Skip to main content

Time ranges.

Project description

Discord

Badges
Build Python package semantic-release PyPI Read the Docs
Tests coverage pre-commit
Standards SemVer 2.0.0 Conventional Commits
Code Code style: black Imports: isort Checked with mypy
Repo GitHub issues GitHub stars GitHub license All Contributors Contributor Covenant

timeranges

Time ranges.

Read the Docs

Installation

pip

timeranges is available on pip:

pip install timeranges

GitHub

You can also install the latest version of the code directly from GitHub:

pip install git+git://github.com/MicaelJarniac/timeranges

Usage

For more examples, see the full documentation.

TimeRange

from datetime import time

from timeranges import TimeRange


# Create a `TimeRange` instance with the interval "0:00 -> 10:00"
time_range = TimeRange(time(0), time(10))

# Check if these times are contained in `time_range`
assert time(0) in time_range
assert time(5) in time_range
assert time(10) in time_range

# Check if these times aren't contained in `time_range`
assert time(10, 0, 1) not in time_range
assert time(11) not in time_range
assert time(20) not in time_range

TimeRanges

from datetime import time

from timeranges import TimeRange, TimeRanges


# Create some `TimeRange` instances
time_range_1 = TimeRange(time(0), time(10))
time_range_2 = TimeRange(time(15), time(20))

# Create a `TimeRanges` instance containing multiple `TimeRange`
time_ranges = TimeRanges([time_range, time_range_2])

assert time(0) in time_ranges
assert time(5) in time_ranges
assert time(10) in time_ranges
assert time(12) not in time_ranges
assert time(15) in time_ranges
assert time(17) in time_ranges
assert time(20) in time_ranges
assert time(22) not in time_ranges

WeekRange

from datetime import time, datetime

from timematic.enums import Weekday
from timeranges import TimeRange, TimeRanges, WeekRange

week_range = WeekRange(
    {
        Weekday.MONDAY: TimeRanges(
            [
                TimeRange(time(5), time(10)),
                TimeRange(time(12), time(14)),
            ]
        ),
        Weekday.SATURDAY: TimeRanges(
            [
                TimeRange(time(0), time(2)),
                TimeRange(time(4), time(8)),
            ]
        ),
    }
)

assert datetime(2021, 12, 6, 5, 0, 0) in week_range
assert datetime(2021, 12, 6, 8, 0, 0) in week_range
assert datetime(2021, 12, 6, 10, 0, 0) in week_range
assert datetime(2021, 12, 6, 11, 0, 0) not in week_range
assert datetime(2021, 12, 7, 5, 0, 0) not in week_range
assert datetime(2021, 12, 13, 5, 0, 0) in week_range

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

More details can be found in CONTRIBUTING.

Contributors ✨


Micael Jarniac

🐛 💻 📖 💡 🤔 🚧 📆 👀 🔧 ⚠️

License

MIT

Created from cookiecutter-python-project.

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

timeranges-1.0.2.tar.gz (10.9 kB view details)

Uploaded Source

File details

Details for the file timeranges-1.0.2.tar.gz.

File metadata

  • Download URL: timeranges-1.0.2.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/37.1 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.64.1 importlib-metadata/4.12.0 keyring/23.9.1 rfc3986/2.0.0 colorama/0.4.5 CPython/3.9.14

File hashes

Hashes for timeranges-1.0.2.tar.gz
Algorithm Hash digest
SHA256 aa16eb99409b556301b3aa8d5624f35f638ef3f0e198d110aa01b7968d961738
MD5 2313fa70b694ad996a7244bf00925e2f
BLAKE2b-256 d06a07225541255d797f2313bccfd6a0d6b90f6b7758b512316c4a4d7916c1a9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page