MAVProxy icon indicating copy to clipboard operation
MAVProxy copied to clipboard

Wrong magic number for 2019/01/01

Open rpeterer opened this issue 2 years ago • 1 comments

Hi there I just stumbled across this line of code and was wondering if it shouldn't be

from datetime import datetime
from datetime import timezone

jan_1_2019_s = datetime(year=2019, month=1, day=1, tzinfo=timezone.utc).timestamp()

Because the referenced magic number 1546261200 is Mon Dec 31 2018 13:00:00 GMT+0000 in UTC. I also think the provided example is more readable. Maybe someone could consider to update this? If I find time I will provide a PR.

rpeterer avatar Dec 14 '23 08:12 rpeterer

I agree, the number in the code looks wrong. One thought: if you put the call to datetime into the timestamp_2019 function, then Python will end up re-running the code to recalculate this constant value every time it is called. So maybe calculate it in __init__ so that its only done once, or hard code the right value with a suitable comment about how you got it.

Also, from unix epoch of 1st Jan 1970, the maths could be also be done without time functions, something like this: 3600*24*int(365.25*(2019-1970)) Or more generically (valid between 1901 and 2100):

def unix_time_1_jan(year):
    3600*24*(365*(year-1970) + math.floor((year-1969)/4))

shancock884 avatar Jan 11 '24 14:01 shancock884