1/1/2024 0 Comments Take the minutes![]() Return dt + datetime.timedelta(0,rounding-seconds,-dt.microsecond) Rounding = (seconds+roundTo/2) // roundTo * roundTo # // is a floor division, not a comment on following line: Stijn Nevens 2014 - Changed to use only datetime objects as variables """Round a datetime object to a multiple of a timedeltaĪuthor: Thierry Husson 2012 - Use it as you want but don't blame me. Print(round_time(datetime.datetime(2012,12,19,23,00,00),date_delta=datetime.timedelta(hours=1)))įrom the best answer I modified to an adapted version using only datetime objects, this avoids having to do the conversion to seconds and makes the calling code more readable: def roundTime(dt=None, dateDelta=datetime.timedelta(minutes=1)): Return dt + datetime.timedelta(0, rounding - seconds, - dt.microsecond) Rounding = seconds // round_to * round_to Rounding = (seconds + dt.microsecond/1000000 + round_to) // round_to * round_to # // is a floor division, not a comment on following line (like in javascript): Rounding = (seconds + round_to / 2) // round_to * round_to If seconds % round_to = 0 and dt.microsecond = 0: Round a datetime object to a multiple of a timedeltaĭt : datetime.datetime object, default now.ĭateDelta : timedelta object, we round to a multiple of this, default 1 minute. import datetimeĭef round_time(dt=None, date_delta=datetime.timedelta(minutes=1), to='average'): Tested for date_delta of "X hours" or "X minutes" or "X seconds". Update = comment Bart incorporated thank you. Update = comment Spinxz incorporated thank you. Rounding up, down and rounding to nearest. For example, the galaxy M109 is located about 83.5 million light-years away.I used Stijn Nevens code (thank you Stijn) and have a little add-on to share. There are galaxies millions of light-years away, which means the light we’re seeing left the surface of those stars millions of years ago. And so, if the light from the nearest star (Alpha Centauri) takes more than 4 years to reach us, we’re seeing that star 4 years in the past. The Sun is more than 8 light-minutes away. The light reflected from the surface of the Moon takes only a second to reach Earth. The light you see from your computer is nanoseconds old. Once they escaped the surface, it was only a short 8 minutes for those photons to cross the vast distance from the Sun to the EarthĪs you look outward into space, you’re actually looking backwards in time. What you probably don’t know, is that these photons striking your eyeballs were ACTUALLY created tens of thousands of years ago and it took that long for them to be emitted by the sun. ![]() They start off as gamma radiation and then are emitted and absorbed countless times in the Sun’s radiative zone, wandering around inside the massive star before they finally reach the surface. You probably know that photons are created by fusion reactions inside the Sun’s core.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |