|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +thai_time() - Spell out time to Thai words |
| 4 | +""" |
| 5 | +from datetime import datetime, time |
| 6 | +from typing import Union |
| 7 | + |
| 8 | +from pythainlp.util.numtoword import num_to_thaiword |
| 9 | + |
| 10 | +_TIME_FORMAT_WITH_SEC = "%H:%M:%S" |
| 11 | +_TIME_FORMAT_WITHOUT_SEC = "%H:%M" |
| 12 | + |
| 13 | + |
| 14 | +def _format_6h(h: int) -> str: |
| 15 | + """ |
| 16 | + Thai time (6-hour clock) |
| 17 | + """ |
| 18 | + text = "" |
| 19 | + |
| 20 | + if h == 0: |
| 21 | + text += "เที่ยงคืน" |
| 22 | + elif h < 7: |
| 23 | + text += "ตี" + num_to_thaiword(h) |
| 24 | + elif h < 12: |
| 25 | + text += num_to_thaiword(h - 6) + "โมงเช้า" |
| 26 | + elif h == 12: |
| 27 | + text += "เที่ยง" |
| 28 | + elif h < 18: |
| 29 | + if h == 13: |
| 30 | + text += "บ่ายโมง" |
| 31 | + else: |
| 32 | + text += "บ่าย" + num_to_thaiword(h - 12) + "โมง" |
| 33 | + elif h == 18: |
| 34 | + text += "หกโมงเย็น" |
| 35 | + else: |
| 36 | + text += num_to_thaiword(h - 18) + "ทุ่ม" |
| 37 | + |
| 38 | + return text |
| 39 | + |
| 40 | + |
| 41 | +def _format_m6h(h: int) -> str: |
| 42 | + """ |
| 43 | + Thai time (modified 6-hour clock) |
| 44 | + """ |
| 45 | + text = "" |
| 46 | + |
| 47 | + if h == 0: |
| 48 | + text += "เที่ยงคืน" |
| 49 | + elif h < 6: |
| 50 | + text += "ตี" + num_to_thaiword(h) |
| 51 | + elif h < 12: |
| 52 | + text += num_to_thaiword(h) + "โมง" |
| 53 | + elif h == 12: |
| 54 | + text += "เที่ยง" |
| 55 | + elif h < 19: |
| 56 | + text += num_to_thaiword(h - 12) + "โมง" |
| 57 | + else: |
| 58 | + text += num_to_thaiword(h - 18) + "ทุ่ม" |
| 59 | + |
| 60 | + return text |
| 61 | + |
| 62 | + |
| 63 | +def _format_24h(h: int) -> str: |
| 64 | + """ |
| 65 | + Thai time (24-hour clock) |
| 66 | + """ |
| 67 | + text = num_to_thaiword(h) + "นาฬิกา" |
| 68 | + return text |
| 69 | + |
| 70 | + |
| 71 | +def _format( |
| 72 | + h: int, |
| 73 | + m: int, |
| 74 | + s: int, |
| 75 | + fmt: str = "24h", |
| 76 | + precision: Union[str, None] = None, |
| 77 | +) -> str: |
| 78 | + text = "" |
| 79 | + if fmt == "6h": |
| 80 | + text = _format_6h(h) |
| 81 | + elif fmt == "m6h": |
| 82 | + text = _format_m6h(h) |
| 83 | + elif fmt == "24h": |
| 84 | + text = _format_24h(h) |
| 85 | + else: |
| 86 | + raise NotImplementedError(fmt) |
| 87 | + |
| 88 | + if precision == "minute" or precision == "second": |
| 89 | + if ( |
| 90 | + m == 30 |
| 91 | + and (s == 0 or precision == "minute") |
| 92 | + and (fmt == "6h" or fmt == "m6h") |
| 93 | + ): |
| 94 | + text += "ครึ่ง" |
| 95 | + else: |
| 96 | + text += num_to_thaiword(m) + "นาที" |
| 97 | + if precision == "second": |
| 98 | + text += num_to_thaiword(s) + "วินาที" |
| 99 | + else: |
| 100 | + if m: |
| 101 | + if m == 30 and s == 0 and (fmt == "6h" or fmt == "m6h"): |
| 102 | + text += "ครึ่ง" |
| 103 | + else: |
| 104 | + text += num_to_thaiword(m) + "นาที" |
| 105 | + if s: |
| 106 | + text += num_to_thaiword(s) + "วินาที" |
| 107 | + |
| 108 | + return text |
| 109 | + |
| 110 | + |
| 111 | +def thai_time( |
| 112 | + time_data: Union[time, datetime, str], |
| 113 | + fmt: str = "24h", |
| 114 | + precision: Union[str, None] = None, |
| 115 | +) -> str: |
| 116 | + """ |
| 117 | + Spell out time to Thai words. |
| 118 | +
|
| 119 | + :param str time_data: time input, can be a datetime.time object \ |
| 120 | + or a datetime.datetime object \ |
| 121 | + or a string (in H:M or H:M:S format, using 24-hour clock) |
| 122 | + :param str fmt: time output format |
| 123 | + * *24h* - 24-hour clock (default) |
| 124 | + * *6h* - 6-hour clock |
| 125 | + * *m6h* - Modified 6-hour clock |
| 126 | + :param str precision: precision of the spell out |
| 127 | + * *minute* - always spell out to minute level |
| 128 | + * *second* - always spell out to second level |
| 129 | + * None - spell out only non-zero parts |
| 130 | + :return: Time spell out in Thai words |
| 131 | + :rtype: str |
| 132 | +
|
| 133 | + :Example: |
| 134 | +
|
| 135 | + thai_time("8:17") |
| 136 | + # output: |
| 137 | + # แปดนาฬิกาสิบเจ็ดนาที |
| 138 | +
|
| 139 | + thai_time("8:17", "6h") |
| 140 | + # output: |
| 141 | + # สองโมงเช้าสิบเจ็ดนาที |
| 142 | +
|
| 143 | + thai_time("8:17", "m6h") |
| 144 | + # output: |
| 145 | + # แปดโมงสิบเจ็ดนาที |
| 146 | +
|
| 147 | + thai_time("18:30", fmt="m6h") |
| 148 | + # output: |
| 149 | + # หกโมงครึ่ง |
| 150 | +
|
| 151 | + thai_time(datetime.time(12, 3, 0)) |
| 152 | + # output: |
| 153 | + # สิบสองนาฬิกาสามนาที |
| 154 | +
|
| 155 | + thai_time(datetime.time(12, 3, 0), precision="second") |
| 156 | + # output: |
| 157 | + # สิบสองนาฬิกาสามนาทีศูนย์วินาที |
| 158 | + """ |
| 159 | + _time = None |
| 160 | + |
| 161 | + if isinstance(time_data, time) or isinstance(time_data, datetime): |
| 162 | + _time = time_data |
| 163 | + else: |
| 164 | + if not isinstance(time_data, str): |
| 165 | + raise TypeError( |
| 166 | + "Time data must be a datetime.time object, a datetime.datetime object, or a string." |
| 167 | + ) |
| 168 | + |
| 169 | + if not time_data: |
| 170 | + raise ValueError("Time string cannot be empty.") |
| 171 | + |
| 172 | + try: |
| 173 | + _time = datetime.strptime(time_data, _TIME_FORMAT_WITH_SEC) |
| 174 | + except ValueError: |
| 175 | + try: |
| 176 | + _time = datetime.strptime(time_data, _TIME_FORMAT_WITHOUT_SEC) |
| 177 | + except ValueError: |
| 178 | + pass |
| 179 | + |
| 180 | + if not _time: |
| 181 | + raise ValueError( |
| 182 | + f"Time string '{time_data}' does not match H:M or H:M:S format." |
| 183 | + ) |
| 184 | + |
| 185 | + text = _format(_time.hour, _time.minute, _time.second, fmt, precision) |
| 186 | + |
| 187 | + return text |
0 commit comments