a
     g3                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
mZ dgZeeeZdZd	Zd
ZeeedddZeeedddZdeeeedddZdS )z
Thai date/time formatting.
    N)datetime)digits)thai_digits)thai_abbr_monthsthai_abbr_weekdaysthai_full_monthsthai_full_weekdaysthai_strftimei  zAaBbCcDFGgvXxYy+zEO-_0^#)dt_objfmt_charreturnc              
   C   st   d}z*|  d| }|r(|d|kr,|}W n@ tyn } z(td| d| t |}W Y d}~n
d}~0 0 |S )zQ
    Standard datetime.strftime() with normalization and exception handling.
     %z%{}z.String format directive unknown/not support: %z#The system raises this ValueError: N)strftimeformat
ValueErrorwarningswarnUserWarning)r
   r   str_err r   A/usr/local/lib/python3.9/dist-packages/pythainlp/util/strftime.py_std_strftime   s    r   c              	   C   s  d}|dkrt |   }n|dkr4t|   }n|dkrNt| jd  }np|dkrht| jd  }nV|dkrtt| jt	 d d 
d	}n*|d
krdt|   | jt| jd  | dt| jt	 
d}n|dkrd| dt| jt	 dd 
d	}n|dkrDdt| jt	 
d| d}nz|dkrntt| dt	 
d}nP|dkrtt| dt	 dd 
d	}n|dkrd| jt| jd  t| jt	 
d}n|dkr| d}n|dkr.dt| j
d	t| j
d	t| jt	 
d}n|dkrNt| jt	 
d}np|dkrvt| jt	 dd 
d	}nH|d krdt|   | jt| jd  | jt	 | d}n
t| |}|S )!zw
    Conversion support for thai_strftime().

    The fmt_char should be in _NEED_L10N when calling this function.
    r   AaB   bCd      cz{:<2} {:>2} {} {} {}z%H:%M:%S   Dz{}/{}z%m/%dNFz{}-{}z%m-%dGz%Ggvz{:>2}-{}-{}Xxz{}/{}/{}Yy+)r   weekdayr   r   monthr   strintyear_BE_AD_DIFFERENCEzfillr   dayr   r   )r
   r   r   r   r   r   _thai_strftime8   s    $



 







	
r7   	%-d %b %yF)r
   fmt	thaidigitr   c                 C   s  g }d}t |}||k rd}|| dkr|d }||k r|| }|tv r\t| |}n4|tv r|}	|d }
|
|k r||
 }|tv rt| |}n
t| |}|	dkr|d r|d dv r|dd }n|	dkr|d r|d d	krd
|dd  }n||	d	kr.|d rv|d d
krvd	|dd  }nH|	dkrB| }n4|	dkrV| }n |	dkrbn|	dkrv|t}|d }n|	}n
t| |}|d }nd}n|| }|	| |d }qd
|}|r|t}|S )u1  
    Convert :class:`datetime.datetime` into Thai date and time format.

    The formatting directives are similar to :func:`datatime.strrftime`.

    This function uses Thai names and Thai Buddhist Era for these directives:
        * **%a** - abbreviated weekday name
          (i.e. "จ", "อ", "พ", "พฤ", "ศ", "ส", "อา")
        * **%A** - full weekday name
          (i.e. "วันจันทร์", "วันอังคาร", "วันเสาร์", "วันอาทิตย์")
        * **%b** - abbreviated month name
          (i.e. "ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.", "ธ.ค.")
        * **%B** - full month name
          (i.e. "มกราคม", "กุมภาพันธ์", "พฤศจิกายน", "ธันวาคม",)
        * **%y** - year without century (i.e. "56", "10")
        * **%Y** - year with century (i.e. "2556", "2410")
        * **%c** - date and time representation
          (i.e. "พ   6 ต.ค. 01:40:00 2519")
        * **%v** - short date representation
          (i.e. " 6-ม.ค.-2562", "27-ก.พ.-2555")

    Other directives will be passed to datetime.strftime()

    :Note:
        * The Thai Buddhist Era (BE) year is simply converted from AD
          by adding 543. This is certainly not accurate for years
          before 1941 AD, due to the change in Thai New Year's Day.
        * This meant to be an interim solution, since
          Python standard's locale module (which relied on C's strftime())
          does not support "th" or "th_TH" locale yet. If supported,
          we can just locale.setlocale(locale.LC_TIME, "th_TH")
          and then use native datetime.strftime().

    We are trying to make this platform-independent and support extensions
    as many as possible. See these links for strftime() extensions
    in POSIX, BSD, and GNU libc:

        * Python
          https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
        * C http://www.cplusplus.com/reference/ctime/strftime/
        * GNU https://metacpan.org/pod/POSIX::strftime::GNU
        * Linux https://linux.die.net/man/3/strftime
        * OpenBSD https://man.openbsd.org/strftime.3
        * FreeBSD https://www.unix.com/man-page/FreeBSD/3/strftime/
        * macOS
          https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/strftime.3.html
        * PHP https://secure.php.net/manual/en/function.strftime.php
        * JavaScript's implementation https://github.com/samsonjs/strftime
        * strftime() quick reference http://www.strftime.net/

    :param datetime dt_obj: an instantiatetd object of
                            :mod:`datetime.datetime`
    :param str fmt: string containing date and time directives
    :param bool thaidigit: If `thaidigit` is set to **False** (default),
                           number will be represented in Arabic digit.
                           If it is set to **True**, it will be represented
                           in Thai digit.

    :return: Date and time text, with month in Thai name and year in
             Thai Buddhist era. The year is simply converted from AD
             by adding 543 (will not accurate for years before 1941 AD,
             due to change in Thai New Year's Day).
    :rtype: str

    :Example:
    ::

        from datetime import datetime
        from pythainlp.util import thai_strftime

        datetime_obj = datetime(year=2019, month=6, day=9, \
            hour=5, minute=59, second=0, microsecond=0)

        print(datetime_obj)
        # output: 2019-06-09 05:59:00

        thai_strftime(datetime_obj, "%A %d %B %Y")
        # output: 'วันอาทิตย์ 09 มิถุนายน 2562'

        thai_strftime(datetime_obj, "%a %-d %b %y")  # no padding
        # output: 'อา 9 มิ.ย. 62'

        thai_strftime(datetime_obj, "%a %_d %b %y")  # space padding
        # output: 'อา  9 มิ.ย. 62'

        thai_strftime(datetime_obj, "%a %0d %b %y")  # zero padding
        # output: 'อา 09 มิ.ย. 62'

        thai_strftime(datetime_obj, "%-H นาฬิกา %-M นาที", thaidigit=True)
        # output: '๕ นาฬิกา ๕๙ นาที'

        thai_strftime(datetime_obj, "%D (%v)")
        # output: '06/09/62 ( 9-มิ.ย.-2562)'

        thai_strftime(datetime_obj, "%c")
        # output: 'อา  9 มิ.ย. 05:59:00 2562'

        thai_strftime(datetime_obj, "%H:%M %p")
        # output: '01:40 AM'

        thai_strftime(datetime_obj, "%H:%M %#p")
        # output: '01:40 am'
    r   r   r   r   -z 0N_0 ^#EO)len
_NEED_L10Nr7   _EXTENSIONSr   upperswapcase	translate_HA_TH_DIGITSappendjoin)r
   r9   r:   Zthaidate_partsiZfmt_lenr   jr   Zfmt_char_extkZthaidate_textr   r   r   r	      s`    l



















)r8   F)__doc__r   r   stringr   Z	pythainlpr   Zpythainlp.util.dater   r   r   r   __all__r1   	maketransrI   r4   rD   rE   r   r7   boolr	   r   r   r   r   <module>   s*   c  