skyfielders / python-skyfield

Elegant astronomy for Python
MIT License
1.43k stars 213 forks source link

Skyfield time arithmetic #227

Closed LIII-XXII closed 5 years ago

LIII-XXII commented 5 years ago

Hi,

I am trying to compute B: skyfield.Date that is a minute before A: skyfield.Date.

How should I go about this?

I tried using datetime.timedelta to represent one minute and subtract it, but it is not supported.

from skyfield.api import load
from datetime import timedelta
ts = load.timescale()
A = ts.now()
B = A - datetime.timedelta(minutes=1) #TypeError: unsupported operand type(s) for -: 'Time' and 'datetime.timedelta'

I couldn't find a good way to do this in the examples or in the source of skyfield.timelib (timelib.py). Any idea?

LIII-XXII commented 5 years ago

The ugly workaround I am using now looks like this:

from skyfield.api import load
import skyfield, skyfield.timelib
import datetime
TS = load.timescale()
A : skyfield.timelib.Time = TS.now()
tmp: datetime = A.utc_datetime()
B: skyfield.timelib.Time = TS.utc(tmp.year, tmp.month, tmp.day, tmp.hour, tmp.minute-1, tmp.second)

I am worried that this will break with leap seconds though.

brandon-rhodes commented 5 years ago

Good question! By "one minute" do you mean "exactly 60 seconds", or "however many seconds will get me back to the same second-number in the previous minute in UTC"? And if the latter, what do you want to happen if the current time is 23:59:60.5, in the middle of a leap second?

LIII-XXII commented 5 years ago

in this case I mean "exactly 60 seconds", the goal is to find the date B that was 60 seconds before A (which makes it sound like I should use tmp.second-60 instead...)

brandon-rhodes commented 5 years ago

Yes, you should get good results if you subtract 1.0 / 24 / 60 from the Terrestrial Time. Something like:

minute = 1.0 / 24 / 60
t2 = ts.tt_jd(t.tt - minute)

By the way, were the : characters in your sample code your own notation, or part of a framework you're using? They didn't work when I tried them in the version of Python on this laptop.

LIII-XXII commented 5 years ago

these are python 3 type declarations (available from python 3.5/3.6): https://docs.python.org/3/library/typing.html thanks for the hint. We can close this now.