Skip to content

Commit

Permalink
Merge pull request #53 from sot/allow-bytes-input
Browse files Browse the repository at this point in the history
Allow bytes input to date2secs
  • Loading branch information
taldcroft authored Mar 26, 2022
2 parents 82b9788 + 6ad0833 commit 2c31c34
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
10 changes: 9 additions & 1 deletion Chandra/Time/Time.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,17 @@ def get_style(fmt):
sys_out, fmt_out, dtype_out = get_style(format_out)

vals, ndim = _make_array(vals)

# Allow passing bytes to axTime3 by converting to string here. This is
# actually silly since later in axTime3.convert_time() it gets encoded back
# to ASCII bytes. But since this package is largely deprecated we take the
# performance hit in the interest of simpler code.
if vals.dtype.kind == 'S':
vals = np.char.decode(vals, 'ascii')

# If the input is already string-like then pass straight to convert_time.
# Otherwise convert to string with repr().
if vals.dtype.char in 'SU':
if vals.dtype.kind == 'U':
outs = [axTime3.convert_time(val, sys_in, fmt_in, sys_out, fmt_out)
for val in vals.flatten()]
else:
Expand Down
13 changes: 10 additions & 3 deletions Chandra/Time/tests/test_Time.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ def test_convert_vals_array():
assert np.all(val == convert_back)


def test_date2secs():
vals = DateTime(['2012:001', '2000:001'])
assert np.all(date2secs(vals.date) == vals.secs)
@pytest.mark.parametrize('date_in', ('2012:001:00:00:00',
['2012:001:00:00:00', '2000:001:00:00:00']))
def test_date2secs(date_in):
vals = DateTime(date_in)
assert np.all(date2secs(date_in) == vals.secs)
if isinstance(date_in, str):
date_in_bytes = date_in.encode('ascii')
else:
date_in_bytes = [date.encode('ascii') for date in date_in]
assert np.all(date2secs(date_in_bytes) == vals.secs)


def test_secs2date():
Expand Down

0 comments on commit 2c31c34

Please sign in to comment.