python-intl 0.1.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.
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
Copyright (c) 2026 David Danier
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
18
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
19
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
|
20
|
+
OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: python-intl
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Add your description here
|
|
5
|
+
Author: David Danier
|
|
6
|
+
Author-email: David Danier <david.danier@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: pyicu>=2.16.2
|
|
10
|
+
Requires-Python: >=3.12
|
|
11
|
+
Project-URL: Repository, https://github.com/ddanier/python-intl
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# `python-intl`
|
|
15
|
+
|
|
16
|
+
A small library using [PyICU](https://pypi.org/project/pyicu/) to provide a Python
|
|
17
|
+
API similar to what the [`Intl` JavaScript API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl)
|
|
18
|
+
provides.
|
|
19
|
+
|
|
20
|
+
**Status:** This is very much work in progress. Currently only `Intl.DateTimeFormat`
|
|
21
|
+
exists and this also is not complete yet.
|
|
22
|
+
|
|
23
|
+
## `Intl.DateTimeFormat`
|
|
24
|
+
|
|
25
|
+
### Example usage
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
import datetime
|
|
29
|
+
from python_intl import DateTimeFormat
|
|
30
|
+
|
|
31
|
+
datetime_ = datetime.datetime(2026, 8, 15)
|
|
32
|
+
formatter = DateTimeFormat("de-DE", {"year": "numeric", "month": "2-digit", "day": "2-digit"})
|
|
33
|
+
formatter.format(datetime_) # Will output the German format: "15.08.2026"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
Be sure to be able to install `PyICU`, see the installation docs there:
|
|
39
|
+
https://gitlab.pyicu.org/main/pyicu#installing-pyicu
|
|
40
|
+
|
|
41
|
+
**Hint:** I mainly did run into issues with `pkg-config` not finding the ICU library,
|
|
42
|
+
setting `PKG_CONFIG_PATH` accordingly helps most of the time I guess.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# `python-intl`
|
|
2
|
+
|
|
3
|
+
A small library using [PyICU](https://pypi.org/project/pyicu/) to provide a Python
|
|
4
|
+
API similar to what the [`Intl` JavaScript API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl)
|
|
5
|
+
provides.
|
|
6
|
+
|
|
7
|
+
**Status:** This is very much work in progress. Currently only `Intl.DateTimeFormat`
|
|
8
|
+
exists and this also is not complete yet.
|
|
9
|
+
|
|
10
|
+
## `Intl.DateTimeFormat`
|
|
11
|
+
|
|
12
|
+
### Example usage
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
import datetime
|
|
16
|
+
from python_intl import DateTimeFormat
|
|
17
|
+
|
|
18
|
+
datetime_ = datetime.datetime(2026, 8, 15)
|
|
19
|
+
formatter = DateTimeFormat("de-DE", {"year": "numeric", "month": "2-digit", "day": "2-digit"})
|
|
20
|
+
formatter.format(datetime_) # Will output the German format: "15.08.2026"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
Be sure to be able to install `PyICU`, see the installation docs there:
|
|
26
|
+
https://gitlab.pyicu.org/main/pyicu#installing-pyicu
|
|
27
|
+
|
|
28
|
+
**Hint:** I mainly did run into issues with `pkg-config` not finding the ICU library,
|
|
29
|
+
setting `PKG_CONFIG_PATH` accordingly helps most of the time I guess.
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "python-intl"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Add your description here"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.12"
|
|
9
|
+
dependencies = ["pyicu>=2.16.2"]
|
|
10
|
+
|
|
11
|
+
[[project.authors]]
|
|
12
|
+
name = "David Danier"
|
|
13
|
+
email = "david.danier@gmail.com"
|
|
14
|
+
|
|
15
|
+
[project.urls]
|
|
16
|
+
Repository = "https://github.com/ddanier/python-intl"
|
|
17
|
+
|
|
18
|
+
[dependency-groups]
|
|
19
|
+
dev = [
|
|
20
|
+
"mypy>=2.3.1",
|
|
21
|
+
"pyright>=1.1.411",
|
|
22
|
+
"pytest>=9.1.1",
|
|
23
|
+
"pytest-cov>=7.1.0",
|
|
24
|
+
"ruff>=0.16.3",
|
|
25
|
+
"tox>=4.60.0",
|
|
26
|
+
"ty>=0.0.72",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[tool.pytest.ini_options]
|
|
30
|
+
markers = ["unit: mark a test as a unit test"]
|
|
31
|
+
|
|
32
|
+
[tool.ruff]
|
|
33
|
+
line-length = 115
|
|
34
|
+
target-version = "py312"
|
|
35
|
+
output-format = "grouped"
|
|
36
|
+
|
|
37
|
+
[tool.ruff.lint]
|
|
38
|
+
select = [
|
|
39
|
+
"F",
|
|
40
|
+
"E",
|
|
41
|
+
"W",
|
|
42
|
+
"C",
|
|
43
|
+
"I",
|
|
44
|
+
"N",
|
|
45
|
+
"UP",
|
|
46
|
+
"ANN",
|
|
47
|
+
"S",
|
|
48
|
+
"B",
|
|
49
|
+
"A",
|
|
50
|
+
"COM",
|
|
51
|
+
"C4",
|
|
52
|
+
"T20",
|
|
53
|
+
"PT",
|
|
54
|
+
"ARG",
|
|
55
|
+
"TD",
|
|
56
|
+
"RUF",
|
|
57
|
+
]
|
|
58
|
+
ignore = [
|
|
59
|
+
"A001",
|
|
60
|
+
"A002",
|
|
61
|
+
"A003",
|
|
62
|
+
"ANN401",
|
|
63
|
+
"C901",
|
|
64
|
+
"N8",
|
|
65
|
+
"B008",
|
|
66
|
+
"F405",
|
|
67
|
+
"F821",
|
|
68
|
+
]
|
|
69
|
+
|
|
70
|
+
[tool.ruff.lint.per-file-ignores]
|
|
71
|
+
"__init__.py" = ["F401"]
|
|
72
|
+
"conftest.py" = [
|
|
73
|
+
"S101",
|
|
74
|
+
"ANN",
|
|
75
|
+
"F401",
|
|
76
|
+
]
|
|
77
|
+
"test_*.py" = [
|
|
78
|
+
"S101",
|
|
79
|
+
"ANN",
|
|
80
|
+
"F401",
|
|
81
|
+
]
|
|
82
|
+
|
|
83
|
+
[tool.uv.build-backend]
|
|
84
|
+
module-name = "python_intl"
|
|
85
|
+
module-root = ""
|
|
86
|
+
|
|
87
|
+
[build-system]
|
|
88
|
+
requires = ["uv_build>=0.12.3,<0.13.0"]
|
|
89
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "python-intl"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Add your description here"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "David Danier", email = "david.danier@gmail.com" }
|
|
8
|
+
]
|
|
9
|
+
license = "MIT"
|
|
10
|
+
license-files = ["LICENSE"]
|
|
11
|
+
requires-python = ">=3.12"
|
|
12
|
+
dependencies = [
|
|
13
|
+
"pyicu>=2.16.2",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[project.urls]
|
|
17
|
+
Repository = "https://github.com/ddanier/python-intl"
|
|
18
|
+
|
|
19
|
+
[dependency-groups]
|
|
20
|
+
dev = [
|
|
21
|
+
"mypy>=2.3.1",
|
|
22
|
+
"pyright>=1.1.411",
|
|
23
|
+
"pytest>=9.1.1",
|
|
24
|
+
"pytest-cov>=7.1.0",
|
|
25
|
+
"ruff>=0.16.3",
|
|
26
|
+
"tox>=4.60.0",
|
|
27
|
+
"ty>=0.0.72",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[tool.pytest.ini_options]
|
|
31
|
+
markers = [
|
|
32
|
+
"unit: mark a test as a unit test",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[tool.ruff]
|
|
36
|
+
line-length = 115
|
|
37
|
+
target-version = "py312"
|
|
38
|
+
output-format = "grouped"
|
|
39
|
+
|
|
40
|
+
[tool.ruff.lint]
|
|
41
|
+
select = ["F","E","W","C","I","N","UP","ANN","S","B","A","COM","C4","T20","PT","ARG","TD","RUF"]
|
|
42
|
+
ignore = ["A001","A002","A003","ANN401","C901","N8","B008","F405","F821"]
|
|
43
|
+
|
|
44
|
+
[tool.ruff.lint.per-file-ignores]
|
|
45
|
+
"__init__.py" = ["F401"]
|
|
46
|
+
"conftest.py" = ["S101","ANN","F401"]
|
|
47
|
+
"test_*.py" = ["S101","ANN","F401"]
|
|
48
|
+
|
|
49
|
+
[build-system]
|
|
50
|
+
requires = ["uv_build>=0.12.3,<0.13.0"]
|
|
51
|
+
build-backend = "uv_build"
|
|
52
|
+
|
|
53
|
+
[tool.uv.build-backend]
|
|
54
|
+
module-name = "python_intl"
|
|
55
|
+
module-root = ""
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .datetimeformat import DateTimeFormat as DateTimeFormat
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import dataclasses
|
|
4
|
+
import datetime as dt
|
|
5
|
+
from functools import cache, cached_property
|
|
6
|
+
from typing import TYPE_CHECKING, Literal, overload
|
|
7
|
+
|
|
8
|
+
import icu # type: ignore[import-untyped]
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from collections.abc import Iterable
|
|
12
|
+
from typing import NotRequired, TypedDict
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
type EraFormatT = Literal["long", "short", "narrow"]
|
|
17
|
+
type YearFormatT = Literal["numeric", "2-digit"]
|
|
18
|
+
type MonthFormatT = Literal["numeric", "2-digit", "long", "short", "narrow"]
|
|
19
|
+
type WeekdayFormatT = Literal["long", "short", "narrow"]
|
|
20
|
+
type DayFormatT = Literal["numeric", "2-digit"]
|
|
21
|
+
type DayPeriodFormatT = Literal["long", "short", "narrow"]
|
|
22
|
+
type HourFormatT = Literal["numeric", "2-digit"]
|
|
23
|
+
type MinuteFormatT = Literal["numeric", "2-digit"]
|
|
24
|
+
type SecondFormatT = Literal["numeric", "2-digit"]
|
|
25
|
+
type FractionSecondDigitsFormatT = Literal[1, 2, 3]
|
|
26
|
+
type TimezoneNameFormatT = Literal[
|
|
27
|
+
"short",
|
|
28
|
+
"long",
|
|
29
|
+
"short_offset",
|
|
30
|
+
"long_offset",
|
|
31
|
+
"short_generic",
|
|
32
|
+
"long_generic",
|
|
33
|
+
]
|
|
34
|
+
type AnyFormatT = (
|
|
35
|
+
EraFormatT
|
|
36
|
+
| YearFormatT
|
|
37
|
+
| MonthFormatT
|
|
38
|
+
| WeekdayFormatT
|
|
39
|
+
| DayFormatT
|
|
40
|
+
| DayPeriodFormatT
|
|
41
|
+
| HourFormatT
|
|
42
|
+
| MinuteFormatT
|
|
43
|
+
| SecondFormatT
|
|
44
|
+
| FractionSecondDigitsFormatT
|
|
45
|
+
| TimezoneNameFormatT
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
type EraPatternT = Literal["G", "GGGG", "GGGGG"]
|
|
49
|
+
type YearPatternT = Literal["y", "yy", "yyyy"]
|
|
50
|
+
type MonthPatternT = Literal["M", "MM", "MMM", "MMMM", "MMMMM"]
|
|
51
|
+
type WeekdayPatternT = Literal["E", "EEEE", "EEEEE"]
|
|
52
|
+
type DayPatternT = Literal["d", "dd"]
|
|
53
|
+
type DayPeriodPatternT = Literal["a", "aaaa", "aaaaa", "b", "bbbb", "bbbbb", "B", "BBBB", "BBBBB"]
|
|
54
|
+
type HourPatternT = Literal["j", "jj", "H", "h", "HH", "hh"]
|
|
55
|
+
type MinutePatternT = Literal["m", "mm"]
|
|
56
|
+
type SecondPatternT = Literal["s", "ss"]
|
|
57
|
+
type FractionSecondDigitsPatternT = Literal["S", "SS", "SSS"]
|
|
58
|
+
type TimezoneNamePatternT = Literal["z", "zzzz", "Z", "ZZZZ", "v", "vvvv"]
|
|
59
|
+
type AnyPatternT = (
|
|
60
|
+
EraPatternT
|
|
61
|
+
| YearPatternT
|
|
62
|
+
| MonthPatternT
|
|
63
|
+
| WeekdayPatternT
|
|
64
|
+
| DayPatternT
|
|
65
|
+
| DayPeriodPatternT
|
|
66
|
+
| HourPatternT
|
|
67
|
+
| MinutePatternT
|
|
68
|
+
| SecondPatternT
|
|
69
|
+
| FractionSecondDigitsPatternT
|
|
70
|
+
| TimezoneNamePatternT
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
type DatetimeFieldT = Literal[
|
|
74
|
+
"era",
|
|
75
|
+
"year",
|
|
76
|
+
"month",
|
|
77
|
+
"weekday",
|
|
78
|
+
"day",
|
|
79
|
+
"day_period",
|
|
80
|
+
"hour",
|
|
81
|
+
"minute",
|
|
82
|
+
"second",
|
|
83
|
+
"fraction_second_digits",
|
|
84
|
+
"time_zone_name",
|
|
85
|
+
]
|
|
86
|
+
|
|
87
|
+
class DateTimeFormatOptionsDictT(TypedDict):
|
|
88
|
+
era: NotRequired[EraFormatT]
|
|
89
|
+
year: NotRequired[YearFormatT]
|
|
90
|
+
month: NotRequired[MonthFormatT]
|
|
91
|
+
weekday: NotRequired[WeekdayFormatT]
|
|
92
|
+
day: NotRequired[DayFormatT]
|
|
93
|
+
day_period: NotRequired[DayPeriodFormatT]
|
|
94
|
+
hour: NotRequired[HourFormatT]
|
|
95
|
+
minute: NotRequired[MinuteFormatT]
|
|
96
|
+
second: NotRequired[SecondFormatT]
|
|
97
|
+
fraction_second_digits: NotRequired[FractionSecondDigitsFormatT]
|
|
98
|
+
time_zone_name: NotRequired[TimezoneNameFormatT]
|
|
99
|
+
|
|
100
|
+
class OptionsToSkeletonT(TypedDict):
|
|
101
|
+
era: dict[EraFormatT, EraPatternT | tuple[EraPatternT, ...]]
|
|
102
|
+
year: dict[YearFormatT, YearPatternT | tuple[YearPatternT, ...]]
|
|
103
|
+
month: dict[MonthFormatT, MonthPatternT | tuple[MonthPatternT, ...]]
|
|
104
|
+
weekday: dict[WeekdayFormatT, WeekdayPatternT | tuple[WeekdayPatternT, ...]]
|
|
105
|
+
day: dict[DayFormatT, DayPatternT | tuple[DayPatternT, ...]]
|
|
106
|
+
day_period: dict[DayPeriodFormatT, DayPeriodPatternT | tuple[DayPeriodPatternT, ...]]
|
|
107
|
+
hour: dict[HourFormatT, HourPatternT | tuple[HourPatternT, ...]]
|
|
108
|
+
minute: dict[MinuteFormatT, MinutePatternT | tuple[MinutePatternT, ...]]
|
|
109
|
+
second: dict[SecondFormatT, SecondPatternT | tuple[SecondPatternT, ...]]
|
|
110
|
+
fraction_second_digits: dict[
|
|
111
|
+
FractionSecondDigitsFormatT,
|
|
112
|
+
FractionSecondDigitsPatternT | tuple[FractionSecondDigitsPatternT, ...],
|
|
113
|
+
]
|
|
114
|
+
time_zone_name: dict[TimezoneNameFormatT, TimezoneNamePatternT | tuple[TimezoneNamePatternT, ...]]
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
@dataclasses.dataclass(frozen=True, kw_only=True)
|
|
118
|
+
class DateTimeFormatOptions:
|
|
119
|
+
era: EraFormatT | None = None
|
|
120
|
+
year: YearFormatT | None = None
|
|
121
|
+
month: MonthFormatT | None = None
|
|
122
|
+
weekday: WeekdayFormatT | None = None
|
|
123
|
+
day: DayFormatT | None = None
|
|
124
|
+
day_period: DayPeriodFormatT | None = None
|
|
125
|
+
hour: HourFormatT | None = None
|
|
126
|
+
minute: MinuteFormatT | None = None
|
|
127
|
+
second: SecondFormatT | None = None
|
|
128
|
+
fraction_second_digits: FractionSecondDigitsFormatT | None = None
|
|
129
|
+
time_zone_name: TimezoneNameFormatT | None = None
|
|
130
|
+
|
|
131
|
+
def to_json(self) -> dict[str, str | int]:
|
|
132
|
+
return {
|
|
133
|
+
k: v
|
|
134
|
+
for k, v in (
|
|
135
|
+
("era", self.era),
|
|
136
|
+
("year", self.year),
|
|
137
|
+
("month", self.month),
|
|
138
|
+
("weekday", self.weekday),
|
|
139
|
+
("day", self.day),
|
|
140
|
+
("day_period", self.day_period),
|
|
141
|
+
("hour", self.hour),
|
|
142
|
+
("minute", self.minute),
|
|
143
|
+
("second", self.second),
|
|
144
|
+
("fraction_second_digits", self.fraction_second_digits),
|
|
145
|
+
("time_zone_name", self.time_zone_name),
|
|
146
|
+
)
|
|
147
|
+
if v is not None
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
OPTIONS_TO_SKELETON: OptionsToSkeletonT = {
|
|
152
|
+
"era": {"short": "G", "long": "GGGG", "narrow": "GGGGG"},
|
|
153
|
+
"year": {"numeric": ("yyyy", "y"), "2-digit": "yy"},
|
|
154
|
+
"month": {"numeric": "M", "2-digit": "MM", "short": "MMM", "long": "MMMM", "narrow": "MMMMM"},
|
|
155
|
+
"weekday": {"short": "E", "long": "EEEE", "narrow": "EEEEE"},
|
|
156
|
+
"day": {"numeric": "d", "2-digit": "dd"},
|
|
157
|
+
"day_period": {
|
|
158
|
+
"short": ("a", "b", "B"),
|
|
159
|
+
"long": ("aaaa", "bbbb", "BBBB"),
|
|
160
|
+
"narrow": ("aaaaa", "bbbbb", "BBBBB"),
|
|
161
|
+
},
|
|
162
|
+
"hour": {"numeric": ("j", "H", "h"), "2-digit": ("jj", "HH", "hh")},
|
|
163
|
+
"minute": {"numeric": "m", "2-digit": "mm"},
|
|
164
|
+
"second": {"numeric": "s", "2-digit": "ss"},
|
|
165
|
+
"fraction_second_digits": {1: "S", 2: "SS", 3: "SSS"},
|
|
166
|
+
"time_zone_name": {
|
|
167
|
+
"short": "z",
|
|
168
|
+
"long": "zzzz",
|
|
169
|
+
"short_offset": "Z",
|
|
170
|
+
"long_offset": "ZZZZ",
|
|
171
|
+
"short_generic": "v",
|
|
172
|
+
"long_generic": "vvvv",
|
|
173
|
+
},
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
@overload
|
|
178
|
+
def _option_to_skeleton_bit(
|
|
179
|
+
field: Literal["era"],
|
|
180
|
+
format: EraFormatT,
|
|
181
|
+
) -> EraPatternT | tuple[EraPatternT, ...]: ...
|
|
182
|
+
@overload
|
|
183
|
+
def _option_to_skeleton_bit(
|
|
184
|
+
field: Literal["year"],
|
|
185
|
+
format: YearFormatT,
|
|
186
|
+
) -> YearPatternT | tuple[YearPatternT, ...]: ...
|
|
187
|
+
@overload
|
|
188
|
+
def _option_to_skeleton_bit(
|
|
189
|
+
field: Literal["month"],
|
|
190
|
+
format: MonthFormatT,
|
|
191
|
+
) -> MonthPatternT | tuple[MonthPatternT, ...]: ...
|
|
192
|
+
@overload
|
|
193
|
+
def _option_to_skeleton_bit(
|
|
194
|
+
field: Literal["weekday"],
|
|
195
|
+
format: WeekdayFormatT,
|
|
196
|
+
) -> WeekdayPatternT | tuple[WeekdayPatternT, ...]: ...
|
|
197
|
+
@overload
|
|
198
|
+
def _option_to_skeleton_bit(
|
|
199
|
+
field: Literal["day"],
|
|
200
|
+
format: DayFormatT,
|
|
201
|
+
) -> DayPatternT | tuple[DayPatternT, ...]: ...
|
|
202
|
+
@overload
|
|
203
|
+
def _option_to_skeleton_bit(
|
|
204
|
+
field: Literal["day_period"],
|
|
205
|
+
format: DayPeriodFormatT,
|
|
206
|
+
) -> DayPeriodPatternT | tuple[DayPeriodPatternT, ...]: ...
|
|
207
|
+
@overload
|
|
208
|
+
def _option_to_skeleton_bit(
|
|
209
|
+
field: Literal["hour"],
|
|
210
|
+
format: HourFormatT,
|
|
211
|
+
) -> HourPatternT | tuple[HourPatternT, ...]: ...
|
|
212
|
+
@overload
|
|
213
|
+
def _option_to_skeleton_bit(
|
|
214
|
+
field: Literal["minute"],
|
|
215
|
+
format: MinuteFormatT,
|
|
216
|
+
) -> MinutePatternT | tuple[MinutePatternT, ...]: ...
|
|
217
|
+
@overload
|
|
218
|
+
def _option_to_skeleton_bit(
|
|
219
|
+
field: Literal["second"],
|
|
220
|
+
format: SecondFormatT,
|
|
221
|
+
) -> SecondPatternT | tuple[SecondPatternT, ...]: ...
|
|
222
|
+
@overload
|
|
223
|
+
def _option_to_skeleton_bit(
|
|
224
|
+
field: Literal["fraction_second_digits"],
|
|
225
|
+
format: FractionSecondDigitsFormatT,
|
|
226
|
+
) -> FractionSecondDigitsPatternT | tuple[FractionSecondDigitsPatternT, ...]: ...
|
|
227
|
+
@overload
|
|
228
|
+
def _option_to_skeleton_bit(
|
|
229
|
+
field: Literal["time_zone_name"],
|
|
230
|
+
format: TimezoneNameFormatT,
|
|
231
|
+
) -> TimezoneNamePatternT | tuple[TimezoneNamePatternT, ...]: ...
|
|
232
|
+
def _option_to_skeleton_bit(
|
|
233
|
+
field,
|
|
234
|
+
format,
|
|
235
|
+
):
|
|
236
|
+
return OPTIONS_TO_SKELETON[field][format]
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def _options_to_possible_skeletons(options: DateTimeFormatOptions) -> Iterable[str]:
|
|
240
|
+
datetime_code_bits: list[str | tuple[str, ...]] = []
|
|
241
|
+
|
|
242
|
+
# For order see babel.dates.PATTERN_CHAR_ORDER
|
|
243
|
+
if options.era:
|
|
244
|
+
datetime_code_bits.append(_option_to_skeleton_bit("era", options.era))
|
|
245
|
+
if options.year:
|
|
246
|
+
datetime_code_bits.append(_option_to_skeleton_bit("year", options.year))
|
|
247
|
+
if options.month:
|
|
248
|
+
datetime_code_bits.append(_option_to_skeleton_bit("month", options.month))
|
|
249
|
+
if options.weekday:
|
|
250
|
+
datetime_code_bits.append(_option_to_skeleton_bit("weekday", options.weekday))
|
|
251
|
+
if options.day:
|
|
252
|
+
datetime_code_bits.append(_option_to_skeleton_bit("day", options.day))
|
|
253
|
+
if options.day_period:
|
|
254
|
+
datetime_code_bits.append(_option_to_skeleton_bit("day_period", options.day_period))
|
|
255
|
+
if options.hour:
|
|
256
|
+
datetime_code_bits.append(_option_to_skeleton_bit("hour", options.hour))
|
|
257
|
+
if options.minute:
|
|
258
|
+
datetime_code_bits.append(_option_to_skeleton_bit("minute", options.minute))
|
|
259
|
+
if options.second:
|
|
260
|
+
datetime_code_bits.append(_option_to_skeleton_bit("second", options.second))
|
|
261
|
+
if options.fraction_second_digits:
|
|
262
|
+
datetime_code_bits.append(
|
|
263
|
+
_option_to_skeleton_bit("fraction_second_digits", options.fraction_second_digits),
|
|
264
|
+
)
|
|
265
|
+
if options.time_zone_name:
|
|
266
|
+
datetime_code_bits.append(_option_to_skeleton_bit("time_zone_name", options.time_zone_name))
|
|
267
|
+
|
|
268
|
+
def generate_skeletons(prefix: str, remaining: list[str | tuple[str, ...]]) -> Iterable[str]:
|
|
269
|
+
if not remaining:
|
|
270
|
+
yield prefix
|
|
271
|
+
return
|
|
272
|
+
|
|
273
|
+
next_bit = remaining[0]
|
|
274
|
+
if isinstance(next_bit, tuple):
|
|
275
|
+
for next_bit_variant in next_bit:
|
|
276
|
+
yield from generate_skeletons(prefix + next_bit_variant, remaining[1:])
|
|
277
|
+
else:
|
|
278
|
+
yield from generate_skeletons(prefix + next_bit, remaining[1:])
|
|
279
|
+
|
|
280
|
+
yield from generate_skeletons("", datetime_code_bits)
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
@dataclasses.dataclass(frozen=True, kw_only=True)
|
|
284
|
+
class MatchedFormatPattern:
|
|
285
|
+
skeleton: str
|
|
286
|
+
pattern: str
|
|
287
|
+
|
|
288
|
+
def __str__(self) -> str:
|
|
289
|
+
return self.pattern
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
class FormatPatternNotFoundException(Exception):
|
|
293
|
+
pass
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
@cache
|
|
297
|
+
def _options_to_format_pattern(
|
|
298
|
+
locale: icu.Locale, # ty: ignore[unresolved-attribute]
|
|
299
|
+
options: DateTimeFormatOptions,
|
|
300
|
+
) -> MatchedFormatPattern:
|
|
301
|
+
possible_skeletons = list(_options_to_possible_skeletons(options))
|
|
302
|
+
|
|
303
|
+
generator = icu.DateTimePatternGenerator.createInstance(locale) # ty: ignore[unresolved-attribute]
|
|
304
|
+
|
|
305
|
+
# Try a perfect match
|
|
306
|
+
for skeleton in possible_skeletons:
|
|
307
|
+
pattern = generator.getPatternForSkeleton(skeleton)
|
|
308
|
+
if pattern:
|
|
309
|
+
return MatchedFormatPattern(
|
|
310
|
+
skeleton=skeleton,
|
|
311
|
+
pattern=pattern,
|
|
312
|
+
)
|
|
313
|
+
|
|
314
|
+
# Try to find best match
|
|
315
|
+
for skeleton in possible_skeletons:
|
|
316
|
+
pattern = generator.getBestPattern(skeleton)
|
|
317
|
+
if pattern:
|
|
318
|
+
return MatchedFormatPattern(
|
|
319
|
+
skeleton=skeleton,
|
|
320
|
+
pattern=pattern,
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
raise FormatPatternNotFoundException("Didn't find pattern for desired options")
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
class DateTimeFormat:
|
|
327
|
+
locale: str
|
|
328
|
+
options: DateTimeFormatOptions
|
|
329
|
+
|
|
330
|
+
def __init__(
|
|
331
|
+
self,
|
|
332
|
+
locale: str,
|
|
333
|
+
options: DateTimeFormatOptions | DateTimeFormatOptionsDictT,
|
|
334
|
+
) -> None:
|
|
335
|
+
self.locale = locale
|
|
336
|
+
if isinstance(options, DateTimeFormatOptions):
|
|
337
|
+
self.options = options
|
|
338
|
+
else:
|
|
339
|
+
self.options = DateTimeFormatOptions(**options)
|
|
340
|
+
|
|
341
|
+
@cached_property
|
|
342
|
+
def icu_locale(self) -> icu.Locale: # ty: ignore[unresolved-attribute]
|
|
343
|
+
return icu.Locale(self.locale) # ty: ignore[unresolved-attribute]
|
|
344
|
+
|
|
345
|
+
@cached_property
|
|
346
|
+
def matched_pattern(self) -> MatchedFormatPattern:
|
|
347
|
+
return _options_to_format_pattern(self.icu_locale, self.options)
|
|
348
|
+
|
|
349
|
+
@cached_property
|
|
350
|
+
def icu_pattern(self) -> str:
|
|
351
|
+
return self.matched_pattern.pattern
|
|
352
|
+
|
|
353
|
+
@cached_property
|
|
354
|
+
def icu_date_format(self) -> icu.SimpleDateFormat: # ty: ignore[unresolved-attribute]
|
|
355
|
+
return icu.SimpleDateFormat(self.icu_pattern, self.icu_locale) # ty: ignore[unresolved-attribute]
|
|
356
|
+
|
|
357
|
+
def format(self, datetime_: dt.datetime, /) -> str:
|
|
358
|
+
return self.icu_date_format.format(datetime_)
|