File size: 11,801 Bytes
e4b9a7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2025 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""
Thai date/time conversion.

Note: It does not take into account the change of new year's day in Thailand
"""

# BE คือ พ.ศ.
# AD คือ ค.ศ.
# AH ปีฮิจเราะห์ศักราชเป็นปีพุทธศักราช จะต้องบวกด้วย 1122
# ไม่ได้รองรับปี พ.ศ. ก่อนการเปลี่ยนวันขึ้นปีใหม่ของประเทศไทย

__all__ = [
    "convert_years",
    "thai_abbr_months",
    "thai_abbr_weekdays",
    "thai_full_months",
    "thai_full_weekdays",
    "thai_strptime",
    "thaiword_to_date",
]

import re
from datetime import datetime, timedelta
from typing import Union

try:
    from zoneinfo import ZoneInfo
except ImportError:
    from backports.zoneinfo import ZoneInfo


thai_abbr_weekdays = ["จ", "อ", "พ", "พฤ", "ศ", "ส", "อา"]
thai_full_weekdays = [
    "วันจันทร์",
    "วันอังคาร",
    "วันพุธ",
    "วันพฤหัสบดี",
    "วันศุกร์",
    "วันเสาร์",
    "วันอาทิตย์",
]

thai_abbr_months = [
    "ม.ค.",
    "ก.พ.",
    "มี.ค.",
    "เม.ย.",
    "พ.ค.",
    "มิ.ย.",
    "ก.ค.",
    "ส.ค.",
    "ก.ย.",
    "ต.ค.",
    "พ.ย.",
    "ธ.ค.",
]
thai_full_months = [
    "มกราคม",
    "กุมภาพันธ์",
    "มีนาคม",
    "เมษายน",
    "พฤษภาคม",
    "มิถุนายน",
    "กรกฎาคม",
    "สิงหาคม",
    "กันยายน",
    "ตุลาคม",
    "พฤศจิกายน",
    "ธันวาคม",
]
thai_full_month_lists = [
    ["มกราคม", "มกรา", "ม.ค.", "01", "1"],
    ["กุมภาพันธ์", "กุมภา", "ก.พ.", "02", "2"],
    ["มีนาคม", "มีนา", "มี.ค.", "03", "3"],
    ["เมษายน", "เมษา", "เม.ย.", "04", "4"],
    ["พฤษภาคม", "พฤษภา", "พ.ค.", "05", "5"],
    ["มิถุนายน", "มิถุนา", "มิ.ย.", "06", "6"],
    ["กรกฎาคม", "ก.ค.", "07", "7"],
    ["สิงหาคม", "สิงหา", "ส.ค.", "08", "8"],
    ["กันยายน", "กันยา", "ก.ย.", "09", "9"],
    ["ตุลาคม", "ตุลา", "ต.ค.", "10"],
    ["พฤศจิกายน", "พฤศจิกา", "พ.ย.", "11"],
    ["ธันวาคม", "ธันวา", "ธ.ค.", "12"]
]
thai_full_month_lists_regex = "(" + '|'.join(
    ['|'.join(i) for i in thai_full_month_lists]
) + ")"
year_all_regex = r"(\d\d\d\d|\d\d)"
dates_list = "(" + '|'.join(
    [str(i) for i in range(32, 0, -1)] + [
        "0" + str(i) for i in range(1, 10)
    ]
) + ")"

_DAY = {
    "วันนี้": 0,
    "คืนนี้": 0,
    "พรุ่งนี้": 1,
    "วันพรุ่งนี้": 1,
    "คืนถัดจากนี้": 1,
    "คืนหน้า": 1,
    "มะรืน": 2,
    "มะรืนนี้": 2,
    "วันมะรืนนี้": 2,
    "ถัดจากพรุ่งนี้": 2,
    "ถัดจากวันพรุ่งนี้": 2,
    "เมื่อวาน": -1,
    "เมื่อวานนี้": -1,
    "วานนี้": -1,
    "เมื่อคืน": -1,
    "เมื่อคืนนี้": -1,
    "วานซืน": -2,
    "เมื่อวานซืน": -2,
    "เมื่อวานของเมื่อวาน": -2,
}


def convert_years(year: str, src="be", target="ad") -> str:
    """
    Convert years

    :param int year: Year
    :param str src: The source year
    :param str target: The target year
    :return: The converted year
    :rtype: str

    **Options for year**
        * *be* - Buddhist calendar
        * *ad* - Anno Domini
        * *re* - Rattanakosin era
        * *ah* - Anno Hejira

    **Warning**: This function works properly only after 1941 \
    because Thailand has change the Thai calendar in 1941.
    If you are the time traveler or the historian, \
    you should care about the correct calendar.
    """
    output_year = None
    if src == "be":
        # พ.ศ. - 543  = ค.ศ.
        if target == "ad":
            output_year = str(int(year) - 543)
        # พ.ศ. - 2324 = ร.ศ. 
        elif target == "re":
            output_year = str(int(year) - 2324)
        # พ.ศ. - 1122 = ฮ.ศ.
        elif target == "ah":
            output_year = str(int(year) - 1122)
    elif src == "ad":
        # ค.ศ. + 543 = พ.ศ.
        if target == "be":
            output_year = str(int(year) + 543)
        # ค.ศ. + 543 - 2324 = ร.ศ.
        elif target == "re":
            output_year = str(int(year) + 543 - 2324)
        # ค.ศ. +543- 1122   = ฮ.ศ.
        elif target == "ah":
            output_year = str(int(year) + 543 - 1122)
    elif src == "re":
        # ร.ศ. + 2324 = พ.ศ.
        if target == "be":
            output_year = str(int(year) + 2324)
        # ร.ศ. + 2324 - 543  = ค.ศ.
        elif target == "ad":
            output_year = str(int(year) + 2324 - 543)
        # ร.ศ. + 2324 - 1122  = ฮ.ศ.
        elif target == "ah":
            output_year = str(int(year) + 2324 - 1122)
    elif src == "ah":
        # ฮ.ศ. + 1122 = พ.ศ.
        if target == "be":
            output_year = str(int(year) + 1122)
        # ฮ.ศ. +1122 - 543= ค.ศ.
        elif target == "ad":
            output_year = str(int(year) + 1122 - 543)
        # ฮ.ศ. +1122 - 2324 = ร.ศ.
        elif target == "re":
            output_year = str(int(year) + 1122 - 2324)
    if output_year is None:
        raise NotImplementedError(
            f"This function doesn't support {src} to {target}"
        )
    return output_year


def _find_month(text):
    for i, m in enumerate(thai_full_month_lists):
        for j in m:
            if j in text:
                return i + 1


def thai_strptime(
    text: str,
    fmt: str,
    year: str = "be",
    add_year: int = None,
    tzinfo=ZoneInfo("Asia/Bangkok")
):
    """
    Thai strptime

    :param str text: text
    :param str fmt: string containing date and time directives
    :param str year: year of the text \
        (ad is Anno Domini and be is Buddhist Era)
    :param int add_year: add to year when converting to ad
    :param object tzinfo: tzinfo (default is Asia/Bangkok)
    :return: The year that is converted to datetime.datetime
    :rtype: datetime.datetime

    The fmt chars that are supported:
        * *%d* - Day (1 - 31)
        * *%B* - Thai month (03, 3, มี.ค., or มีนาคม)
        * *%Y* - Year (66, 2566, or 2023)
        * *%H* - Hour (0 - 23)
        * *%M* - Minute (0 - 59)
        * *%S* - Second (0 - 59)
        * *%f* - Microsecond

    :Example:
    ::

        from pythainlp.util import thai_strptime

        thai_strptime("15 ก.ค. 2565 09:00:01","%d %B %Y %H:%M:%S")
        # output:
        # datetime.datetime(
        #   2022,
        #   7,
        #   15,
        #   9,
        #   0,
        #   1,
        #   tzinfo=zoneinfo.ZoneInfo(key='Asia/Bangkok')
        # )
    """
    d = ""
    m = ""
    y = ""
    fmt = fmt.replace("%-m", "%m")
    fmt = fmt.replace("%-d", "%d")
    fmt = fmt.replace("%b", "%B")
    fmt = fmt.replace("%-y", "%y")
    data = {}
    _old = fmt
    if "%d" in fmt:
        fmt = fmt.replace("%d", dates_list)
    if "%B" in fmt:
        fmt = fmt.replace("%B", thai_full_month_lists_regex)
    if "%Y" in fmt:
        fmt = fmt.replace("%Y", year_all_regex)
    if "%H" in fmt:
        fmt = fmt.replace("%H", r"(\d\d|\d)")
    if "%M" in fmt:
        fmt = fmt.replace("%M", r"(\d\d|\d)")
    if "%S" in fmt:
        fmt = fmt.replace("%S", r"(\d\d|\d)")
    if "%f" in fmt:
        fmt = fmt.replace("%f", r"(\d+)")
    keys = [
        i.strip().strip('-').strip(':').strip('.')
        for i in _old.split("%") if i != ''
    ]
    y = re.findall(fmt, text)

    data = {i: ''.join(list(j)) for i, j in zip(keys, y[0])}
    H = 0
    M = 0
    S = 0
    f = 0
    d = data['d']
    m = _find_month(data['B'])
    y = data['Y']
    if "H" in keys:
        H = data['H']
    if "M" in keys:
        M = data['M']
    if "S" in keys:
        S = data['S']
    if "f" in keys:
        f = data['f']
    if int(y) < 100 and year == "be":
        if add_year is None:
            y = str(2500 + int(y))
        else:
            y = str(int(add_year) + int(y))
    elif int(y) < 100 and year == "ad":
        if add_year is None:
            y = str(2000 + int(y))
        else:
            y = str(int(add_year) + int(y))
    if year == "be":
        y = convert_years(y, src="be", target="ad")
    return datetime(
        year=int(y),
        month=int(m),
        day=int(d),
        hour=int(H),
        minute=int(M),
        second=int(S),
        microsecond=int(f),
        tzinfo=tzinfo
    )


def now_reign_year() -> int:
    """
    Return the reign year of the 10th King of Chakri dynasty.

    :return: reign year of the 10th King of Chakri dynasty.
    :rtype: int

    :Example:
    ::

        from pythainlp.util import now_reign_year

        text = "เป็นปีที่ {reign_year} ในรัชกาลปัจจุบัน"\\
            .format(reign_year=now_reign_year())

        print(text)
        # output: เป็นปีที่ 4 ในรัชการปัจจุบัน
    """
    now_ = datetime.now()
    return now_.year - 2015


def reign_year_to_ad(reign_year: int, reign: int) -> int:
    """
    Convert reign year to AD.

    Return AD year according to the reign year for
    the 7th to 10th King of Chakri dynasty, Thailand.
    For instance, the AD year of the 4th reign year of the 10th King is 2019.

    :param int reign_year: reign year of the King
    :param int reign: the reign of the King (i.e. 7, 8, 9, and 10)

    :return: the year in AD of the King given the reign and reign year.
    :rtype: int

    :Example:
    ::

        from pythainlp.util import reign_year_to_ad

        print("The 4th reign year of the King Rama X is in", \\
            reign_year_to_ad(4, 10))
        # output: The 4th reign year of the King Rama X is in 2019

        print("The 1st reign year of the King Rama IX is in", \\
            reign_year_to_ad(1, 9))
        # output: The 4th reign year of the King Rama X is in 1946
    """
    if int(reign) == 10:
        ad = int(reign_year) + 2015
    elif int(reign) == 9:
        ad = int(reign_year) + 1945
    elif int(reign) == 8:
        ad = int(reign_year) + 1928
    elif int(reign) == 7:
        ad = int(reign_year) + 1924
    return ad


def thaiword_to_date(
    text: str, date: datetime = None
) -> Union[datetime, None]:
    """
    Convert Thai relative date to :class:`datetime.datetime`.

    :param str text: Thai text containing relative date
    :param datetime.datetime date: date (default is datetime.datetime.now())

    :return: datetime object, if it can be calculated. Otherwise, None.
    :rtype: datetime.datetime

    :Example:

        thaiword_to_date("พรุ่งนี้")
        # output:
        # datetime of tomorrow
    """
    if text not in _DAY:
        return None

    day_num = _DAY.get(text)

    if not date:
        date = datetime.now()

    return date + timedelta(days=day_num)