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.Moment vape shop boss in Wes Streeting's constituency ridicules disposables ban during undercover probe
Kate Middleton's five signature fashion looks - and how she's transformed the standard for royal style
Donald Trump DROPS historic UK-US trade deal as it is officially agreed and leaves Starmer scrambling
BBC Breakfast's toxic feud 'exposed': Bullying probe is branded 'the tip of the iceberg' with star Naga Munchetty 'at her wits end' as crisis-hit show is 'torn apart by backstage tensions'
Conspiracy of silence over grooming gangs' race doomed thousands of girls to abuse: Sickening evidence covered up 'for fear of appearing racist', damning report finds
Erin Patterson mushroom murder trial LIVE updates: Defence argues there is an 'absence of motive' in Erin Patterson's murder trial as jury hears closing addresses
Freetrade launches fee-free fund dealing: Investing app expands choice for investors
Rayleigh Weir McDonald's shuts for huge refurb
Lilo and Stitch actor David Hekili Kenui Bell dies at age 46 as family pays heartbreaking tribute
Rupert Murdoch's 'wild child' granddaughter Charlotte Freud reveals she has finally found love after disaster 14-month marriage
These Essex postcodes have the worst drivers according to government figures
Modern Family star Aubrey Anderson-Emmons, 18, comes out as bisexual five years after end of iconic sitcom
'Gorgeous' beer garden in 'picturesque' Essex village named one of UK's best
Owner of The Swan says restoration is 'urgent' for future of Ocean's Braintree
'Gorgeous' beer garden in 'picturesque' Essex village named one of UK's best
Witham church submits plans to replace lead roof after multiple targeted thefts
Essex PE teacher who drank alcohol before driving school mini bus allowed to keep teaching
Fugitive diamond tycoon accuses Indian government of 'orchestrating his kidnapping' in Antigua in bid to extradite him over alleged bank fraud
Social Media Now Main Source of News In US, Research Suggests
Read more of this story at Slashdot.