Feed aggregator
CodeSOD: A Second Date
Ah, bad date handling. We've all seen it. We all know it. So when Lorenzo sent us this C# function, we almost ignored it:
private string GetTimeStamp(DateTime param) { string retDate = param.Year.ToString() + "-"; if (param.Month < 10) retDate = retDate + "0" + param.Month.ToString() + "-"; else retDate = retDate + param.Month.ToString() + "-"; if (param.Day < 10) retDate = retDate + "0" + param.Day.ToString() + " "; else retDate = retDate + param.Day.ToString() + " "; if (param.Hour < 10) retDate = retDate + "0" + param.Hour.ToString() + ":"; else retDate = retDate + param.Hour.ToString() + ":"; if (param.Minute < 10) retDate = retDate + "0" + param.Minute.ToString() + ":"; else retDate = retDate + param.Minute.ToString() + ":"; if (param.Second < 10) retDate = retDate + "0" + param.Second.ToString() + "."; else retDate = retDate + param.Second.ToString() + "."; if (param.Millisecond < 10) retDate = retDate + "0" + param.Millisecond.ToString(); else retDate = retDate + param.Millisecond.ToString(); return retDate; }Most of this function isn't terribly exciting. We've seen this kind of bad code before, but even when we see a repeat like this, there are still special treats in it. Look at the section for handling milliseconds: if the number is less than 10, they pad it with a leading zero. Just the one, though. One leading zero should be enough for everybody.
But that's not the thing that makes this code special. You see, there's another function worth looking at:
private string FileTimeStamp(DateTime param) { string retDate = param.Year.ToString() + "-"; if (param.Month < 10) retDate = retDate + "0" + param.Month.ToString() + "-"; else retDate = retDate + param.Month.ToString() + "-"; if (param.Day < 10) retDate = retDate + "0" + param.Day.ToString() + " "; else retDate = retDate + param.Day.ToString() + " "; if (param.Hour < 10) retDate = retDate + "0" + param.Hour.ToString() + ":"; else retDate = retDate + param.Hour.ToString() + ":"; if (param.Minute < 10) retDate = retDate + "0" + param.Minute.ToString() + ":"; else retDate = retDate + param.Minute.ToString() + ":"; if (param.Second < 10) retDate = retDate + "0" + param.Second.ToString() + "."; else retDate = retDate + param.Second.ToString() + "."; if (param.Millisecond < 10) retDate = retDate + "0" + param.Millisecond.ToString(); else retDate = retDate + param.Millisecond.ToString(); return retDate; }Not only did they fail to learn the built-in functions for formatting dates, they forgot about the functions they wrote for formatting dates, and just wrote (or realistically, copy/pasted?) the same function twice.
At least both versions have the same bug with milliseconds. I don't know if I could handle it if they were inconsistent about that.
[Advertisement] Picking up NuGet is easy. Getting good at it takes time. Download our guide to learn the best practice of NuGet for the Enterprise.Social Media Now Main Source of News In US, Research Suggests
Read more of this story at Slashdot.
Your Brain Has a Hidden Beat -- and Smarter Minds Sync To It
Read more of this story at Slashdot.
Google Cloud Caused Outage By Ignoring Its Usual Code Quality Protections
Read more of this story at Slashdot.
Intel Will Lay Off 15% To 20% of Its Factory Workers, Memo Says
Read more of this story at Slashdot.
Vandals Cut Fiber-Optic Lines, Causing Outage For Spectrum Internet Subscribers
Read more of this story at Slashdot.
Threads Will Let You Hide Spoilers In Your Posts
Read more of this story at Slashdot.
Salesforce Study Finds LLM Agents Flunk CRM and Confidentiality Tests
Read more of this story at Slashdot.
The US Navy Is More Aggressively Telling Startups, 'We Want You'
Read more of this story at Slashdot.
Obscure Chinese Stock Scams Dupe American Investors by the Thousands
Read more of this story at Slashdot.
OpenAI, Growing Frustrated With Microsoft, Has Discussed Making Antitrust Complaints To Regulators
Read more of this story at Slashdot.
That 'Unsubscribe' Button Could Be a Trap, Researchers Warn
Read more of this story at Slashdot.
Dutch Court Confirms Apple Abused Dominant Position in Dating Apps
Read more of this story at Slashdot.
Windows Hello Face Unlock No Longer Works in the Dark and Microsoft Says It's Not a Bug
Read more of this story at Slashdot.
Japan Builds Near $700 Million Fund To Lure Foreign Academic Talent
Read more of this story at Slashdot.
Researchers Create World's First Completely Verifiable Random Number Generator
Read more of this story at Slashdot.
Trump Organization Announces Mobile Plan, $499 Smartphone
Read more of this story at Slashdot.
Windows 11 Bug Resurrects Vista's 2006 Boot Sound in Latest Preview Builds
Read more of this story at Slashdot.
Novo Nordisk Loses Canadian Patent Protection For Blockbuster Diabetes Drug Over Unpaid $450 Fee
Read more of this story at Slashdot.
WhatsApp Introduces Ads in Its App
Read more of this story at Slashdot.