How many seconds?
How many seconds are there in a minute? If you said 60, you’re wrong. Some minutes have 61, although the so-called leap second only comes around every few years, and then only once a year at most.
How many minutes in an hour? There are 60—at least, I can’t think of any cases when there are more or fewer. However, one of those minutes could have a leap second, giving a length of 3,600 or 3,601 seconds for an hour.
How many hours in a day? 24? Sometimes: it depends. With summer/winter time zone changes, it can be an hour longer or shorter, making 23, 24 or 25 hours. Allowing for the fact that a leap second may occur, that means that a day is in the range of 82,800 to 90,001 seconds long.
How many days in a month? In the Gregorian calendar, it can be 28, 29, 30 or 31 days. Allowing the minimum and maximum day lengths above, that’s somewhere between 2,415,600 and 2,678,401 seconds inclusive.
That’s not exhaustive, and I might have missed further complications, but the point of all this is simple: the precise length of any unit of time longer than a second is undefined. Minutes, hours, days, weeks, months and years only have a specific length within a particular time context.
Even then, it gets complicated. If we are currently in the 25th hour of an autumn day on which the clocks go back, what is the time this time tomorrow?
I was prompted by this plaintive message on the ruby-talk mailing list this morning, from someone bitten by a ‘daylight saving’ bug:
The Time::next_week method is supposed to give the time of the start of the next week. But look at this, it cocks up :
>> t=Time.parse "Monday October 16th 2006" => Mon Oct 16 00:00:00 BST 2006 >> t.next_week => Mon Oct 23 00:00:00 BST 2006 >> t.next_week.next_week => Tue Oct 24 00:00:00 BST 2006 >> t.next_week.next_week.next_week => Mon Oct 30 00:00:00 GMT 2006Arrhhhgg!! Its just f*cked up my application data!
I feel his pain. Even if it’s hard to say how things should work in certain pathological cases, this doesn’t strike me as one of them. There is, by the way, an outstanding patch for this bug.
On the other hand, it’s fine to be blasé in certain situations. If I want to clear out cache files over a day old, I probably don’t care about a few seconds either way, so I’m going to be happy with 86,400 seconds (which is what 1.day will give me in Rails, for example).
The moral of the story? Use abstract lengths of time when accuracy is unimportant. When it matters, use methods that understand the context (as I mentioned previously)—and test those edge cases: the author of the code you’re using might not have thought of the problem, or might have interpreted it differently to you.
2006-10-25 18:38 UTC. Comments: 6.
Ben Griffiths
Wrote at 2006-10-25 20:03 UTC using Safari 419.3 on Mac OS X:
March is not a number…http://www.eaipatterns.com/ramblings/40_marchnan.html
And it looks like a day is not a number of seconds.
Paul Battley
Wrote at 2006-10-25 20:33 UTC using Firefox 1.5.0.7 on Mac OS X:
That’s a good link. The last paragraph, in particular, makes a really important point.The more I think about it, the more I wonder whether some of the oversimplified stuff like 1.day and Time.now.next_week ought to be eschewed entirely. Saving a few keypresses is hardly a benefit if it divorces the code from the problem domain and obscures what’s really going on.
Perhaps we need a Ruby port of Time & Money!
James Darling
Wrote at 2006-11-11 11:52 UTC using Safari 419.3 on Mac OS X:
When is the EU going to step in and finally decimalise time?James Mead
Wrote at 2006-11-11 14:15 UTC using Firefox 2.0 on Mac OS X:
In a former life, I used to run scientific equipment that needed to be adjusted for leap seconds. I still chuckle at the name of the Earth Rotation Service which issues the bulletins announcing them. Let’s hope they don’t go on strike ;-)Have a closer look at your Wikipedia link – leap seconds can be introduced at the end of any month. Historically they have only been added at the end of June or December (twice in one year in 1972 for those of us who can remember that far back). So theoretically there could be 12 in a year.
belinda
Wrote at 2008-03-31 12:09 UTC using Internet Explorer 6.0 on Windows XP:
i found that robots are smart these are the estimations:1 year = 365.25 days)
(24 h = 86,400 seconds)
(2 day = 172,800 seconds)
(3 day = 259,200 seconds)
(4 day = 345,600 seconds)
(1 year = 31,557,600 seconds
belinda
Wrote at 2008-03-31 12:11 UTC using Internet Explorer 6.0 on Windows XP:
i am 14 and do extreamly well in science