Back to Basics: Explore the Edge Cases or Date Math will Get You
Disclaimer: I don't work for the Zune team and I don't know anyone on the team. I think that Z2K9 was a bummer, but I don't have any inside knowledge. Everything here came from the public interweb.
Dates will get you every time. Further more, it's all about Edge Cases. This is one of the things you'll think about when doing Test Driven Development and it's one of the things that everyone learns in Computer Science 101. You really have to hit those edge cases, be they dates, or number overflows, or buffer overflows.
The news reports:
"A bug in the internal clock driver related to the way the device handles a leap year affected Zune users," said the company in a statement. "That being the case, the issue should be resolved over the next 24 hours as the time change moves to January 1, 2009."
Disclaimer*2: I have no idea if the following is 100% true, only that it seems quite plausible. I present it for educational purposes, nothing else. It's very interesting. Again, I'm just a caveman.
A Zune Fan poked around in the source code from the vendor (Freescale Semiconductor) that made the real time clock in the Zune (The vendor's source for rtc.c is here) and with the benefit of hindsight, noted that there's the opportunity to get stuck in an infinite loop.
The Zune 30 shows the date and time, as do many devices, as the number of days and seconds since January 1st, 1980 at midnight.
year = ORIGINYEAR; /* = 1980 */
while (days > 365)
{
if (IsLeapYear(year))
{
if (days > 366)
{
days -= 366;
year += 1;
}
}
else
{
days -= 365;
year += 1;
}
}
The days variable is read out of the memory location managed by the Real Time Clock (RTC). When the value of days == 366, you break out of the inner loop, but you can never get out of the outer loop.
A number of folks have blogged about the bug, their analysis and how they'd fix it. Programming Phases has a good post and folks have twittered suggestions). The basic problem is that since there are 366 days remaining when the calculations are reached for the year 2008, there will never be another subtraction to bring the total below 365, so the loop continues. The value of days is stuck at 366. It IS a leap year, but days is not > 366, so the loop continues.
In working with banking software for years, I can tell you that dates'll get you. When dealing with dates and date math you can't underestimate the value of really good code coverage. Also, even if you have 100% coverage, as I learned in my interview with Quetzal Bradley, 100% coverage just tells you a line of code DID run, it doesn't tell you that it ran correctly.
I noticed also when I visited my Live home page on Dec-31 that it said today was Jan-1, likely a Time Zone glitch. However, when I clicked the date, I was taken to a page with historical facts about Dec-31. ;)

Dates, especially when TimeZones are added in, are notoriously hard.
To this day there are a half-dozen bugs in DasBlog where we have a devil of a time with Time Zones. Omar spent weeks fighting with them before he just gave up. We have to reconcile the local time of the visitor, the time on the server, and GMT time (the time we store everything in). It usually works, but when the Server isn't on GMT we get into all sorts of trouble. We also have problems with clients that call the XML-RPC APIs, some of which use UTC (GMT) time and some use local time. Other than keeping an internal table to wrong-headed clients, there are no good solutions.
In 2007 CNN talked to a Major General about a bug in some F-22's that caused them to malfunction when going across the international dateline. The Unofficial Apple Weblog had an interesting post on Apple Date/Time bugs through the years. If you're interested in working with and maintaining legacy code, I recommend Michael Feathers' Working Effectively with Legacy Code.
As a random slightly-related aside, I was over at Hollywood Video buying a used copy of Mirror's Edge yesterday and some folks were talking about the Zune problem. The manager said, wow, I'm running the music in here on a Zune right now, and produced his brown 30G Zune from behind the counter. He either hadn't turned it on today fresh or the clock was wrong so he was able to weather the whole day without an incident. He seemed pretty pleased about his "survival."
I wonder how many other less widespread devices are running this real time clock and if any of them had trouble as well.
Do you have any personal Date/Time stories, Dear Reader? Please share in the comments!
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter





This trip to Africa was a reunion of sorts. My wife is the 4th of 7 kids, ranging in age from 38 to 18. This is the first time in over a decade that all seven kids have been in the same place at the same time. Folks are spread all over the world, so it's a challenge.
We were flying Air France this time (KLM before) and we knew that bags need to weigh less than 23kg/50lbs leaving the US, and 32kg/70lbs on the way back into the US. When we arrived at the Air France counter there was large sign that said just that - 32kg. Turns out that this weight limit changed in January, it's 20kg/44lbs for economy. Only Business Class gets 32kg. Everyone was having a problem with this today, and the guy in front of us was going insane. Like "I'll never fly Air France again" insane.
We usually weigh our bags with a scale, and I highly recommend you to also, and give yourself 5 pounds of wiggle room. The Jo'burg airport is interesting because it has a scale before you check in and they are strict. We had no trouble this time because we were prepared, but it was close.
The first guy deleted the baby doppelganger and we travelled fine. This was over three weeks ago. Fast forward to today and when we go to the airport, the baby is gone. They'd deleted his record and ticket completely. But, he was still in there, floating around Tron-style in the system. The guy was doing his best, it was clear, but some foreign-key relationship was definitely not working. He spend literally 90 minutes working on this, going back and forth to ticketing, management, and IT. He ended up deleting our entire ticket completely and starting over issuing new tickets to get us boarding passes.
We had a final meal as a group at Nando's outside security then headed in to Passport Control. This was tricky also because after waiting for a useless 90 minutes, folks figured that any urgency had passed. However, outbound Passport Control at Jo'burg is always busy and there's usually very few agents working. I knew we could spend, easily, another hour waiting for our outbound stamp. The flight boarded at 7:55pm, and I was only able to get folks to say their good byes and start walking at 7:15pm. I didn't even bother mentioning that it was a full 2 kilometers between Passport Control and gate A17.
After another 20 minutes as the next folks in line at Passport Control we moved up to talk to the agent. I
Anyway, at this point, it's 7:45pm and we're really pushing it. We take off speed walking with the 4 bags, and two kids, which isn't really speed walking at all. It was easily 2km to the gate, and they were boarding when we got there.