From 47d1e3001e34f7a89ed684493fdfd47d5c2185ac Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Sat, 11 Nov 2023 11:05:11 -0500 Subject: [PATCH] Short circuit convert_time_format for a CxoTime object --- cxotime/convert.py | 5 +++++ cxotime/tests/test_cxotime.py | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/cxotime/convert.py b/cxotime/convert.py index 0a0ae9f..97e7e6d 100644 --- a/cxotime/convert.py +++ b/cxotime/convert.py @@ -36,6 +36,11 @@ def convert_time_format(val, fmt_out, *, fmt_in=None): val_out : str Time string in output format """ + # If this is already a CxoTime object then return the attribute without all the + # conversion machinery. + if isinstance(val, CxoTime): + return getattr(val, fmt_out) + jd1, jd2 = None, None if fmt_in is None: # Get the format. For some formats the jd1/jd2 values are generated as a diff --git a/cxotime/tests/test_cxotime.py b/cxotime/tests/test_cxotime.py index 9855054..1ca2b3a 100644 --- a/cxotime/tests/test_cxotime.py +++ b/cxotime/tests/test_cxotime.py @@ -447,3 +447,9 @@ def test_convert_functions(fmt_val, val_type, fmt_out): out3 = func(val) assert type(out) is type(out3) assert np.all(out == out3) + + +def test_convert_time_format_obj(): + """Explicit test of convert_time_format for CxoTime object""" + tm = CxoTime(100.0) + assert tm.date == convert_time_format(tm, "date")