Example #1
0
    def test_set_format(self):
        """
        Test basics of setting format attribute.
        """
        dt = TimeDelta(86400.0, format='sec')
        assert dt.value == 86400.0
        assert dt.format == 'sec'

        dt.format = 'jd'
        assert dt.value == 1.0
        assert dt.format == 'jd'

        dt.format = 'datetime'
        assert dt.value == timedelta(days=1)
        assert dt.format == 'datetime'
Example #2
0
    def test_set_format(self):
        """
        Test basics of setting format attribute.
        """
        dt = TimeDelta(86400.0, format='sec')
        assert dt.value == 86400.0
        assert dt.format == 'sec'

        dt.format = 'jd'
        assert dt.value == 1.0
        assert dt.format == 'jd'

        dt.format = 'datetime'
        assert dt.value == timedelta(days=1)
        assert dt.format == 'datetime'
Example #3
0
    def __getitem__(self, i):
        """ Supports indexing of the TimeScale, e.g. ts[0:10:2] """
        if isinstance(i, slice):
            skip = 1 if i.step is None else i.step
            start = 0 if i.start is None else i.start
            stop = self.n_step if i.stop is None else i.stop
            new_n_step = (stop - start) // skip

            new_time_unix = self.val_start + start * self.val_step
            new_time_obj = Time(new_time_unix, format='unix')
            new_time_obj.format = self.time_format

            new_delta_sec = self.val_step * skip
            new_delta_obj = TimeDelta(new_delta_sec, format='sec')
            new_delta_obj.format = self.time_delta_format

            #return self.val_start + self.val_step * np.arange(start, stop, step)
            return TimeScale(self.name,
                             new_time_obj.value,
                             new_delta_obj.value,
                             new_n_step,
                             time_format=new_time_obj.format,
                             time_delta_format=new_delta_obj.format)
        else:
            if i < 0 or i > self.n_step:
                raise IndexError(
                    f"Index ({i}) is out of axis bound ({self.n_step})")
            t = Time(i * self.val_step + self.val_start, format='unix')
            t.format = self.time_format
            return t
Example #4
0
def test_timedelta_datetime_roundtrip(scale, days, day_frac):
    td = TimeDelta(days, day_frac, format="jd", scale=scale)
    td.format = "datetime"
    assert_almost_equal(td, TimeDelta(td.value, scale=scale), atol=2 * u.us)
Example #5
0
 def time_delta(self):
     t = TimeDelta(self.val_step, format='sec')
     t.format = self.time_delta_format
     return t