skyfielders / python-skyfield

Elegant astronomy for Python
MIT License
1.41k stars 211 forks source link

Support for adding and subtracting deltas to Time objects? #568

Closed joncox123 closed 2 years ago

joncox123 commented 3 years ago

It seems that Skyfield does not support adding or subtracting increments of time (e.g. timedelta) to a Time object. For example, say you have a Time object and you want to increment it exactly 0.0000038001 seconds, you would like to do:

ts.now() + timedelta(seconds=0.0000038001) # or something similar

This would give you the time by a precise amount in the future or past, at the reference frame of the Time object. Is this functionality already available? If not, it would be appreciated. Right now, I am rolling my own implementation.

P.S. converting back and forth between datetime objects and Time objects can get you this functionality, but the loss of precision is unacceptable because the resolution defaults to microseconds. I need double precision second resolution.

brandon-rhodes commented 3 years ago

Thanks for the suggestion! There are a few different ways, even with the current code, to do increment and decrement of a time. Could you outline the situation you're in that requires you to adjust times, since that might affect what the best solution for your case looks like?

A problem with the expression you suggest is that:

Wanting to avoid those ambiguities, Skyfield users have typically pulled out the timescale they want to increment (like TT, or TAI, or UTC), added their value to it, then built a new Time object. Let's see if your situation suggests a solution that will make sense!

cruisen commented 3 years ago

For a ts.now() + timedelta(seconds=0.0000038001) see comments above.

However for something likets.now() + timedelta(days=5) using ts.from_datetime(datetime.now(timezone.utc) + datetime.timedelta(days=5)) appears sufficient. Or am I missing something?

brandon-rhodes commented 2 years ago

As you'll see in the above commit, I've added this feature to Skyfield and it should appear in the next release. Thanks for suggesting it!