Skip to main content

Handling of month based time ranges.

Project description

month-range

A Python package for handling and manipulating date ranges at the month level.

Overview

month-range provides a clean, intuitive API for working with date ranges at the month level. It's particularly useful for:

  • Financial reporting periods
  • Academic terms and semesters
  • Business quarters and fiscal years
  • Project timeline management
  • Subscription periods

The package brings the power of Python's standard datetime handling to month-level operations with an easy-to-use, chainable API.

Installation

pip install month-range

Features

  • Create and manipulate month ranges easily
  • Represent individual months, quarters, half-years, and full years
  • Handle date arithmetic (add/subtract months, compare ranges)
  • Calculate intersections and unions of date ranges
  • Detect overlaps and adjacency between ranges
  • Simplify arbitrary ranges into standard periods (months, quarters, etc.)
  • Parse from and convert to various formats (strings, integers, etc.)

Basic Usage

from month_range import Month, MonthRange, Year, QuarterYear, HalfYear

# Create a month (multiple formats supported)
jan_2023 = Month.parse("2023-01")
feb_2023 = Month.parse(202302)  # YYYYMM format
current_month = Month.current()  # Current month

# Create a range
q1_2023 = MonthRange.parse(["2023-01", "2023-03"])
fiscal_year = MonthRange(Month(2023, 4), Month(2024, 3))

# Check if ranges overlap
if q1_2023.overlaps(fiscal_year):
    print("Ranges overlap")
    
# Get intersection
overlap = q1_2023 & fiscal_year  # Using operator
# or
overlap = q1_2023.intersect(fiscal_year)  # Using method

# Get all months in a range
months = fiscal_year.months  # Returns a list of Month objects

# Simplify standard periods
quarter = MonthRange(Month(2025, 1), Month(2025, 3)).simplify()  # Returns a QuarterYear
print(isinstance(quarter, QuarterYear))  # True

# Move ranges forward or backward
next_quarter = q1_2023 + 1  # Q2 2023
previous_quarter = q1_2023 - 1  # Q4 2022

# Compare ranges
if MonthRange(Month(2025, 1), Month(2025, 3)) == QuarterYear(2025, 1):
    print("These are the same period")

Class Hierarchy

  • MonthRange: Base class for all date ranges
    • Month: Represents a single month
    • QuarterYear: Represents a fiscal quarter (3 months)
    • HalfYear: Represents half a year (6 months)
    • Year: Represents a full calendar year (12 months)

Advanced Usage

Working with Custom Business Logic

# Check if a range follows directly after another
q1 = QuarterYear(2023, 1)
q2 = QuarterYear(2023, 2)

if q2.follows_directly(q1):
    print("Q2 follows Q1 directly")

# Check containment
fiscal_year = MonthRange("2023-01", "2023-12")
q3 = QuarterYear(2023, 3)

if q3 in fiscal_year:
    print("Q3 is within the fiscal year")

API Reference

Month

# Initialization formats
month = Month.current()  # Current month
month = Month(2025, 4)  # From year and month

month.year == 2025 # year as int
month.month == 4 # month as int from 1 to 12

str(month) == "2025-04"

MonthRange

# Initialize with Month objects, strings, or integers
range = MonthRange(Month("2023-01"), Month("2023-12"))
range = MonthRange("2023-01", "2023-12")
range = MonthRange(202301, 202312)

# Properties
first = range.first_month  # First month in range
last = range.last_month  # Last month in range
count = range.month_count  # Number of months in range
all_months = range.months  # List of all Month objects in range

# Methods
simplified = range.simplify()  # Convert to most specific type
next_range = range.next()  # Move range forward by its own length
prev_range = range.prev(2)  # Move range backward by twice its length
has_overlap = range.overlaps(other_range)  # Check for overlap
is_following = range.follows_directly(other_range)  # Check adjacency
intersection = range.intersect(other_range)  # Get intersection
unions = range.union(other_range1, other_range2)  # Get union

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT license.

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

month_range-0.0.6.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

month_range-0.0.6-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file month_range-0.0.6.tar.gz.

File metadata

  • Download URL: month_range-0.0.6.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for month_range-0.0.6.tar.gz
Algorithm Hash digest
SHA256 28d38d073bbd797f574690d51f28691a166cb16c24d7449fa4ef84d2396ebbd2
MD5 10845f4096f55d3bd2de4b2eb565cae4
BLAKE2b-256 160659519d7e5df721c108fd93b2156095579080744440006a246054e3ca8ccd

See more details on using hashes here.

File details

Details for the file month_range-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: month_range-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for month_range-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 8815bd0defb1128f3df8b01b2fa1d236bd1d72d0b4d88047f0806ec6481f800b
MD5 adeb6b9abc030042c43d89711fce9d9f
BLAKE2b-256 93d544f4c23c5ae2b4a7c74b8c44c28b607078adc4bd200df235408124cc3ce7

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