From 5047ced9095f74602417b27be1fa426020f5dddd Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Mon, 12 Dec 2022 09:32:43 -0500 Subject: [PATCH] Add CxoTimeLike type definition --- cxotime/__init__.py | 7 ++++--- cxotime/cxotime.py | 21 +++++++++++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/cxotime/__init__.py b/cxotime/__init__.py index 83a1c8c..c24886b 100644 --- a/cxotime/__init__.py +++ b/cxotime/__init__.py @@ -1,8 +1,9 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst -from .cxotime import CxoTime, date2secs, secs2date # noqa -from astropy.time import TimeDelta # noqa -from astropy import units # noqa import ska_helpers +from astropy import units # noqa +from astropy.time import TimeDelta # noqa + +from .cxotime import CxoTime, CxoTimeLike, date2secs, secs2date # noqa __version__ = ska_helpers.get_version(__package__) diff --git a/cxotime/cxotime.py b/cxotime/cxotime.py index 5a902e0..c638baf 100644 --- a/cxotime/cxotime.py +++ b/cxotime/cxotime.py @@ -1,23 +1,28 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst +import datetime import sys -import numpy as np +import warnings from copy import copy +from ctypes import c_int from pathlib import Path -import warnings -import datetime +from typing import Union +import numpy as np import numpy.ctypeslib as npct -from ctypes import c_int - -from astropy.time import Time, TimeCxcSec, TimeYearDayTime, TimeDecimalYear +import numpy.typing as npt +from astropy.time import Time, TimeCxcSec, TimeDecimalYear, TimeYearDayTime from astropy.time.utils import day_frac from astropy.utils import iers + # in astropy versions < 4.2, erfa was an astropy private package: try: import erfa except ModuleNotFoundError: from astropy import _erfa as erfa +# TODO: use npt.NDArray with numpy 1.21 +CxoTimeLike = Union["CxoTime", str, float, int, np.ndarray, npt.ArrayLike, None] + # Globally ignore the ERFA dubious year warning that gets emitted for UTC dates # either before around 1950 or well after the last known leap second. This # warning is conservatively indicating that UTC is not well-defined in those @@ -85,8 +90,8 @@ def date2secs(date): """ # This code is adapted from the underlying code in astropy time, with some # of the general-purpose handling and validation removed. - from astropy.time.formats import TimeYearDayTime from astropy.time import _parse_times + from astropy.time.formats import TimeYearDayTime # Handle bytes or str input and convert to uint8. We need to the # dtype _parse_times.dt_u1 instead of uint8, since otherwise it is @@ -351,8 +356,8 @@ def print_conversions(self): iso 2010-01-01 00:00:00.000 unix 1262304000.000 """ - from dateutil import tz from astropy.table import Table + from dateutil import tz formats = { "date": "s",