CodeSOD: The Hard Problem
I'll warn you to start: this is a date handling CodeSOD, but that's only a small part of the WTF. You see, there's an old joke, "There are three hard problems in computer science: naming things and counting things." This code has a hard time with the first:
private string ReturnCurrentGMTTime() { string result = string.Empty; DateTime time = DateTime.Now; string fs = "yyyy-MM-dd'T'HH:mm:ss"; result = time.ToString(fs); result += "+02:00"; return result; }
Robert sent this in, after receiving it from a contractor.
This method name is a thing of beauty, simply because it is so wrong. Everything about it is wrong. The first problem is small, but rarely do we use the word "Return" as part of a method name. GetCurrentGMTTime would be better, but as this is C#, the convention is just to treat it like a property: CurrentGMTTime. Of course, GMT stands for "Greenwich Mean Time", so like "ATM Machine", our method name remains wrong. Perhaps it should be CurrentGMT or CurrentGMTime.
But that's small potatoes. Not only is the method just poorly named, it's wildly inaccurate. DateTime.now only returns GMT in the event that you are on a machine in that time zone- which I happen to know that Robert is not. He's actually in Denmark- which explains the "+02:00"- during Daylight Savings Time, that is their offset from GMT.
So, the method isn't ReturnCurrentGMTTime, but it is CurrentTimeWithTimeZone. And, of course, this formatting uses some of the built-in formatting functionality from .NET, but decides in the end to use string concatenation to take it the last mile. In the end, it's not wrong- just stupid.
[Advertisement] BuildMaster is more than just an automation tool: it brings together the people, process, and practices that allow teams to deliver software rapidly, reliably, and responsibly. And it's incredibly easy to get started; download now and use the built-in tutorials and wizards to get your builds and/or deploys automated!