Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for CxoTime() to get current time in date format #9

Merged
merged 1 commit into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ Important differences:
- Conversely, starting with `CxoTime` one can add or subtract a `TimeDelta` or
any astropy `Quantity` with time units.

- To get the current time replace `DateTime()` with `CxoTime.now()`

The standard built-in Time formats that are available in `CxoTime` are:

Format | Example
Expand Down
19 changes: 19 additions & 0 deletions cxotime/cxotime.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ class CxoTime(Time):
=========== ==============================

"""
def __new__(cls, *args, **kwargs):
# Handle the case of `CxoTime()` which returns the current time. This is
# for compatibility with DateTime.
if not args:
if not kwargs:
args = (None, )
else:
raise ValueError('cannot supply keyword arguments with no time value')
return super().__new__(cls, *args, **kwargs)

def __init__(self, *args, **kwargs):
if args:
if args[0].__class__.__name__ == 'DateTime':
Expand All @@ -74,6 +84,9 @@ def __init__(self, *args, **kwargs):
raise ValueError("must use scale 'utc' for DateTime input")
if kwargs.setdefault('format', 'date') != 'date':
raise ValueError("must use format 'date' for DateTime input")
else:
# For `CxoTime()`` return the current time in `date` format.
args = (Time.now().yday, )

# If format is supplied and is a DateTime format then require scale='utc'.
fmt = kwargs.get('format')
Expand Down Expand Up @@ -104,6 +117,12 @@ def __init__(self, *args, **kwargs):

super(CxoTime, self).__init__(*args, **kwargs)

@classmethod
def now(cls):
return cls()

now.__doc__ = Time.now.__doc__


class TimeSecs(TimeCxcSec):
"""
Expand Down
13 changes: 13 additions & 0 deletions cxotime/tests/test_cxotime.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .. import CxoTime
from astropy.time import Time
from Chandra.Time import DateTime
import astropy.units as u


def test_cxotime_basic():
Expand Down Expand Up @@ -37,6 +38,18 @@ def test_cxotime_basic():
t = CxoTime('1998:001:00:00:01.000', scale='tt')


@pytest.mark.parametrize('now_method', [CxoTime, CxoTime.now])
def test_cxotime_now(now_method):
ct_now = now_method()
t_now = Time.now()
assert t_now >= ct_now
assert (ct_now - t_now) < 10 * u.s

with pytest.raises(ValueError,
match='cannot supply keyword arguments with no time value'):
CxoTime(scale='utc')


def test_cxotime_from_datetime():
secs = DateTime(np.array(['2000:001', '2015:181:23:59:60.500', '2015:180:01:02:03.456'])).secs
dts = DateTime(secs)
Expand Down
2 changes: 0 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ The key differences between |CxoTime| and DateTime_ are:
- Conversely, starting with |CxoTime| one can add or subtract a TimeDelta_ or
any quantity with time units.

- To get the current time replace ``DateTime()`` with ``CxoTime.now()``

The standard built-in Time formats that are available in |CxoTime| are:

=========== ==============================
Expand Down