# Licensed under a 3-clause BSD style license - see LICENSE.rst import functools import numpy as np import pytest from astropy import units as u from astropy.table import Table from astropy.time import Time from astropy.utils import iers from astropy.utils.compat import PYTHON_LT_3_11 from astropy.utils.compat.optional_deps import HAS_H5PY allclose_sec = functools.partial( np.allclose, rtol=2.0**-52, atol=2.0**-52 * 24 * 3600 ) # 20 ps atol is_masked = np.ma.is_masked # The first form is expanded to r"can't set attribute '{0}'" in Python 3.10, and replaced # with the more informative second form as of 3.11 (python/cpython#31311). no_setter_err = ( r"can't set attribute" if PYTHON_LT_3_11 else r"property '{0}' of '{1}' object has no setter" ) def test_simple(): t = Time([1, 2, 3], format="cxcsec") assert t.masked is False assert np.all(t.mask == [False, False, False]) # Before masking, format output is not a masked array (it is an ndarray # like always) assert not isinstance(t.value, np.ma.MaskedArray) assert not isinstance(t.unix, np.ma.MaskedArray) t[2] = np.ma.masked assert t.masked is True assert np.all(t.mask == [False, False, True]) assert allclose_sec(t.value[:2], [1, 2]) assert is_masked(t.value[2]) assert is_masked(t[2].value) # After masking format output is a masked array assert isinstance(t.value, np.ma.MaskedArray) assert isinstance(t.unix, np.ma.MaskedArray) # Todo : test all formats def test_scalar_init(): t = Time("2000:001") assert t.masked is False assert t.mask == np.array(False) def test_mask_not_writeable(): t = Time("2000:001") with pytest.raises( AttributeError, match=no_setter_err.format("mask", t.__class__.__name__) ): t.mask = True t = Time(["2000:001"]) with pytest.raises(ValueError) as err: t.mask[0] = True assert "assignment destination is read-only" in str(err.value) def test_str(): t = Time(["2000:001", "2000:002"]) t[1] = np.ma.masked assert str(t) == "['2000:001:00:00:00.000' --]" assert ( repr(t) == "