Adding a timedelta to a skyfield Time

Viewed 1310

The skyfield Almanach documentation

uses this code to define the points in time between which to compute sunrise & sunset:

t0 = ts.utc(2018, 9, 12, 4)
t1 = ts.utc(2018, 9, 13, 4)

What if I just wanted to use one (start) date and set the next date to be exactly one day after? I can't just add one to the day argument since this would not be correct at the end of the month.

Using Python's datetime I could do this using

from datetime import datetime, timedelta

datetime(2019, 1, 31, 12) + timedelta(days=1)
# datetime.datetime(2019, 2, 1, 12, 0)

but I can't find anything like timedelta in the skyfield API documentation.

2 Answers

What if I just wanted to use one (start) date and set the next date to be exactly one day after? I can't just add one to the day argument since this would not be correct at the end of the month.

Happily, you can just add one to the day! As the documentation says:

https://rhodesmill.org/skyfield/time.html

"you are free to provide out-of-range values and leave it to Skyfield to work out the correct result"

>>> from skyfield.api import load
>>> ts = load.timescale()
[#################################] 100% deltat.data
>>> t = ts.utc(2018, 2, 28 + 1)
>>> t.utc_jpl()
'A.D. 2018-Mar-01 00:00:00.0000 UT'

You can use datetime's timedelta and convert back between datetime and skyfield's Time objects like this:

t0 = ts.utc(2019, 1, 31, 12)
t1 = ts.utc(t0.utc_datetime() + timedelta(days=1))

# Print
t1.utc_iso()
# '2019-02-01T12:00:00Z'

While certainly not beautiful, this allows you to use all the features of Python's datetime.

Related