skyfielders / python-skyfield

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

Specify decimal places for seconds display. #133

Closed ajpmaclean closed 7 years ago

ajpmaclean commented 7 years ago

In the time object how do I control the number of decimal places for seconds in:

It would be nice to have a places keyword like in:

brandon-rhodes commented 7 years ago

Good question! When you run either of those routines, you are returned a Python datetime object instead of a native Skyfield object, so it will be the native Python routines that determine what you can and can't do with controlling how many decimal places print. Here are the docs for how to control the format that a datetime uses when printing in Python:

https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

You might also search for Stack Overflow issues talking about datetimes. Good luck! If nothing else, Python string slicing using square brackets should let you trim off extra decimal places if you can't do anything else:

from skyfield.api import load
ts = load.timescale()
dt = ts.now().utc_datetime()
digits_wanted = 4
digits_to_trim = 6 - digits_wanted
print(dt.strftime('%Y-%m-%d %H:%M:%S.%f')[:-digits_to_trim])
ajpmaclean commented 7 years ago

Thank you so much for the reply. I didn't think about slicing. That's a good suggestion.

Regards Andrew

Andrew Maclean

On 24 May 2017 13:50, "Brandon Rhodes" notifications@github.com wrote:

Good question! When you run either of those routines, you are returned a Python datetime object instead of a native Skyfield object, so it will be the native Python routines that determine what you can and can't do with controlling how many decimal places print. Here are the docs for how to control the format that a datetime uses when printing in Python:

https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

You might also search for Stack Overflow issues talking about datetimes. Good luck! If nothing else, Python string slicing using square brackets should let you trim off extra decimal places if you can't do anything else:

from skyfield.api import load ts = load.timescale() dt = ts.now().utc_datetime() digits_wanted = 4 digits_to_trim = 6 - digits_wanted print(dt.strftime('%Y-%m-%d %H:%M:%S.%f')[:-digits_to_trim])

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/skyfielders/python-skyfield/issues/133#issuecomment-303624425, or mute the thread https://github.com/notifications/unsubscribe-auth/AIJ4O2vmZkJlJZRWju9sq0vboWEggCkPks5r88UugaJpZM4NSjNN .