Metadata-Version: 2.4
Name: calbridgepy
Version: 0.2.0
Summary: Fast date conversion and difference functions for Jalali, Gregorian, and Hijri calendars
Author-email: Bistoon Hosseini <bistoon.hosseini@gmail.com>
License: MIT
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pandas>=1.5
Provides-Extra: polars
Requires-Dist: polars>=0.20; extra == "polars"
Dynamic: license-file

# calbridgepy

**calbridgepy** is a high-performance Python package for **fast and accurate conversion** between Jalali (Shamsi), Gregorian, and Hijri calendars.  
It efficiently handles **large datasets** using a **pre-built mapping table** covering **Gregorian years 1700–2200**, with exact aligned mappings for **Jalali** and **Hijri** dates.

The package provides five main functions:

1. `normalize_date()`: Normalize date strings to `"YYYY-MM-DD"` format, preserving optional time components.  
2. `convert_date()`: Convert dates between any two calendars (Jalali, Gregorian, Hijri) using vectorized joins/lookups.  
3. `day2day()`: Generate a sequence of dates between two endpoints in the chosen calendar, including optional start and end times.  
4. `date_diff()`: Compute the difference in days between two dates in any supported calendar using fast index lookups.  
5. `date_time_diff()`: Compute the difference between two date-time values in any calendar. Returns both raw seconds and a formatted `"X days HH:MM:SS"` string.

### Performance & Backend Flexibility
The package is designed with **zero heavy runtime overhead**:
- Uses **polars** when available for lightning-fast vectorized joins and parallel processing.
- Automatically falls back to **pandas** if Polars is not installed.
- Requires no heavy binary serialization dependencies (such as `pyarrow`).

---

## Installation

### From PyPI (Recommended)

Install the standard version via `pip`:
```bash
pip install calbridgepy
```

To install with optional **Polars** acceleration support:
```bash
pip install "calbridgepy[polars]"
```

You can also install directly from the GitHub repository:
```bash
pip install https://github.com/bistoonh/calbridgepy/archive/refs/heads/main.zip
```

To install with optional **Polars** acceleration support:
```bash
pip install "calbridgepy[polars] @ https://github.com/bistoonh/calbridgepy/archive/refs/heads/main.zip"
```

---

## Examples

```python
from calbridgepy import (
    normalize_date,
    convert_date,
    day2day,
    date_diff,
    date_time_diff,
)

# 1. Normalize dates (Jalali)
normalize_date(["14020101", "1402-01", "1402-01-01 14:30:00"])
# ['1402-01-01', '1402-01-01', '1402-01-01 14:30:00']

# Single string input returns a string
normalize_date("14020101")
# '1402-01-01'


# 2. Convert Jalali to Gregorian
convert_date("1402-01-01", from_cal="j", to_cal="g")
# '2023-03-21'

# Convert Gregorian to Hijri
convert_date("2023-03-21", from_cal="g", to_cal="h")
# '1444-08-29'

# Vectorized conversions
dates = ["1402-01-01", "1402-05-15", "1402-12-29"]
convert_date(dates, from_cal="j", to_cal="g")
# ['2023-03-21', '2023-08-06', '2024-03-18']


# 3. Generate a sequence of Hijri dates
day2day("1447-09-01", "1447-09-03", calendar="h")
# ['1447-09-01', '1447-09-02', '1447-09-03']

# Include start and end times (only attached at endpoints)
day2day("2025-03-21 08:00:00", "2025-03-23 18:00:00", calendar="g")
# ['2025-03-21 08:00:00', '2025-03-22', '2025-03-23 18:00:00']


# 4. Compute day differences (Gregorian)
date_diff("2025-03-21", "2025-03-25", calendar="g")
# 4

# Vectorized day differences (Hijri)
date_diff(
    ["1447-09-01", "1447-09-05"],
    ["1447-09-03", "1447-09-10"],
    calendar="h",
)
# [2, 5]


# 5. Compute date-time differences (seconds + formatted)
res = date_time_diff("1402-01-01 12:00:00", "1402-01-02 14:30:00", calendar="j")
print(res["seconds"])
# 95400

print(res["formatted"])
# '1 days 02:30:00'
```

---

## Data

The package includes a **pre-indexed calendar mapping** covering 1700 to 2200 CE:

```python
from calbridgepy.normalize_date import _get_calendar_map

df = _get_calendar_map()
# Returns a Polars DataFrame (if installed) or Pandas DataFrame
```

This table maps Jalali, Gregorian, and Hijri dates across multiple centuries, allowing instant O(1) lookups and fast vectorized joins without recalculating leap years or month structures on every call.

---

## Calendar Identifier Aliases

All functions accept both full names and short aliases:
- **Jalali / Shamsi:** `"j"`, `"jalali"`
- **Gregorian:** `"g"`, `"gregorian"`
- **Hijri:** `"h"`, `"hijri"`

---

## License

MIT License
