U
    CvfӦ                     @  sD  d dl mZ d dlZd dlmZ d dlmZ d dlZd dl	Z
d dlZd dlmZ d dlmZmZmZmZmZmZmZ d dlmZmZmZmZmZmZ d dlmZ d d	lm Z m!Z! d d
l"m#Z# d dl$m%Z%m&Z& d dl'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- ddddddhZ.e/e.0ej1j2Z3e/e.Z4e/ej1j2Z2e5ddfe5d6ddfe5d6ddfe5d7dddfde5d dfde5d dfde5d dfde5d dfddgdfdggdfddgdfe8ddfd d gd!fd ggd!fe5dd!fe5d d"d#d$fe5d d"d#d%fd&d'd(gd)fd*d+e9d,d-fde5d d.fd/d0d1d2gd3fgZ:d4d5 ee:e2D Z;d6d7 Z<e,ej=>d8ej=?d9d:d;ge;d<d=d>d?Z@e,d<d=d@dAZAd<d=dBdCZBe,d<d=dDdEZCe,ej=?d;e2d<d=dFdGZDe,ej=?d;e4d<d=dHdIZEe,ej=?d;e3d<d=dJdKZFe,ej=?d;e2d<d=dLdMZGe,ej=?d;e4d<d=dNdOZHe,ej=?d;e4d<d=dPdQZIe,ej=?d;e2d<d=dRdSZJe,ej=?d;e4d<d=dTdUZKe,ej=?d;e3d<d=dVdWZLe,ej=?dXdYdZd[gd<d=d\d]ZMe,d<d=d^d_ZNe,d<d=d`daZOe(e,ej=?d9d:dbgejPgddcgfejPd gddcddgfejPd degddcdddfgfgd<d=dgdhZQe,d<d=didjZRdkdldmdndodpdqdrZSej=?dseST d<d=dtduZUej=?dvdwge
Vdxdydcgdzfe
Vdcdxgdzfe
Vdcgd{fgd<d=d|d}ZWd~d~gdfd~ddgdfd~ddgdfgZXe,ej=?d;e4ddg ej=?deXd<d=ddZYej=?dd:dgddqeZdefdddgdqe8deddgdfddpeZdefddmeZdefddleZdefddkeZdefdcddgdejPd degfddgdpd'dgfdcdqejPfdcdcgdqejPejPgfg
d<d=ddZ[d<d=ddZ\ej=?ddwge
]ddgdqfe
]ddgdpfe
]ddejPgdofe
]ddgdnfgd<d=ddZ^e,ej=?ddwgddddgd<d=ddZ_ej=?d;e3d<d=ddZ`d<d=ddZae,d<d=ddZbejce3ddd; Zdec dd Z1ec dd Zeec dd Zfe,d<d=ddZge,e-d<d=ddZhe,d<d=ddZie,e-d<d=ddĄZjej=?deg ededggd<d=ddǄZke-ej=?deg ededggd<d=ddɄZle,ej=?ddddgd<d=ddτZmd<d=ddфZnd<d=ddӄZoe,d<d=ddՄZpe,d<d=ddׄZqej=?d;e2d<d=ddلZre,ej=?d;e2ej=?dddgd<d=ddބZse,ej=?d;e4ej=?ddddgd<d=ddZte,ej=?d;e3ej=?ddddgd<d=ddZuej=?d;e2d<d=ddZvej=?d;e2ej=?dddgd<d=ddZwej=?d;e4ej=?ddddgd<d=ddZxe,ej=?d;e3d<d=ddZyej=?deSz ej=?deS{ ej=?de
j|egd<d=ddZ}ej=?deS{ d<d=ddZ~e,ej=?dddddddgd<d=ddZe,d<d=ddZej=?d;dddgd<d=dd Ze,dd Ze,dd Zej=?dejejejejgdd Ze,dd	 Ze,d
d Zej=?dddgdd Ze,d<d=ddZdS (      )annotationsN)	timedelta)product)OutOfBoundsDatetime)	DataArrayDatasetVariablecftime_rangecodingconventions	decode_cf)_encode_datetime_with_cftime_should_cftime_be_usedcftime_to_nptimedecode_cf_datetimeencode_cf_datetimeto_timedelta_unboxed)SerializationWarning)_update_bounds_attributes
cf_encoder)contains_cftime_datetimes)assert_equalassert_identical)	arm_xfailassert_array_equalassert_no_warnings
has_cftimerequires_cftimerequires_dasknoleap365_day360_dayjulianall_leap366_day
   days since 2000-01-01float64Zfloat32      i0  zhours since 1680-01-01 00:00:00zhour since 1680-01-01  00:00:00zHour  since 1680-01-01 00:00:00z" Hour  since  1680-01-01 00:00:00 )r%   r&   zdaYs  since 2000-01-01)r   days since 1000-01-01r*   i i N  zdays since 1900-01-01zdays since 1-01-01)g   .MpAzhours since 1-1-1 00:00:0.0g      ?g      ?zhours since 1900-01-01T00:00:00)r   z&milliseconds since 2000-01-01T00:00:00)r   z&microseconds since 2000-01-01T00:00:00i@/zseconds since 1981-01-01z%hour since 1680-01-01 00:00:00.500000)i days since 1850-01-01 00:00:00)    Ar+   r,   g    DAr+   c                 C  s   g | ]\}}||f qS  r-   ).0Znum_dates_unitscalendarr-   r-   B/tmp/pip-unpacked-wheel-h316xyqg/xarray/tests/test_coding_times.py
<listcomp>W   s   r1   c               	   C  s.   dd l } | j| j| j| j| j| j| j| jdS )Nr   )r   r    r!   r"   r#   r$   	gregorianproleptic_gregorian)cftimeZDatetimeNoLeapZDatetime360DayZDatetimeJulianZDatetimeAllLeapDatetimeGregorianZDatetimeProlepticGregorian)r4   r-   r-   r0   _all_cftime_date_types_   s    r6   z&ignore:Ambiguous reference date string	num_datesunitsr/   None)returnc              	   C  sV  dd l }|j| ||dd}tt|t|  j}tt|t|  j}|dkrj|dk rjt|}t	
 " t	dd tj| ||}W 5 Q R X tt||  }t|  }|tdd	k sttj|||\}	}
}
t| t|	d t| d
rR| jdkrRd|krRtjt|||\}	}
}
t| t|	d d S )Nr   Tonly_use_cftime_datetimesi    ignoreUnable to decode time axis   sndimZ1000)r4   num2datenpZravelZ
atleast_1dZ	nanargminyearZ	nanargmaxr   warningscatch_warningsfilterwarningsr
   timesr   asarrayabspdto_timedeltatolistto_numpytimedelta64allAssertionErrorr   r   ZaroundhasattrrB   ZIndex)r7   r8   r/   r4   expectedZmin_yZmax_yactualabs_diffencoded_r-   r-   r0   test_cf_datetimen   s4       
"  
rY   c               
   C  s   ddl m}  | }d}d}|ddd|dd	df}t|D ]F\}}t   td
d tj||}W 5 Q R X ||| ks8t	q8d S )Nr   )r5   days since 2000-01-01 00:00:00)i4i3v i        r=      r>   r?   )
r4   r5   	enumeraterF   rG   rH   r
   rI   r   rR   )r5   datetimer8   daysrT   idayresultr-   r-   r0    test_decode_cf_datetime_overflow   s    
rd   c                  C  s6   t jdddd} d}tjtd|}t||  d S )Nd   z
1970-01-01hperiodsstartfreqzhours since 1-1-1970)rL   
date_ranger
   rI   r   rD   aranger   )rT   r8   rU   r-   r-   r0   *test_decode_cf_datetime_non_standard_units   s    rm   c                  C  s~   t jdddd} tddftddftddfg}|D ]<\}}tj||}t|| j }|t	dd	k
 s<tq<d S )
Nre   
2000-01-01rf   rg   zhours since 2000-01-01 0zhours since 2000-1-1 0zhours since 2000-01-01 0:00r@   rA   )rL   rk   rD   rl   r
   rI   r   rK   valuesrP   rQ   rR   )rT   Zcasesr7   r8   rU   rV   r-   r-   r0   'test_decode_cf_datetime_non_iso_strings   s    rp   c           	      C  s   dd l }d}tjdddd}|j| || d}|j}td}tj	j
||| d}|j|ks`tt|| }|td	d
k std S )Nr   days since 0001-01-012001-04-01-002001-04-30-23Hendrj   r/   M8[ns]r@   rA   )r4   rL   rk   date2numto_pydatetimero   rD   dtyper
   rI   r   rR   rK   rP   rQ   )	r/   r4   r8   rI   timerT   expected_dtyperU   rV   r-   r-   r0   4test_decode_standard_calendar_inside_timestamp_range   s    
r~   c           	      C  s   dd l }d}tjdddd}|j| || d}|j||| dd	}td
}tj	j
||| d}|j|ksltt|| }|tddk std S )Nr   rq   rr   rs   rt   ru   rw   Tr/   r<   Or@   rA   )r4   rL   rk   ry   rz   rC   rD   r{   r
   rI   r   rR   rK   rP   rQ   )	r/   r4   r8   rI   Znon_standard_timerT   r}   rU   rV   r-   r-   r0   8test_decode_non_standard_calendar_inside_timestamp_range   s&       
  r   c              	     s   ddl m   dd l}d} fddtddD }|j||| d}|j||| d	d
}t|d t $ tdd t	j
j||| d}W 5 Q R X tfdd|D stt|| }|tddk std S )Nr   r_   rq   c                   s   g | ]} d dd |qS r@   r]   r-   )r.   rf   r   r-   r0   r1      s     z=test_decode_dates_outside_timestamp_range.<locals>.<listcomp>r@   r)   rw   Tr   r>   r?   c                 3  s   | ]}t | V  qd S N)
isinstance)r.   value)expected_date_typer-   r0   	<genexpr>  s     z<test_decode_dates_outside_timestamp_range.<locals>.<genexpr>rA   )r_   r4   rangery   rC   typerF   rG   rH   r
   rI   r   rQ   rR   rK   rD   rP   )r/   r4   r8   rI   r|   rT   rU   rV   r-   )r_   r   r0   )test_decode_dates_outside_timestamp_range   s$       
r   c              
   C  sd   d}ddgdggfD ]J}t  $ t dd tjj||| d}W 5 Q R X |jtdkstqd S )Nrq   8 r>   r?   rw   rx   	rF   rG   rH   r
   rI   r   r{   rD   rR   r/   r8   num_timerU   r-   r-   r0   Ctest_decode_standard_calendar_single_element_inside_timestamp_range  s    
r   c              
   C  sd   d}ddgdggfD ]J}t  $ t dd tjj||| d}W 5 Q R X |jtdkstqd S )Nrq   r   r>   r?   rw   r   r   r   r-   r-   r0   Gtest_decode_non_standard_calendar_single_element_inside_timestamp_range  s    
r   c                 C  s   dd l }d}dD ]t}||g|ggfD ]^}t $ tdd tjj||| d}W 5 Q R X |j||| dd}t|	 t
|s$tq$qd S )	Nr   rq   )r@   io r>   r?   rw   Tr;   )r4   rF   rG   rH   r
   rI   r   rC   r   itemr   rR   )r/   r4   r8   r`   r   rU   rT   r-   r-   r0   2test_decode_single_element_outside_timestamp_range'  s$    
     r   c                 C  s   dd l }d}tjdddd}tjdddd}|j| || d	}|j| || d	}tt|d
f}||d d df< ||d d df< |j}|j}	t	j
j||| d	}
|
jtdkstt|
d d df | }t|
d d df |	 }|tddk st|tddk std S )Nr   rq   
2001-04-01
2001-04-05Dru   
2001-05-01
2001-05-05rw   r(   r@   rx   rA   )r4   rL   rk   ry   rz   rD   emptylenro   r
   rI   r   r{   rR   rK   rP   rQ   r/   r4   r8   times1times2time1time2	mdim_time	expected1	expected2rU   	abs_diff1	abs_diff2r-   r-   r0   Btest_decode_standard_calendar_multidim_time_inside_timestamp_range;  s"    r   c                 C  sd  dd l }d}tjdddd}tjdddd}|j| || d	}|j| || d	}tt|d
f}||d d df< ||d d df< |jdkr|j	||| dd}|j	||| dd}	n|	||| }|	||| }	t
d}
tjj||| d	}|j
|
kstt|d d df | }t|d d df |	 }|tddk sFt|tddk s`td S )Nr   rq   r   r   r   ru   r   r   rw   r(   r@   r4   Tr;   r   rA   )r4   rL   rk   ry   rz   rD   r   r   __name__rC   r{   r
   rI   r   rR   rK   rP   rQ   )r/   r4   r8   r   r   r   r   r   r   r   r}   rU   r   r   r-   r-   r0   Etest_decode_nonstandard_calendar_multidim_time_inside_timestamp_rangeZ  s>    
      
r   c              	     sn  ddl m   dd l}d} fddtddD } fddtddD }|j||| d	}|j||| d	}tt|d
f}||d d df< ||d d df< |j||| dd}|j||| dd}	t	 $ t
dd tjj||| d	}
W 5 Q R X |
jtdkstt|
d d df | }t|
d d df |	 }|tddk sPt|tddk sjtd S )Nr   r   rq   c                   s   g | ]} d d|qS r   r-   r.   rb   r   r-   r0   r1     s     zEtest_decode_multidim_time_outside_timestamp_range.<locals>.<listcomp>r@      c                   s   g | ]} d d|qS )r@   r)   r-   r   r   r-   r0   r1     s     rw   r(   Tr;   r>   r?   r   rA   )r_   r4   r   ry   rD   r   r   rC   rF   rG   rH   r
   rI   r   r{   rR   rK   rP   rQ   r   r-   r   r0   1test_decode_multidim_time_outside_timestamp_range  s(    
r   )r/   r   )r!   g    t%A)r#       6W&A)r$   r   c                 C  sZ   dd l }d}tjj||| d}t|j||| dd}|jtdksJt||ksVtd S )Nr   rq   rw   Tr;   r   )	r4   r
   rI   r   rD   rJ   rC   r{   rR   )r/   r   r4   r8   rU   rT   r-   r-   r0   0test_decode_non_standard_calendar_single_element  s    r   c               
   C  s   dd l } d}dD ]}d| d}td}| j|||dd}tjdd	2}td
 tjj	|||d}t
|dksvtW 5 Q R X |jtdkstt|| qd S )Nr   r!   )i  i  i  i  i  days since -01-01re   Tr;   )recordalwaysrw   r   )r4   rD   rl   rC   rF   rG   simplefilterr
   rI   r   r   rR   r{   r   )r4   r/   rE   r8   Z	num_timesrT   wrU   r-   r-   r0   test_decode_360_day_calendar  s*    
   
  r   c                  C  sH   ddl } tdg}d}tj||}tj| ||}t|| dS )zFTest making sure we properly fall back to cftime on abbreviated units.r   Ng  _wBz msecs since 1970-01-01T00:00:00Z)	r4   rD   arrayr
   rI   r   r   rC   r   )r4   valr8   rU   rT   r-   r-   r0   test_decode_abbreviation  s    r   expected_listNaTz2000-01-01T00:00:00Zr@   z2000-01-02T00:00:00Zc              	   C  sN   t    t dd tj| |}W 5 Q R X t|jdd}t	|| d S )Nr>   zAll-NaNdatetime64[ns]r{   )
rF   rG   rH   r
   rI   r   rL   to_datetimerO   r   )r7   r8   r   rU   rT   r-   r-   r0   test_cf_datetime_nan  s
    
r   c                  C  sl   t dtddgddggddi} tj | }|jdks@tt	j
d	d
djdd}tt|| d S )Nxyr   r@   r(      r8   r&   r   rn   r]   rh   )r   rD   r   r
   rI   CFDatetimeCoderdecoder{   rR   rL   rk   ro   reshaper   rJ   )variablerc   rT   r-   r-   r0   !test_decoded_cf_datetime_array_2d  s      r   nanosecondsmicrosecondsZmillisecondssecondsminuteshoursr`   )NULSTrt   r   )rj   r8   c                 C  s2   t jdd| d}| d}|tj|ks.td S )N2000r(   rh   rj   z since 2000-01-01 00:00:00)rL   rk   r
   rI   infer_datetime_unitsrR   )rj   r8   datesrT   r-   r-   r0   test_infer_datetime_units  s    
r   r   rT   z
1900-01-01z
1900-01-02zdays since 1900-01-01 00:00:00days since 1970-01-01 00:00:00c                 C  s   |t j| kstd S r   )r
   rI   r   rR   )r   rT   r-   r-   r0   "test_infer_datetime_units_with_NaT  s    r   )l  r@   r@   z%days since 1900-01-01 00:00:00.000000)r   r@   r(   )r   r@   r(   r   r   r@   z(seconds since 1900-01-01 00:00:00.000000)r   r@      )r   r@      r2   r3   )	date_argsrT   c                   s4   t  |    fdd|D }|tj|ks0td S )Nc                   s   g | ]} | qS r-   r-   )r.   args	date_typer-   r0   r1   5  s     z4test_infer_cftime_datetime_units.<locals>.<listcomp>)r6   r
   rI   r   rR   )r/   r   rT   r   r-   r   r0    test_infer_cftime_datetime_units.  s    
r   
timedeltasnumbers1D2D3Dr   int64Z1hZ1msZ1usZ1nsZ0sZ1sZ30mZ60m      ?c                 C  s   | dkrt dd} nt| } t |}|}tj| |\}}t|| |j|jksXt	|d k	r| }tj
||}t|| |j|jkst	t dd}tj
t t jd}t|| d S )Nr   nsr`   )rD   rP   r   r   r
   rI   Zencode_cf_timedeltar   r{   rR   decode_cf_timedeltanan)r   r8   r   rT   rU   rX   r-   r-   r0   test_cf_timedelta9  s     


r   c                  C  sX   d} t dddg}t tdddg}|}tj|| }t|| |j|jksTtd S )Nr`   r@   r(   r   r   r   r   )	rD   Z
atleast_2dr   r
   rI   r   r   r{   rR   )r8   r   r   rT   rU   r-   r-   r0   test_cf_timedelta_2d_  s    
r   deltasz1 dayz2 daysz1 day 1 hourZ1mZ2mZ1m3sZ1m4sc                 C  s   |t j| kstd S r   )r
   rI   Zinfer_timedelta_unitsrR   )r   rT   r-   r-   r0   test_infer_timedelta_unitsk  s    
r   r   ))r@   r(   r   r]   r)   r   z0001-02-03 04:05:06.000000))r%   r(   r   r]   r)   r   z0010-02-03 04:05:06.000000))re   r(   r   r]   r)   r   z0100-02-03 04:05:06.000000))i  r(   r   r]   r)   r   z1000-02-03 04:05:06.000000c                 C  s4   t  }| D ] }tj||  }||kstqd S r   )r6   ro   r
   rI   Zformat_cftime_datetimerR   )r   rT   Z
date_typesr   rc   r-   r-   r0   test_format_cftime_datetimex  s    r   c              	   C  s   dddg}t ||gdgdd}| }dD ] }d|| jd	< | || jd
< q*tsx| tkrxtt t|}W 5 Q R X n>t|}| tkr|j	j
t
dkstn|j	j
t
dkstd S )Nr          @g      @r|   testZcoordsdimsname)r   r|   zdays since 2001-01-01r8   r/   r   rx   )r   
to_datasetattrsr   _STANDARD_CALENDARSpytestraises
ValueErrorr   r   r{   rD   rR   )r/   r`   dadsvr-   r-   r0   test_decode_cf  s    
r   c                  C  s8  t tjddddddddgid	d
d} ddd
d}|  }|d j| t|j |jd
 jdddksrt	t
|}|jjtdkst	t
|dd}|jjtdkst	|  }|d j| ddd}|d
 j| t|j |jd
 j|kst	|  }|d j| d|d jd< t|j d S )Nr   r   r   )r   r(   r|   r@   r(   r   )r|   Znbnd	time_bndsr   zdays since 2001-01standard)r8   r/   bounds)r8   r/   rx   F)Zdecode_timeszhours since 2001-01r   Zfake_varr   )r   rD   rl   r   r   r   updater   	variablesrR   r   r   r{   )r   r   r   ZdscZbnd_attrr-   r-   r0   test_decode_cf_time_bounds  s>    



r  c               	     s  t jddd} t jdddd}tt| |d d	d
i j_ddd j_i }tt	ddgd
gd|d
< t
 j j\}}t|d
 |d
  d|d
 jkstd|d
 jkstddd j_t
 fdd jD  j\}}t|d
 |d
  d|d
 jkstd|d
 jkstddd j_t
 fdd jD  j\}}tt t|d
 |d
  W 5 Q R X d|d
 jkst|d
 jd  jjd ksti  j_tt t
 j j W 5 Q R X d S )Nz
2000-01-16r@   r   rn   r(   MSr   )r|   time_boundsr   r  r   r&   r/   r8   r   r\   )datar   r/   r8   c                   s   i | ]}| | qS r-   r-   r.   kr   r-   r0   
<dictcomp>  s      z+test_encode_time_bounds.<locals>.<dictcomp>zdays since 1849-01-01c                   s   i | ]}| | qS r-   r-   r  r
  r-   r0   r    s      )rL   rk   r   dictr|   r   encodingr   rD   r   r   r  r   rR   r  r   r   warnsUserWarning)r|   r  rT   rW   rX   r-   r
  r0   test_encode_time_bounds  s2       r  )paramsc                 C  s   | j S r   )param)requestr-   r-   r0   r/     s    c                 C  s    dd l }|jtdd| ddS )Nr   r]   hours since 2000-01-01T)r8   r/   r<   )r4   rC   rD   rl   )r/   r4   r-   r-   r0   rI     s    rI   c                 C  sH   t jddd}t ddd}t ddd}t|||| gdddgd	d
S )Nr(   r]   r         lonlatr|   r  r   )rD   randomZrandlinspacer   )rI   r  lonslatsr-   r-   r0   r    s       r  c                 C  sH   t ddd}t ddd}t jj| dd}t|||| gddd	gd
dS )Nr   r  r(   r  )r(   r(   r]   )sizer  r  r|   r  r   )rD   r  r  choicer   )rI   r  r  Z	times_arrr-   r-   r0   times_3d  s       r  c                 C  s   t | jstd S r   )r   r|   rR   r  r-   r-   r0   !test_contains_cftime_datetimes_1d  s    r!  c                 C  s   t | j std S r   )r   r|   chunkrR   r   r-   r-   r0   &test_contains_cftime_datetimes_dask_1d  s    r#  c                 C  s   t | std S r   r   rR   r  r-   r-   r0   !test_contains_cftime_datetimes_3d!  s    r&  c                 C  s   t |  std S r   r   r"  rR   r%  r-   r-   r0   &test_contains_cftime_datetimes_dask_3d&  s    r(  non_cftime_datac                 C  s   t | rtd S r   r$  r)  r-   r-   r0   *test_contains_cftime_datetimes_non_cftimes,  s    r+  c                 C  s   t |  rtd S r   r'  r*  r-   r-   r0   /test_contains_cftime_datetimes_non_cftimes_dask1  s    r,  shape)   )r   r   )r(   r]   r   c                 C  sN   t jdddj| }d}d}t|||\}}}t|||}tj|| d S )NZ2100r.  r   zdays since 1800-01-01r   )	rL   rk   ro   r   r   r   rD   testingr   )r-  r   r8   r/   numrX   Z	roundtripr-   r-   r0    test_encode_cf_datetime_overflow7  s    r1  c               	   C  s^   t jddd} tjtdd t| dd W 5 Q R X tjtdd t| d	d W 5 Q R X d S )
Nr   r   r   zinvalid time unitsmatchzdays after 2000-01-01)r8   zinvalid reference datezdays since NO_YEAR)rL   rk   r   r   r   r   )r   r-   r-   r0   test_encode_expected_failuresD  s
    r4  c                  C  s^   t jddd} t| \}}}tdddg}d}d}tj|| ||ksNt||ksZtd S )	Nr   r   r   g        r   r   rZ   r3   )rL   rk   r   rD   r   r/  r   rR   )r   r0  r8   r/   expected_numexpected_unitsexpected_calendarr-   r-   r0   "test_encode_cf_datetime_pandas_minM  s    r8  c                  C  sT   t tjddddd\} }}dddg}d}d}t| | ||ksDt||ksPtd S )	Nr   r   r   z!days since 1970-01-01 00:00:00 00r   i*  i*  i*  )r   rL   rk   r   rR   )r0  r8   r/   r5  r6  r7  r-   r-   r0   3test_encode_cf_datetime_invalid_pandas_valid_cftimeY  s    


r9  c           	      C  s   d}t dddg}t||| }t|jj}tdddg}t|| t||| \}}}| tkrnt j	
|| nt j	|| ||kst|| kstd S )Nz$days since 2000-01-01T00:00:00-05:00r@   r(   r   r)   )rD   r   r   r   dthourr   r   r   r/  r   Zassert_allcloserR   )	r/   r6  Zexpected_num_datesr   Zresult_hoursZexpected_hoursZresult_num_datesZresult_unitsZresult_calendarr-   r-   r0   'test_time_units_with_timezone_roundtripj  s     
  
r<  c              	   C  sJ   ddg}d}t jddd}t   t||| }tj|| W 5 Q R X d S )Nr   r@   r&   r   r(   r   rL   rk   r   r   rD   r/  r   r/   numerical_datesr8   rT   rc   r-   r-   r0   2test_use_cftime_default_standard_calendar_in_range  s    r@  
units_yeari  i	  c              	   C  sd   ddl m} ddg}d| d}|||| dd}tt  t||| }tj|| W 5 Q R X d S Nr   rC   r@   r   r   Tr;   )	r4   rC   r   r  r   r   rD   r/  r   r/   rA  rC   r?  r8   rT   rc   r-   r-   r0   6test_use_cftime_default_standard_calendar_out_of_range  s       rE  i  c              	   C  s`   ddl m} ddg}d| d}|||| dd}t   t||| }tj|| W 5 Q R X d S rB  r4   rC   r   r   rD   r/  r   rD  r-   r-   r0   -test_use_cftime_default_non_standard_calendar  s       rG  c              	   C  sd   ddl m} ddg}d| d}|||| dd}t $ t||| dd}tj|| W 5 Q R X d S )	Nr   rC  r@   r   r   Tr;   
use_cftimerF  rD  r-   r-   r0   test_use_cftime_true  s       rJ  c              	   C  sN   ddg}d}t jddd}t $ t||| dd}tj|| W 5 Q R X d S )	Nr   r@   r&   r   r(   r   FrH  r=  r>  r-   r-   r0   0test_use_cftime_false_standard_calendar_in_range  s    rK  c              	   C  s>   ddg}d| d}t t t||| dd W 5 Q R X d S Nr   r@   r   r   FrH  r   r   r   r   r/   rA  r?  r8   r-   r-   r0   4test_use_cftime_false_standard_calendar_out_of_range  s    rO  c              	   C  s>   ddg}d| d}t t t||| dd W 5 Q R X d S rL  rM  rN  r-   r-   r0   +test_use_cftime_false_non_standard_calendar  s    rP  c           	   	   C  s   ddl m} | tjjk}dddg}d}|||| dd}|rtt}t||| d	}W 5 Q R X d
d |j	D }t
|dkstn t  t||| d	}W 5 Q R X tj|| d S )Nr   rC  r@   r(   r   zdays since 1-1-1Tr   rw   c                 S  s    g | ]}t |jd r|qS )z&Ambiguous reference date string: 1-1-1)strmessage
startswith)r.   rr-   r-   r0   r1     s   z4test_decode_ambiguous_time_warns.<locals>.<listcomp>)r4   rC   r
   rI   r   r   r  r   r   listr   rR   r   rD   r/  r   )	r/   rC   Zis_standard_calendarr   r8   rT   r   rc   Zrelevant_warningsr-   r-   r0    test_decode_ambiguous_time_warns  s    
rV  encoding_unitsrj   rk   c           	      C  s   t s|tkrtd |dks&| dkr8|tkr8td |dd|d}|  d}tj||\}}}tj| }t	d	|}t
d	||kr|jtjkstn|jtjkstd S )
NzTest requires cftimer   r   z3Nanosecond frequency is not valid for cftime dates.r   r   r   z since 2000-01-01r@   )r   r	   r   skipr
   rI   r   Z_netcdf_to_numpy_timeunitrD   rP   rL   rM   r{   r   rR   r'   )	rW  rj   rk   rI   r8   rW   rX   Znumpy_timeunitZencoding_units_as_timedeltar-   r-   r0   1test_encode_cf_datetime_defaults_to_correct_dtype  s    


rY  c                 C  sT   t jddd}|t jdd| d}tdg|}t|}td|}t|| d S )Nz
1678-01-01r@   r   Z1968r(   r   r|   )rL   rk   appendr   r   encode_cf_variabledecode_cf_variabler   rj   Zinitial_timerI   r   rW   decodedr-   r-   r0   'test_encode_decode_roundtrip_datetime64  s    
r_  r   r   r   r   rt   r   c                 C  s^   t ddd}|t dd| dtdd }tdg|}t|}tjd|d	d
}t|| d S )NZ0001r@   r   r(   r   iXT)r`   r|   TrH  )r	   rZ  r   r   r   r[  r\  r   r]  r-   r-   r0   #test_encode_decode_roundtrip_cftime#  s    
r`  c                  C  sz   dd l } d}| ddgd|}d}z| j|||dd}W n" tk
rZ   | |||}Y nX t|||}tj|| d S )Nr   r2   r@   r  r&   F)Z
longdouble)r4   rC   ry   	TypeErrorr   rD   r/  r   )r4   r/   rI   rW  rT   rc   r-   r-   r0   !test__encode_datetime_with_cftime0  s    rb  Z	GregorianZ	GREGORIANc                 C  s\   ddg}d}| |d}t dg||}td|}t|}t|jtjsNtt	|| d S )Nr   r@   r&   r  r|   )
r   r   r\  r[  rD   Z
issubdtyper{   
datetime64rR   r   )r/   rI   r8   r   r   r^  rW   r-   r-   r0   7test_decode_encode_roundtrip_with_non_lowercase_lettersF  s    

rd  c               	   C  s:   t ddddd} tjtdd t| dd	 W 5 Q R X d S )
Nz
1000-01-01re   r  r   rh   rj   r/   z3Source time range is not valid for numpy datetimes.r2  r   Fr	   r   r   r   r   srcr-   r-   r0   /test_should_cftime_be_used_source_outside_rangeZ  s     ri  c               	   C  s:   t ddddd} tjtdd t| dd W 5 Q R X d S )	Nrn   re   r  r   re  z,Calendar 'noleap' is only valid with cftime.r2  Frf  rg  r-   r-   r0   ,test_should_cftime_be_used_target_not_npablec  s     rj  r{   c                 C  s:   d}| d}t ||}ttdd}tj|| d S )Nz"seconds since 2018-08-22T03:23:03Z2   z2018-08-22T03:23:53r   )r   rD   rJ   rc  r/  r   )r{   r8   r7   rc   rT   r-   r-   r0   test_decode_cf_datetime_uintl  s
    
rl  c                  C  s<   d} t d}t|| }t t dd}t j|| d S )Nzdays since 1700-01-01i] z
2200-01-01r   )rD   uint64r   rJ   rc  r/  r   )r8   r7   rc   rT   r-   r-   r0   *test_decode_cf_datetime_uint64_with_cftimeu  s
    

rn  c               	   C  s8   d} d}t d}tt t|| | W 5 Q R X d S )Nzmicroseconds since 1700-01-01r!   l     5h6> )rD   rm  r   r   OverflowErrorr   )r8   r/   r7   r-   r-   r0   9test_decode_cf_datetime_uint64_with_cftime_overflow_error~  s
    
rp  rI  TFc                 C  sX   | rt st  | rtnd}tjg |d}ttjdtjddd| d}tj	
|| d S )Nrx   r   r   )r-  r{   r   r3   )r8   r/   rI  )r   r   rX  objectrD   r   r   zerosr   r/  r   )rI  r{   rT   rU   r-   r-   r0   test_decode_0size_datetime  s    rs  c                  C  sL   t dtddgddggdtji} tj | }t|j	d sHt
d S )Nr   r   r@   r(   r   r8   )r   rD   r   r   r
   rI   r   r   isnanr   rR   )r   rc   r-   r-   r0   test_scalar_unit  s    $ru  )
__future__r   rF   r_   r   	itertoolsr   ZnumpyrD   ZpandasrL   r   Zpandas.errorsr   Zxarrayr   r   r   r	   r
   r   r   Zxarray.coding.timesr   r   r   r   r   r   Zxarray.coding.variablesr   Zxarray.conventionsr   r   Zxarray.core.commonr   Zxarray.testingr   r   Zxarray.testsr   r   r   r   r   r   Z_NON_STANDARD_CALENDARS_SETsortedunionrI   r   Z_ALL_CALENDARSZ_NON_STANDARD_CALENDARSrl   Zastyper   r   Zint32Z_CF_DATETIME_NUM_DATES_UNITSZ_CF_DATETIME_TESTSr6   markrH   ZparametrizerY   rd   rm   rp   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ZFREQUENCIES_TO_ENCODING_UNITSitemsr   r   r   Z_CFTIME_DATETIME_UNITS_TESTSr   r   r   r   rM   r   r   r   r  r  Zfixturer/   r  r  r!  r#  r&  r(  r+  r,  r1  r4  r8  r9  r<  r@  rE  rG  rJ  rK  rO  rP  rV  ro   keysrk   rY  r_  r`  rb  rd  ri  rj  Zuint8Zuint16Zuint32rm  rl  rn  rp  rs  ru  r-   r-   r-   r0   <module>   s4  $	  





  
#	' 
	
 

		*&



	
		

