Skip to content

Commit e8af382

Browse files
committed
Allow Time/TimeDelta to init from any Time/TimeDelta subclass
1 parent f531050 commit e8af382

1 file changed

Lines changed: 38 additions & 27 deletions

File tree

‎astropy/time/core.py‎

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -334,28 +334,6 @@ class TimeBase(ShapedLikeNDArray):
334334
# attribute where column attributes will be stored.
335335
_astropy_column_attrs = None
336336

337-
def __new__(cls, val, val2=None, format=None, scale=None,
338-
precision=None, in_subfmt=None, out_subfmt=None,
339-
location=None, copy=False):
340-
341-
# Because of import problems, this can only be done on
342-
# first call of Time.
343-
global _LEAP_SECONDS_CHECKED
344-
if not _LEAP_SECONDS_CHECKED:
345-
# *Must* set to True first as update_leap_seconds uses Time.
346-
# In principle, this may cause wrong leap seconds in
347-
# update_leap_seconds itself, but since expiration is in
348-
# units of days, that is fine.
349-
_LEAP_SECONDS_CHECKED = True
350-
update_leap_seconds()
351-
352-
if isinstance(val, cls):
353-
self = val.replicate(format=format, copy=copy)
354-
else:
355-
self = super().__new__(cls)
356-
357-
return self
358-
359337
def __getnewargs__(self):
360338
return (self._time,)
361339

@@ -1004,7 +982,7 @@ def copy(self, format=None):
1004982
"""
1005983
return self._apply('copy', format=format)
1006984

1007-
def replicate(self, format=None, copy=False):
985+
def replicate(self, format=None, copy=False, cls=None):
1008986
"""
1009987
Return a replica of the Time object, optionally changing the format.
1010988
@@ -1033,9 +1011,9 @@ def replicate(self, format=None, copy=False):
10331011
tm : Time object
10341012
Replica of this object
10351013
"""
1036-
return self._apply('copy' if copy else 'replicate', format=format)
1014+
return self._apply('copy' if copy else 'replicate', format=format, cls=cls)
10371015

1038-
def _apply(self, method, *args, format=None, **kwargs):
1016+
def _apply(self, method, *args, format=None, cls=None, **kwargs):
10391017
"""Create a new time object, possibly applying a method to the arrays.
10401018
10411019
Parameters
@@ -1083,7 +1061,7 @@ def _apply(self, method, *args, format=None, **kwargs):
10831061
jd2 = apply_method(jd2)
10841062

10851063
# Get a new instance of our class and set its attributes directly.
1086-
tm = super().__new__(self.__class__)
1064+
tm = super().__new__(cls or self.__class__)
10871065
tm._time = TimeJD(jd1, jd2, self.scale, precision=0,
10881066
in_subfmt='*', out_subfmt='*', from_jd=True)
10891067

@@ -1611,6 +1589,28 @@ class Time(TimeBase):
16111589
copy : bool, optional
16121590
Make a copy of the input values
16131591
"""
1592+
def __new__(cls, val, val2=None, format=None, scale=None,
1593+
precision=None, in_subfmt=None, out_subfmt=None,
1594+
location=None, copy=False):
1595+
1596+
# Because of import problems, this can only be done on
1597+
# first call of Time.
1598+
global _LEAP_SECONDS_CHECKED
1599+
if not _LEAP_SECONDS_CHECKED:
1600+
# *Must* set to True first as update_leap_seconds uses Time.
1601+
# In principle, this may cause wrong leap seconds in
1602+
# update_leap_seconds itself, but since expiration is in
1603+
# units of days, that is fine.
1604+
_LEAP_SECONDS_CHECKED = True
1605+
update_leap_seconds()
1606+
1607+
if isinstance(val, Time):
1608+
self = val.replicate(format=format, copy=copy, cls=cls)
1609+
else:
1610+
self = super().__new__(cls)
1611+
1612+
return self
1613+
16141614
def __init__(self, val, val2=None, format=None, scale=None,
16151615
precision=None, in_subfmt=None, out_subfmt=None,
16161616
location=None, copy=False):
@@ -1627,7 +1627,7 @@ def __init__(self, val, val2=None, format=None, scale=None,
16271627
if not hasattr(self, 'location'):
16281628
self.location = None
16291629

1630-
if isinstance(val, self.__class__):
1630+
if isinstance(val, Time):
16311631
# Update _time formatting parameters if explicitly specified
16321632
if precision is not None:
16331633
self._time.precision = precision
@@ -2136,6 +2136,17 @@ class TimeDelta(TimeBase):
21362136

21372137
info = TimeDeltaInfo()
21382138

2139+
def __new__(cls, val, val2=None, format=None, scale=None,
2140+
precision=None, in_subfmt=None, out_subfmt=None,
2141+
location=None, copy=False):
2142+
2143+
if isinstance(val, TimeDelta):
2144+
self = val.replicate(format=format, copy=copy, cls=cls)
2145+
else:
2146+
self = super().__new__(cls)
2147+
2148+
return self
2149+
21392150
def __init__(self, val, val2=None, format=None, scale=None, copy=False):
21402151
if isinstance(val, TimeDelta):
21412152
if scale is not None:

0 commit comments

Comments
 (0)