Yesterday I told you about the Doomsday algorithm in my post Doomsday in 1900 Was a Wednesday . Let's look at some more interesting tricks and stories to do with dates. Days Each Month 30 days hath September, April, June and November, All the rest have 31, Excepting February alone. Which only has but 28 days clear And 29 in each leap year I'm sure you know that rhyme, although to be honest if you can remember it then you might as well just remember the number of days per month. But here's an easier way, that most people don't seem to know. Put your fists together (fold in your thumbs). Now the knuckles are the 31 day months, and the valleys between your knuckles are the 30 day months (except February, but I'm sure you can remember that separately). The "trick" is that when you get to your right knuckle (July), there is no valley. The sequence starts again with the left knuckle of your right hand, so another 31 day month (August). 1461 Algorithm It is desirable to turn a date express as a number of days since an epoch without using tables of the number of days per month and stepping through. It is not very efficient. Plus, if not programmed correctly, it is easy to get into an infinite loop. In fact, at the end of 2008, all Microsoft Zune players went dead for a day because of just such a problem. The code mishandled a daynumber greater than 365 and all Zunes went dead until the day rolled over back to 1 with the new year. This is actually ACM Algorithm 199 published in 1963, over 50 years ago. I used this algorithm in the network file system that I created as part of my PhD thesis. I think of it as the 1461 algorithm since that number appears early in the implementation. 1461 is the number of days in 4 years (365 + 365 + 365 + 366). From 1900 (not a leap year) until 2100 (also not a leap year), every 4 year block (including 2000, which was a leap year) has this number of days. You also need to renumber the months, so that month 1 is March, and month 11 is January of the following year, and 12 is February of the following year. It is straightforward to fix this bit up at the end. I'll explain roughly how it works. The first trick is to use the 1461 number to divide the years up into blocks of 4 years, with the leap day as the last day of the entire 4 years. That way, the leap day won't mess anything up. Of course, to do that, you have to start on a March 1st, not a January 1st, as mentioned above. Next, note that the year consists of sequences of 5 months with days as follows: 31,30,31,30,31 (153 days). For example, March, April, May, June, July have just that sequence. So do August, September, October, November December. And so do January, February...except the day number will never be big enough to go beyond the last day of February into the mythical following months (or the mythical last days of February). This is kind of like the knuckle trick with your fists. If you divide the year up into blocks of 5 like that, it copes with the July/August both having 31 days, as do December/January (remember, we start the year in March). That should be enough of a hint to implement it yourself. You should end up with something like (all integer arithmetic—the rounding is important):. w= 4*daysincentury-1 year = w / 1461 d = (w - 1461*year + 4) / 4 w = 5*d - 3 month = w / 153 day = (w - 153*month + 5) / 5 if month<10 then month += 3 else { month -= 9; year += 1 } Astronomers Astronomers want to be able to do calculations on dates easily and want to go back a long way in time to calculate eclipses and other events. So they use the Julian Day Number. This is the number of days since January 1st 4713 BC noon GMT (as measured by extending the Julian calendar backward as if it was in use that far back). So today, July 4th 2018, is Julian Day 2458304. If the time of day is required, it is simply a fraction of a day added onto the Julian Day Number. I mentioned GMT in the preceding paragraph, which stands for Greenwich Mean Time. Greenwich (pronounced "grennich") is a town in East London, just south of the river Thames. There is an observatory there and the 0° meridian runs through it. There is a brass strip on the ground showing exactly where it runs, and you can stand astride it with one foot in each hemisphere. If you are at all interested in time, clocks, or the measurement of longitude, then it is a must-visit place for another reason. All of Harrison's chronometers are kept there. If you have read Dava Sobel's wonderful book Longitude , or seen the TV series version, then you know the story. And if not, read it immediately! The 2038 Problem As we approached 2000, there was a lot of panic about the Y2K problem. I was pretty sure most of the worries were bogus. I talked to a friend of mine who ran a programming group at a bank, and she said it was a non-issue and had been solved years ago at her bank. When I heard that cars might not start due to their engine control unit failing, I knew it was bogus since there would be no need to have a date in the code, and even if there were, in an embedded system it would be unlikely to be stored in a day and month format anyway. But there is a similar problem lurking in 32-bit Unix systems (and some others). Many systems, such as most 32-bit Unix systems, represent time as the number of seconds passed since 1st January 1970. It is stored as a signed 32-bit integer. This will max out at 03:14:07 UTC on 19 January 2038. This mostly affects older systems and embedded systems. In particular, 64-bit Linux uses a 64-bit integer to store the number of seconds so will not have any problem in 2038. There may be some issues with 32-bit Linux, which also stores the time in 64 bits, but has to pass times back and forth as a 32-bit number for backward compatibility reasons. These sort of problems can be serious. It is thought that NASA's Deep Impact spacecraft was lost in 2013 when the number of tenth-of-a-second increments since January 1st 2000 exceeded the 32-bit value designed to store the time. Bill Gates Reviews Joel Spolsky's Excel Spec This is about dates and times, too. Joel has his own company and is also behind StackOverflow. But he used to work for Microsoft and was the program manager for converting Excel to use Visual Basic for its macro language. Read the wonderful and amusing story of his review of this document in My First BillG Review . He found some date anomalies where Lotus 1-2-3 (the prior successful spreadsheet that Excel competed with)...but you will have to read the whole piece to find out what and why. To whet your appetite, I'll give you part of the denouement when Bill Gates asks his hardest question: I don’t know, you guys,” Bill said, “Is anyone really looking into all the details of how to do this? Like, all those date and time functions. Excel has so many date and time functions. Is Basic going to have the same functions? Will they all work the same way?” “Yes,” I said, “except for January and February, 1900.” Silence. The F-counter and my boss exchanged astonished glances. How did I know that? January and February WHAT? “OK. Well, good work,” said Bill. He took his marked up copy of the spec Paul McCartney Nothing to do with dates, but if you haven't seen it yet, watch Paul McCartney on Carpool Karaoke: www.youtube.com/watch Sign up for Sunday Brunch, the weekly Breakfast Bytes email.
↧