Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong magic number for 2019/01/01 #1282

Open
rpeterer opened this issue Dec 14, 2023 · 1 comment
Open

Wrong magic number for 2019/01/01 #1282

rpeterer opened this issue Dec 14, 2023 · 1 comment

Comments

@rpeterer
Copy link

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.

@shancock884
Copy link
Contributor

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants