calbridgepy 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- calbridgepy-0.2.0/LICENSE +21 -0
- calbridgepy-0.2.0/PKG-INFO +155 -0
- calbridgepy-0.2.0/README.md +141 -0
- calbridgepy-0.2.0/pyproject.toml +31 -0
- calbridgepy-0.2.0/setup.cfg +4 -0
- calbridgepy-0.2.0/src/calbridgepy/__init__.py +15 -0
- calbridgepy-0.2.0/src/calbridgepy/convert_date.py +165 -0
- calbridgepy-0.2.0/src/calbridgepy/data/calendar_map.pkl +0 -0
- calbridgepy-0.2.0/src/calbridgepy/date_diff.py +181 -0
- calbridgepy-0.2.0/src/calbridgepy/date_time_diff.py +297 -0
- calbridgepy-0.2.0/src/calbridgepy/day2day.py +146 -0
- calbridgepy-0.2.0/src/calbridgepy/normalize_date.py +287 -0
- calbridgepy-0.2.0/src/calbridgepy.egg-info/PKG-INFO +155 -0
- calbridgepy-0.2.0/src/calbridgepy.egg-info/SOURCES.txt +15 -0
- calbridgepy-0.2.0/src/calbridgepy.egg-info/dependency_links.txt +1 -0
- calbridgepy-0.2.0/src/calbridgepy.egg-info/requires.txt +4 -0
- calbridgepy-0.2.0/src/calbridgepy.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bistoon Hosseini
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: calbridgepy
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Fast date conversion and difference functions for Jalali, Gregorian, and Hijri calendars
|
|
5
|
+
Author-email: Bistoon Hosseini <bistoon.hosseini@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: pandas>=1.5
|
|
11
|
+
Provides-Extra: polars
|
|
12
|
+
Requires-Dist: polars>=0.20; extra == "polars"
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# calbridgepy
|
|
16
|
+
|
|
17
|
+
**calbridgepy** is a high-performance Python package for **fast and accurate conversion** between Jalali (Shamsi), Gregorian, and Hijri calendars.
|
|
18
|
+
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.
|
|
19
|
+
|
|
20
|
+
The package provides five main functions:
|
|
21
|
+
|
|
22
|
+
1. `normalize_date()`: Normalize date strings to `"YYYY-MM-DD"` format, preserving optional time components.
|
|
23
|
+
2. `convert_date()`: Convert dates between any two calendars (Jalali, Gregorian, Hijri) using vectorized joins/lookups.
|
|
24
|
+
3. `day2day()`: Generate a sequence of dates between two endpoints in the chosen calendar, including optional start and end times.
|
|
25
|
+
4. `date_diff()`: Compute the difference in days between two dates in any supported calendar using fast index lookups.
|
|
26
|
+
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.
|
|
27
|
+
|
|
28
|
+
### Performance & Backend Flexibility
|
|
29
|
+
The package is designed with **zero heavy runtime overhead**:
|
|
30
|
+
- Uses **polars** when available for lightning-fast vectorized joins and parallel processing.
|
|
31
|
+
- Automatically falls back to **pandas** if Polars is not installed.
|
|
32
|
+
- Requires no heavy binary serialization dependencies (such as `pyarrow`).
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
### From PyPI (Recommended)
|
|
39
|
+
|
|
40
|
+
Install the standard version via `pip`:
|
|
41
|
+
```bash
|
|
42
|
+
pip install calbridgepy
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
To install with optional **Polars** acceleration support:
|
|
46
|
+
```bash
|
|
47
|
+
pip install "calbridgepy[polars]"
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
You can also install directly from the GitHub repository:
|
|
51
|
+
```bash
|
|
52
|
+
pip install https://github.com/bistoonh/calbridgepy/archive/refs/heads/main.zip
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
To install with optional **Polars** acceleration support:
|
|
56
|
+
```bash
|
|
57
|
+
pip install "calbridgepy[polars] @ https://github.com/bistoonh/calbridgepy/archive/refs/heads/main.zip"
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## Examples
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from calbridgepy import (
|
|
66
|
+
normalize_date,
|
|
67
|
+
convert_date,
|
|
68
|
+
day2day,
|
|
69
|
+
date_diff,
|
|
70
|
+
date_time_diff,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# 1. Normalize dates (Jalali)
|
|
74
|
+
normalize_date(["14020101", "1402-01", "1402-01-01 14:30:00"])
|
|
75
|
+
# ['1402-01-01', '1402-01-01', '1402-01-01 14:30:00']
|
|
76
|
+
|
|
77
|
+
# Single string input returns a string
|
|
78
|
+
normalize_date("14020101")
|
|
79
|
+
# '1402-01-01'
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
# 2. Convert Jalali to Gregorian
|
|
83
|
+
convert_date("1402-01-01", from_cal="j", to_cal="g")
|
|
84
|
+
# '2023-03-21'
|
|
85
|
+
|
|
86
|
+
# Convert Gregorian to Hijri
|
|
87
|
+
convert_date("2023-03-21", from_cal="g", to_cal="h")
|
|
88
|
+
# '1444-08-29'
|
|
89
|
+
|
|
90
|
+
# Vectorized conversions
|
|
91
|
+
dates = ["1402-01-01", "1402-05-15", "1402-12-29"]
|
|
92
|
+
convert_date(dates, from_cal="j", to_cal="g")
|
|
93
|
+
# ['2023-03-21', '2023-08-06', '2024-03-18']
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
# 3. Generate a sequence of Hijri dates
|
|
97
|
+
day2day("1447-09-01", "1447-09-03", calendar="h")
|
|
98
|
+
# ['1447-09-01', '1447-09-02', '1447-09-03']
|
|
99
|
+
|
|
100
|
+
# Include start and end times (only attached at endpoints)
|
|
101
|
+
day2day("2025-03-21 08:00:00", "2025-03-23 18:00:00", calendar="g")
|
|
102
|
+
# ['2025-03-21 08:00:00', '2025-03-22', '2025-03-23 18:00:00']
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
# 4. Compute day differences (Gregorian)
|
|
106
|
+
date_diff("2025-03-21", "2025-03-25", calendar="g")
|
|
107
|
+
# 4
|
|
108
|
+
|
|
109
|
+
# Vectorized day differences (Hijri)
|
|
110
|
+
date_diff(
|
|
111
|
+
["1447-09-01", "1447-09-05"],
|
|
112
|
+
["1447-09-03", "1447-09-10"],
|
|
113
|
+
calendar="h",
|
|
114
|
+
)
|
|
115
|
+
# [2, 5]
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
# 5. Compute date-time differences (seconds + formatted)
|
|
119
|
+
res = date_time_diff("1402-01-01 12:00:00", "1402-01-02 14:30:00", calendar="j")
|
|
120
|
+
print(res["seconds"])
|
|
121
|
+
# 95400
|
|
122
|
+
|
|
123
|
+
print(res["formatted"])
|
|
124
|
+
# '1 days 02:30:00'
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
## Data
|
|
130
|
+
|
|
131
|
+
The package includes a **pre-indexed calendar mapping** covering 1700 to 2200 CE:
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
from calbridgepy.normalize_date import _get_calendar_map
|
|
135
|
+
|
|
136
|
+
df = _get_calendar_map()
|
|
137
|
+
# Returns a Polars DataFrame (if installed) or Pandas DataFrame
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
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.
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## Calendar Identifier Aliases
|
|
145
|
+
|
|
146
|
+
All functions accept both full names and short aliases:
|
|
147
|
+
- **Jalali / Shamsi:** `"j"`, `"jalali"`
|
|
148
|
+
- **Gregorian:** `"g"`, `"gregorian"`
|
|
149
|
+
- **Hijri:** `"h"`, `"hijri"`
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## License
|
|
154
|
+
|
|
155
|
+
MIT License
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# calbridgepy
|
|
2
|
+
|
|
3
|
+
**calbridgepy** is a high-performance Python package for **fast and accurate conversion** between Jalali (Shamsi), Gregorian, and Hijri calendars.
|
|
4
|
+
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.
|
|
5
|
+
|
|
6
|
+
The package provides five main functions:
|
|
7
|
+
|
|
8
|
+
1. `normalize_date()`: Normalize date strings to `"YYYY-MM-DD"` format, preserving optional time components.
|
|
9
|
+
2. `convert_date()`: Convert dates between any two calendars (Jalali, Gregorian, Hijri) using vectorized joins/lookups.
|
|
10
|
+
3. `day2day()`: Generate a sequence of dates between two endpoints in the chosen calendar, including optional start and end times.
|
|
11
|
+
4. `date_diff()`: Compute the difference in days between two dates in any supported calendar using fast index lookups.
|
|
12
|
+
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.
|
|
13
|
+
|
|
14
|
+
### Performance & Backend Flexibility
|
|
15
|
+
The package is designed with **zero heavy runtime overhead**:
|
|
16
|
+
- Uses **polars** when available for lightning-fast vectorized joins and parallel processing.
|
|
17
|
+
- Automatically falls back to **pandas** if Polars is not installed.
|
|
18
|
+
- Requires no heavy binary serialization dependencies (such as `pyarrow`).
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
### From PyPI (Recommended)
|
|
25
|
+
|
|
26
|
+
Install the standard version via `pip`:
|
|
27
|
+
```bash
|
|
28
|
+
pip install calbridgepy
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
To install with optional **Polars** acceleration support:
|
|
32
|
+
```bash
|
|
33
|
+
pip install "calbridgepy[polars]"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
You can also install directly from the GitHub repository:
|
|
37
|
+
```bash
|
|
38
|
+
pip install https://github.com/bistoonh/calbridgepy/archive/refs/heads/main.zip
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
To install with optional **Polars** acceleration support:
|
|
42
|
+
```bash
|
|
43
|
+
pip install "calbridgepy[polars] @ https://github.com/bistoonh/calbridgepy/archive/refs/heads/main.zip"
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Examples
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from calbridgepy import (
|
|
52
|
+
normalize_date,
|
|
53
|
+
convert_date,
|
|
54
|
+
day2day,
|
|
55
|
+
date_diff,
|
|
56
|
+
date_time_diff,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
# 1. Normalize dates (Jalali)
|
|
60
|
+
normalize_date(["14020101", "1402-01", "1402-01-01 14:30:00"])
|
|
61
|
+
# ['1402-01-01', '1402-01-01', '1402-01-01 14:30:00']
|
|
62
|
+
|
|
63
|
+
# Single string input returns a string
|
|
64
|
+
normalize_date("14020101")
|
|
65
|
+
# '1402-01-01'
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
# 2. Convert Jalali to Gregorian
|
|
69
|
+
convert_date("1402-01-01", from_cal="j", to_cal="g")
|
|
70
|
+
# '2023-03-21'
|
|
71
|
+
|
|
72
|
+
# Convert Gregorian to Hijri
|
|
73
|
+
convert_date("2023-03-21", from_cal="g", to_cal="h")
|
|
74
|
+
# '1444-08-29'
|
|
75
|
+
|
|
76
|
+
# Vectorized conversions
|
|
77
|
+
dates = ["1402-01-01", "1402-05-15", "1402-12-29"]
|
|
78
|
+
convert_date(dates, from_cal="j", to_cal="g")
|
|
79
|
+
# ['2023-03-21', '2023-08-06', '2024-03-18']
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
# 3. Generate a sequence of Hijri dates
|
|
83
|
+
day2day("1447-09-01", "1447-09-03", calendar="h")
|
|
84
|
+
# ['1447-09-01', '1447-09-02', '1447-09-03']
|
|
85
|
+
|
|
86
|
+
# Include start and end times (only attached at endpoints)
|
|
87
|
+
day2day("2025-03-21 08:00:00", "2025-03-23 18:00:00", calendar="g")
|
|
88
|
+
# ['2025-03-21 08:00:00', '2025-03-22', '2025-03-23 18:00:00']
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
# 4. Compute day differences (Gregorian)
|
|
92
|
+
date_diff("2025-03-21", "2025-03-25", calendar="g")
|
|
93
|
+
# 4
|
|
94
|
+
|
|
95
|
+
# Vectorized day differences (Hijri)
|
|
96
|
+
date_diff(
|
|
97
|
+
["1447-09-01", "1447-09-05"],
|
|
98
|
+
["1447-09-03", "1447-09-10"],
|
|
99
|
+
calendar="h",
|
|
100
|
+
)
|
|
101
|
+
# [2, 5]
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
# 5. Compute date-time differences (seconds + formatted)
|
|
105
|
+
res = date_time_diff("1402-01-01 12:00:00", "1402-01-02 14:30:00", calendar="j")
|
|
106
|
+
print(res["seconds"])
|
|
107
|
+
# 95400
|
|
108
|
+
|
|
109
|
+
print(res["formatted"])
|
|
110
|
+
# '1 days 02:30:00'
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## Data
|
|
116
|
+
|
|
117
|
+
The package includes a **pre-indexed calendar mapping** covering 1700 to 2200 CE:
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
from calbridgepy.normalize_date import _get_calendar_map
|
|
121
|
+
|
|
122
|
+
df = _get_calendar_map()
|
|
123
|
+
# Returns a Polars DataFrame (if installed) or Pandas DataFrame
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
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.
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## Calendar Identifier Aliases
|
|
131
|
+
|
|
132
|
+
All functions accept both full names and short aliases:
|
|
133
|
+
- **Jalali / Shamsi:** `"j"`, `"jalali"`
|
|
134
|
+
- **Gregorian:** `"g"`, `"gregorian"`
|
|
135
|
+
- **Hijri:** `"h"`, `"hijri"`
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
## License
|
|
140
|
+
|
|
141
|
+
MIT License
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "calbridgepy"
|
|
7
|
+
version = "0.2.0"
|
|
8
|
+
description = "Fast date conversion and difference functions for Jalali, Gregorian, and Hijri calendars"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Bistoon Hosseini", email = "bistoon.hosseini@gmail.com" }
|
|
14
|
+
]
|
|
15
|
+
dependencies = [
|
|
16
|
+
"pandas>=1.5"
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[project.optional-dependencies]
|
|
20
|
+
polars = ["polars>=0.20"]
|
|
21
|
+
|
|
22
|
+
[tool.setuptools]
|
|
23
|
+
package-dir = {"" = "src"}
|
|
24
|
+
include-package-data = true
|
|
25
|
+
|
|
26
|
+
[tool.setuptools.packages.find]
|
|
27
|
+
where = ["src"]
|
|
28
|
+
|
|
29
|
+
[tool.setuptools.package-data]
|
|
30
|
+
"calbridgepy" = ["data/*.pkl"]
|
|
31
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from .normalize_date import normalize_date
|
|
2
|
+
from .convert_date import convert_date
|
|
3
|
+
from .date_diff import date_diff
|
|
4
|
+
from .date_time_diff import date_time_diff
|
|
5
|
+
from .day2day import day2day
|
|
6
|
+
|
|
7
|
+
__version__ = "0.2.0"
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"normalize_date",
|
|
11
|
+
"convert_date",
|
|
12
|
+
"date_diff",
|
|
13
|
+
"date_time_diff",
|
|
14
|
+
"day2day",
|
|
15
|
+
]
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
from functools import lru_cache
|
|
2
|
+
from .normalize_date import _get_calendar_map, normalize_date
|
|
3
|
+
|
|
4
|
+
try:
|
|
5
|
+
import polars as pl
|
|
6
|
+
except ImportError:
|
|
7
|
+
pl = None
|
|
8
|
+
|
|
9
|
+
if pl is None:
|
|
10
|
+
import pandas as pd
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
_CALENDARS = {
|
|
14
|
+
"j": "Shamsi",
|
|
15
|
+
"jalali": "Shamsi",
|
|
16
|
+
"g": "Gregorian",
|
|
17
|
+
"gregorian": "Gregorian",
|
|
18
|
+
"h": "Hijri",
|
|
19
|
+
"hijri": "Hijri",
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _normalize_calendar(name):
|
|
24
|
+
"""Validate and normalize calendar name input."""
|
|
25
|
+
if not isinstance(name, str) or name.lower() not in _CALENDARS:
|
|
26
|
+
raise ValueError(
|
|
27
|
+
"Unsupported calendar type. Use 'jalali'/'j', "
|
|
28
|
+
"'gregorian'/'g', or 'hijri'/'h'."
|
|
29
|
+
)
|
|
30
|
+
return _CALENDARS[name.lower()]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _is_scalar(x):
|
|
34
|
+
"""Check if input is a scalar value."""
|
|
35
|
+
return isinstance(x, (str, bytes)) or not hasattr(x, "__iter__")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@lru_cache(maxsize=12)
|
|
39
|
+
def _get_scalar_dict(from_col, to_col):
|
|
40
|
+
"""Cache O(1) Python dictionary for lightning-fast scalar lookups."""
|
|
41
|
+
calendar_map = _get_calendar_map()
|
|
42
|
+
if pl is not None:
|
|
43
|
+
src = calendar_map.get_column(from_col).to_list()
|
|
44
|
+
dst = calendar_map.get_column(to_col).to_list()
|
|
45
|
+
else:
|
|
46
|
+
src = calendar_map[from_col].tolist()
|
|
47
|
+
dst = calendar_map[to_col].tolist()
|
|
48
|
+
|
|
49
|
+
# Built in reverse order so the first occurrence wins (equivalent to keep='first')
|
|
50
|
+
mapping = {}
|
|
51
|
+
for k, v in zip(reversed(src), reversed(dst)):
|
|
52
|
+
mapping[k] = v
|
|
53
|
+
return mapping
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@lru_cache(maxsize=12)
|
|
57
|
+
def _get_polars_mapping(from_col, to_col):
|
|
58
|
+
"""Pre-computed cached unique mapping table for Polars join."""
|
|
59
|
+
calendar_map = _get_calendar_map()
|
|
60
|
+
return calendar_map.select(
|
|
61
|
+
pl.col(from_col).alias("date"),
|
|
62
|
+
pl.col(to_col).alias("converted"),
|
|
63
|
+
).unique(subset="date", keep="first", maintain_order=True)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@lru_cache(maxsize=12)
|
|
67
|
+
def _get_pandas_mapping(from_col, to_col):
|
|
68
|
+
"""Pre-computed cached unique mapping table for Pandas merge."""
|
|
69
|
+
calendar_map = _get_calendar_map()
|
|
70
|
+
return (
|
|
71
|
+
calendar_map[[from_col, to_col]]
|
|
72
|
+
.rename(columns={from_col: "date", to_col: "converted"})
|
|
73
|
+
.drop_duplicates(subset="date", keep="first")
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def convert_date(dates, from_cal, to_cal):
|
|
78
|
+
"""Convert dates between Shamsi, Gregorian, and Hijri calendars.
|
|
79
|
+
|
|
80
|
+
Parameters:
|
|
81
|
+
-----------
|
|
82
|
+
dates : str, list, or array-like
|
|
83
|
+
Date strings or timestamps to convert.
|
|
84
|
+
from_cal : str
|
|
85
|
+
Source calendar ('jalali'/'j', 'gregorian'/'g', 'hijri'/'h').
|
|
86
|
+
to_cal : str
|
|
87
|
+
Target calendar ('jalali'/'j', 'gregorian'/'g', 'hijri'/'h').
|
|
88
|
+
|
|
89
|
+
Returns:
|
|
90
|
+
--------
|
|
91
|
+
str, list, or None
|
|
92
|
+
Converted date(s) preserving time components if present.
|
|
93
|
+
"""
|
|
94
|
+
from_col = _normalize_calendar(from_cal)
|
|
95
|
+
to_col = _normalize_calendar(to_cal)
|
|
96
|
+
|
|
97
|
+
if from_col == to_col:
|
|
98
|
+
raise ValueError("'from' and 'to' calendars must be different.")
|
|
99
|
+
|
|
100
|
+
is_scalar = _is_scalar(dates)
|
|
101
|
+
|
|
102
|
+
# 1. Fast path for scalar input
|
|
103
|
+
if is_scalar:
|
|
104
|
+
norm_val = normalize_date(dates)
|
|
105
|
+
if norm_val is None:
|
|
106
|
+
return None
|
|
107
|
+
|
|
108
|
+
scalar_map = _get_scalar_dict(from_col, to_col)
|
|
109
|
+
if " " in norm_val:
|
|
110
|
+
d_part, t_part = norm_val.split(" ", 1)
|
|
111
|
+
converted = scalar_map.get(d_part)
|
|
112
|
+
return f"{converted} {t_part}" if converted is not None else None
|
|
113
|
+
else:
|
|
114
|
+
return scalar_map.get(norm_val)
|
|
115
|
+
|
|
116
|
+
# 2. Vectorized list / array path
|
|
117
|
+
values = normalize_date(dates)
|
|
118
|
+
if not values:
|
|
119
|
+
return []
|
|
120
|
+
|
|
121
|
+
if pl is not None:
|
|
122
|
+
df_input = pl.DataFrame({"original": values}, schema={"original": pl.Utf8})
|
|
123
|
+
split_df = df_input.with_columns(
|
|
124
|
+
split_struct=pl.col("original").str.splitn(" ", 2)
|
|
125
|
+
).with_columns(
|
|
126
|
+
date=pl.col("split_struct").struct.field("field_0"),
|
|
127
|
+
time=pl.col("split_struct").struct.field("field_1"),
|
|
128
|
+
).drop("split_struct")
|
|
129
|
+
|
|
130
|
+
mapping = _get_polars_mapping(from_col, to_col)
|
|
131
|
+
|
|
132
|
+
result = (
|
|
133
|
+
split_df
|
|
134
|
+
.join(mapping, on="date", how="left")
|
|
135
|
+
.with_columns(
|
|
136
|
+
final_res=pl.when(
|
|
137
|
+
pl.col("converted").is_not_null() &
|
|
138
|
+
pl.col("time").is_not_null() &
|
|
139
|
+
(pl.col("time") != "")
|
|
140
|
+
)
|
|
141
|
+
.then(pl.col("converted") + " " + pl.col("time"))
|
|
142
|
+
.otherwise(pl.col("converted"))
|
|
143
|
+
)
|
|
144
|
+
.get_column("final_res")
|
|
145
|
+
.to_list()
|
|
146
|
+
)
|
|
147
|
+
return result
|
|
148
|
+
|
|
149
|
+
else:
|
|
150
|
+
source_series = pd.Series(values, dtype="object")
|
|
151
|
+
split_df = source_series.str.split(" ", n=1, expand=True)
|
|
152
|
+
date_only = split_df[0]
|
|
153
|
+
time_only = split_df[1] if 1 in split_df.columns else pd.Series([None] * len(values))
|
|
154
|
+
|
|
155
|
+
mapping = _get_pandas_mapping(from_col, to_col)
|
|
156
|
+
|
|
157
|
+
merged = pd.DataFrame({"date": date_only}).merge(mapping, on="date", how="left")
|
|
158
|
+
converted = merged["converted"]
|
|
159
|
+
|
|
160
|
+
mask = converted.notna() & time_only.notna() & (time_only != "")
|
|
161
|
+
result_series = converted.copy()
|
|
162
|
+
result_series[mask] = converted[mask] + " " + time_only[mask]
|
|
163
|
+
result_series[~converted.notna()] = None
|
|
164
|
+
|
|
165
|
+
return result_series.tolist()
|
|
Binary file
|