a
     g.                     @   s  d Z g dZddlmZmZ ddlmZ ddlZzddlmZ W n e	y^   ddl
mZ Y n0 g dZg dZg d	Zg d
Zg dg dg dg dg dg dg dg dg dg dg dg dgZdddd eD  d ZdZdddd edddD d d ed!d"D   d Zddd!d!d!d!d#d#d#d#d#dddddd$d$d$d%Zd:eed(d)d*Zd+d, Zd&ded-feeeed.d/d0Zed1d2d3Zeeed4d5d6Zd;eeeedf d7d8d9ZdS )<zj
Thai date/time conversion.

Note: It does not take into account the change of new year's day in Thailand
)convert_yearsthai_abbr_monthsthai_abbr_weekdaysthai_full_monthsthai_full_weekdaysthai_strptimethaiword_to_date    )datetime	timedelta)UnionN)ZoneInfo)u   จu   อu   พu   พฤu   ศu   สu   อา)u   วันจันทร์u   วันอังคารu   วันพุธu!   วันพฤหัสบดีu   วันศุกร์u   วันเสาร์u   วันอาทิตย์)   ม.ค.   ก.พ.   มี.ค.   เม.ย.   พ.ค.   มิ.ย.   ก.ค.   ส.ค.   ก.ย.   ต.ค.   พ.ย.   ธ.ค.)   มกราคม   กุมภาพันธ์   มีนาคม   เมษายน   พฤษภาคม   มิถุนายน   กรกฎาคม   สิงหาคม   กันยายน   ตุลาคม   พฤศจิกายน   ธันวาคม)r   u   มกราr   Z011)r   u   กุมภาr   022)r   u   มีนาr   Z033)r   u   เมษาr   Z044)r   u   พฤษภาr   Z055)r   u   มิถุนาr   Z066)r   r   Z077)r    u   สิงหาr   Z088)r!   u   กันยาr   Z099)r"   u   ตุลาr   10)r#   u   พฤศจิกาr   Z11)r$   u   ธันวาr   Z12(|c                 C   s   g | ]}d  |qS )r1   )join.0i r6   =/usr/local/lib/python3.9/dist-packages/pythainlp/util/date.py
<listcomp>Y       r8   )z(\d\d\d\d|\d\d)c                 C   s   g | ]}t |qS r6   strr3   r6   r6   r7   r8   ]   r9       c                 C   s   g | ]}d t | qS )0r;   r3   r6   r6   r7   r8   ]   s      
      )u   วันนี้u   คืนนี้u   พรุ่งนี้u!   วันพรุ่งนี้u$   คืนถัดจากนี้u   คืนหน้าu   มะรืนu   มะรืนนี้u!   วันมะรืนนี้u*   ถัดจากพรุ่งนี้u3   ถัดจากวันพรุ่งนี้u   เมื่อวานu!   เมื่อวานนี้u   วานนี้u   เมื่อคืนu!   เมื่อคืนนี้u   วานซืนu!   เมื่อวานซืนu9   เมื่อวานของเมื่อวานbead)yearreturnc                 C   s  d}|dkr\|dkr&t t| d }n2|dkr@t t| d }n|dkrXt t| d }n$|dkr|dkr~t t| d }n:|dkrt t| d d }n|dkrt t| d d }n|dkr|dkrt t| d }n<|dkrt t| d d }n|dkrt t| d d }nd|dkr|dkrBt t| d }n>|dkrbt t| d d }n|dkrt t| d d }|du rtd	| d
| |S )a#  
    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.
    NrD   rE   i  rei	  Zahib  zThis function doesn't support z to )r<   intNotImplementedError)rF   srctargetZoutput_yearr6   r6   r7   r   y   sD    






r   c                 C   s8   t tD ]*\}}|D ]}|| v r|d     S qqd S )Nr@   )	enumeratethai_full_month_lists)textr5   mjr6   r6   r7   _find_month   s    rR   zAsia/Bangkok)rO   fmtrF   add_yearc              
   C   sZ  d}d}d}| dd}| dd}| dd}| dd	}i }|}	d|v rX| dt}d|v rl| dt}d
|v r| d
t}d|v r| dd}d|v r| dd}d|v r| dd}d|v r| dd}dd |	dD }
t|| }dd t|
|d D }d}d}d}d}|d }t|d }|d }d|
v rF|d }d|
v rX|d }d|
v rj|d }d|
v r||d }t	|dk r|dkr|d u rt
d!t	| }nt
t	|t	| }nHt	|dk r|d"kr|d u rt
d#t	| }nt
t	|t	| }|dkr&t|dd"d$}tt	|t	|t	|t	|t	|t	|t	||d%S )&u<  
    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=backports.zoneinfo.ZoneInfo(key='Asia/Bangkok')
        # )
     z%-mz%mz%-dz%dz%bz%Bz%-yz%yz%Yz%Hz	(\d\d|\d)z%Mz%Sz%fz(\d+)c                 S   s.   g | ]&}|d kr|   d d dqS )rU   -:.)stripr3   r6   r6   r7   r8   	  s   
z!thai_strptime.<locals>.<listcomp>%c                 S   s    i | ]\}}|d  t|qS )rU   )r2   list)r4   r5   rQ   r6   r6   r7   
<dictcomp>  r9   z!thai_strptime.<locals>.<dictcomp>r   dBYHMSfd   rD   Ni	  rE   i  )rK   rL   )rF   monthdayhourminutesecondmicrosecondtzinfo)replace
dates_listthai_full_month_lists_regexyear_all_regexsplitrH   findallziprR   rI   r<   r   r	   )rO   rS   rF   rT   rk   r]   rP   ydataZ_oldkeysr`   ra   rb   rc   r6   r6   r7   r      s~    -






r   )rG   c                  C   s   t  } | jd S )u  
    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 ในรัชการปัจจุบัน
      )r	   nowrF   )Znow_r6   r6   r7   now_reign_year7  s    rx   )
reign_yearreignrG   c                 C   sj   t |dkrt | d }nLt |dkr4t | d }n2t |dkrNt | d }nt |dkrft | d }|S )	aE  
    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
    rA   rv   	   i     i     i  )rI   )ry   rz   rE   r6   r6   r7   reign_year_to_adM  s    r~   )rO   daterG   c                 C   s0   | t vrdS t | }|s"t }|t|d S )u  
    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
    N)days)_DAYgetr	   rw   r
   )rO   r   Zday_numr6   r6   r7   r   s  s    
r   )rD   rE   )N)__doc____all__r	   r
   typingr   rH   Zzoneinfor   ImportErrorZbackports.zoneinfor   r   r   r   rN   r2   rn   ro   rangerm   r   r<   r   rR   rI   r   rx   r~   r   r6   r6   r6   r7   <module>   s   

E
r' 
