Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

John Carmack may have misremembered the 10th digit of pi, but you should all search your codebases for 84,600 and see who typo'd the number of seconds in a day. It is surprisingly common and there is a lesson about whether or not you should type constants into your program when they likely exist in the standard library of your programming language.


Oh wow, you weren't kidding. Even big projects like MySQL and Textmate look like they might have that error in their codebase.

https://github.com/search?type=code&auto_enroll=true&q=84600

https://github.com/mysql/mysql-server/blob/824e2b4064053f7da...

https://github.com/textmate/textmate/blob/346b52b108b387462d...


That is so smart to try it on Github.

None of those look fatal but I think they all differ from the intent of the author. That's why code review is so good. You just need someone to ask "what's 874600?". Turns out: not a thing.

MySQL's issue is a timeout on SSL sessions which is normally 300s. Textmate's is a "friendly" display of the time. Not a showstopper, but probably not what they had in mind. It's so easy to transpose digits. It's great to have someone ask "are you sure?".


I spent an embarrassing amount of time debugging why some token refresh code that was supposed to trigger every 15 minutes didn't. Eventually I had to just print out the value for each variable and found that I defined 15 minutes as 15 * 60 * 60...


I find it easier to do something like:

SECONDS_IN_MINUTE = 60

REFRESH_TIME_MINUTES = 15

REFRESH_TIME_SECONDS = SECONDS_IN_MINUTE * REFRESH_TIME_MINUTES

e.g. I define every constant separately, and ideally any constant defined by an operation will only have one such operation. This makes mistakes _much_ easier to spot.


    new TimeSpan(minutes = 15)


Depending on the language, this is definitely preferable!


Oh the places you'll go

When you roll your own date

You'll think you are great

'Til you're 6 minutes slow!


I’ve encountered similar issues for long lived tokens in cookies on Safari.

Turned out my Maths was okay, but Safari under many circumstances removes first party cookies after 7 days.

https://snowplow.io/blog/tracking-cookies-length/


I like defining constants with math.


*the number of seconds in most days


Depends what planet you're on too, I suppose.


earth has leap seconds


But those don't increase the number of seconds in the day. Instead, the final second happens twice.

This is different from what happens in leap year, where February is actually lengthened and February 29 is considered a different day from February 28. It escapes me why leap seconds aren't treated that way. (I know, people say the stupid treatment of leap seconds is required by the POSIX definition of time. So what? Change the POSIX definition.)


> But those don't increase the number of seconds in the day. Instead, the final second happens twice

No, it adds a 61th second to the last minute.

UNIX timestamps happen twice, because it's broken. NTP too.


> But those don't increase the number of seconds in the day. Instead, the final second happens twice.

i agree that this is a rather confusing way of putting it


If you can't do multiplication and you rely on a library for that. I feel sorry for you.


It's really a matter of who's going to catch your typos. If you use the constant built into your language, millions of people are looking at it. If you type them into your throwaway bash script, nobody is going to notice you typed 60 * 60 * 24 * 356 to get a year's worth of seconds.


So meta that you typed 356 instead of 365


That was their point.


Magic numbers something something. If your language provides it as a constant, why not use that? You’ll always get the correct value AND a descriptive name for free.


If I have it I’d use it, but I’m not going to go out of my way to add another dependency for it lol.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: