4

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.

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120

2 Answers2

5

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'
Brandon Rhodes
  • 83,755
  • 16
  • 106
  • 147
  • For whomever may find this comment. Be careful doing this for hours, minutes, seconds values. `skyfield.timelib.Timescale.utc` only handles leap seconds against the year, month, day arguments. Thus, if you add a large out of range value for hours, minutes, seconds it may not account for leap seconds! // `load.timescale().utc(1970, 1, 1, 0, 0, 1664381247.00).utc_strftime() Out[57]: '2022-09-28 16:07:00 UTC' datetime.datetime.fromtimestamp(1664381247, datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S") Out[58]: '2022-09-28 16:07:27'` – KDecker Sep 28 '22 at 16:08
2

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.

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120