The Windows Developer team wanted to celebrate the New Year by tweeting some software code to its followers. Unfortunately, the code doesn’t work. And this despite the incredibly basic nature of the code.
I don’t want to spend too much time on this. But it is kind of funny.
On Monday, the official Windows Developer account on Twitter tweeted the following:
Sign up for our new free newsletter to get three time-saving tips each Friday — and get free copies of Paul Thurrott's Windows 11 and Windows 10 Field Guides (normally $9.99) as a special welcome gift!
"*" indicates required fields
Run code: Happy New Year
if (DateTime.Now.ToString() == "01/01/2022 00:00:00") { Console.WriteLine("Happy New Year!"); } else { Console.WriteLine("It\'s still 2021..."); }
That’s C# console code, if you’re not familiar. (And it’s kind of weird that Windows Developer would tweet console code, but whatever.)
Even a non-programmer should be able to tell what the code is trying to do: If you run this app when it’s exactly midnight on New Year’s Eve, you’ll get a “Happy New Year!” message. But if you run it at any other time, it will tell you that it’s still 2021.
So what’s the problem? As it turns out, there are all kinds of issues with this code. But the two biggest and most obvious blunders that I see are:
1. You will only get the New Year greeting at exactly midnight. And I mean exactly. If it is one second into 2022 or one second before 2022, you will not get this greeting.
2. If it’s not exactly midnight, the app will tell you that it’s still 2021. But it will do that no matter what the date or time is. It’s 2022 now, for example, but this app will tell you that it’s 2021.
Realizing its error, the Windows Developer team deleted the tweet. But thanks to Davor Pihač for capturing a screenshot and pointing it in my direction. He also pointed out that this code will only work in the United States because of the way it uses ToString() to make the date/time conversion. Basically, dates are a type and you should never compare dates using strings.
And if you’re really interested in this mess, check out Scott Hanselman’s overly-detailed analysis of the many problems with this code. I was originally going to publish a corrected version of the code myself, but Scott has this covered nicely.
Ah well. All we can do now is laugh.
dftf
<p>I was expecting at-least one person to say "what was it… the source-code to <em>Windows</em>"!</p>