B
    d                 @   s  d Z ddlZddlmZ ddlmZmZ	m
ZmZ ddlmZ dZejZejZdd	d
dddddddddZeddddddZeddddddZdd Zdd Zdd Zdd Zdd  Zd)d!d"Zd*d#d$Zd+d%d&Ze d'k re Z!ee!Z"e#d(e"e!f  dS ),zLUtilties for calculating and modifying GPS times using the LAL date
package
    N)parse   )LIGOTimeGPS
GPSTimeNowGPSToUTCUTCToGPS)git_versionz(Duncan Macleod <duncan.macleod@ligo.org>iiiiii  i   )PSTPDTCSTCDTESTEDTGMTUTCBSTZCETZCESTi     i            7   c             C   s$   | t k rtd| tkr tddS )zHCheck whether this UTC datetime will convert to a valid LIGOTimeGPS
    z2Given UTC time is before the start of the GPS era.zDGiven UTC time is too far in the future, LAL will SegmentationFault.N)	GPS_EPOCH
ValueErrorLAL_GPS_MAX)utc_time r   X/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/lal/gpstime.py
_check_utcI   s
    r   c               C   s   t  S )zDGet the current time in GPS seconds

    @returns a LIGOTimeGPS
    )_gps_time_nowr   r   r   r   gps_time_nowT   s    r   c             C   s6   t | tjstj| t } t|  tt|  S )zVConvert the given `datetime.datetime` into a GPS time

    @returns a LIGOTimeGPS
    )	
isinstance	_datetimedatetimecombinetimer   r   _utc_to_gpsutctimetuple)r   r   r   r   
utc_to_gps\   s    r'   c             C   sL   t | } tjt| jdd  jdd}| jrH|jtt| jd dS |S )zkConvert a GPS time into a `datetime.datetime`

    @returns a Python `datetime.datetime` object in UTC
    N   r   )microsecondgMbP?)	r   r!   r"   _gps_to_utcZ
gpsSecondsreplacegpsNanoSecondsintround)gpsdtr   r   r   
gps_to_utcg   s    r1   c               C   s
   t t S )zbGet the current date and time in UTC

    @returns a Python `datetime.datetime` object in UTC
    )r1   r   r   r   r   r   utc_time_noww   s    r2   c             C   s   | r|   dkrt S | dkr>tj }ttj|t S | dkrvtjtj t }|tj	dd }t|S | dkrtjtj t }|tj	dd }t|S t
| td}|j}t|}||d  S )	aB  Converts a date/time string into a GPS time.

    The following special words are permitted:
        - "now"
        - "today"
        - "yesterday"
        - "tomorrow"

    Example:
    \code
    >>> gpstime.str_to_gps("September 14 2011, 01:46:25")
    1000000000.000000000
    \endcode

    @returns a LIGOTimeGPS
    nowtodaytomorrowr   )days	yesterday)Ztzinfosg    .A)lowerr   r!   dater4   r'   r"   r#   r$   	timedelta
str_to_utc
TIME_ZONESr)   )Ztime_stringr9   r4   r5   r7   utcmicror/   r   r   r   
str_to_gps   s&    


r?   c             C   s2   t | } t| }| jr |s d}n|s(d}||S )a0  
    Convert a LIGOTimeGPS time object into a string.
    The output format can be given explicitly, but will default
    as shown in the example.

    Example:

    \code
    >>> gps_to_str(1000000000)
    'September 14 2011, 01:46:25 UTC'
    \endcode

    @returns a string with the given format.
    z%B %d %Y, %H:%M:%S.%f UTCz%B %d %Y, %H:%M:%S UTC)r   r1   r,   strftime)r/   formr=   r   r   r   
gps_to_str   s    
rB   c             C   s6   yt |  W n tk
r$   t| S X t| |dS dS )a)  Convert date/time strings to and from GPS times.
    If no argument is given, the current GPS time is returned.

    The following special words are permitted:
        - "now"
        - "today"
        - "yesterday"
        - "tomorrow"

    Example:

    \code
    >>> tconvert()
    1048275013.000000000
    >>> tconvert("January 6 1980 00:00:00")
    0.000000000
    >>> tconvert(1000000000)
    'September 14 2011, 01:46:25 UTC'
    \endcode

    @returns the LIGOTimeGPS of the given time string, OR, string
    representing the given GPS time
    )rA   N)floatr   r?   rB   )argrA   r   r   r   tconvert   s
    
rE   __main__zThe date/time now is %s (%d))N)N)NN)$__doc__r"   r!   Zdateutil.parserr   r;   Zlalr   r   r   r   r*   r   r%    r   
__author__Zverbose_msg__version__r9   __date__r<   r   r   r   r   r'   r1   r2   r?   rB   rE   __name__r3   Znow_utcprintr   r   r   r   <module>   s@   
'

#
