- Feature Articles
- CodeSOD
-
Error'd
- Most Recent Articles
- Lucky Penny
- Mike's Job Search Job
- Teamwork
- Cuts Like a Knife
- Charge Me
- Que Sera, Sera
- Hot Dog
- Sentinel Headline
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
My initial guess was that it's a shitty way of making a copy, but DateTime is a struct and thus always copied on assignment. Maybe they are trying to discard sub-second precision? Or just don't understand structs?
Admin
You left out "or just don't understand"...
Admin
Grey cells? What, is this prison?
Admin
So ... am I missing something, or could they just have checked member.PubDate.HasValue instead of parsing?
Admin
Don't forget that they forgot the culture! So, the ToString call on the initial DateTime will be formatted according to the current culture, which could be day/month/year. However, the parse is given an exact format string, so the culture (null implies current IIRC) is mostly disregarded in favor of the format string they specified.
As @anExactDate theorized, they're either incompetent or were trying to discard sub-second precision. My money is on the former.
Admin
"Which sure, this is almost certainly server-side code running on a single server with a well known locale configured. So this probably won't ever blow up on them, but it's 100% the kind of thing everyone thinks is fine until the day it's not."
That's really fun to find out when you're in some kind of emergency. Your physical server dies so you push your backup to a VM to stay online. A day later all your new orders show up in the wrong month. Oh, your local server had a proper locale setting put in by your admin in 2007 and the VMs are all clones of a default en-US image? Well, better get on with manually adjusting all those dd-mm to mm-dd.
Admin
It's likely a Poirot reference. :)
Admin
"well known locale configured" Ha. Only US coders will use that excuse.
Admin
Yeah, looks to me like is was written by a Java developer. I have encountered a lot of times where deep copies are wrongfully made like this (there's better ways to do it) and when it is value types, it was always a Java developer who made it through the HR process because those people often think that .net and Java is the same. To be frank, it's not their fault, to me woman and man are the same too but HR people would completely disagree.
Admin
Maybe I'm overlooking something, but could it be that member.PubDate is NOT of type datetime at all, yet has a well known tasting function?
Admin
Re. Easy Reader Version, cuneiform is text too.
Admin
TRWTF is the ad being commented out by the easy reader version
Admin
This actually does something--zaps anything that isn't a correctly formatted date. Whether that is a desirable action or not depends on the situation...
Admin
Note that while
DateTime.ToString()
has an overload that allows specifying a format string,Nullable<T>.ToString()
does not.Admin
Grey cells is often used as an synonym (or perhaps an idiom) for brain cells. Think it might be a British thing.
Admin
member.PubDate?.ToString() ?? DateTime.MinValue.ToString()
Addendum 2025-05-16 16:02: eta: with or without a format string in each. For real funsies, use a different one in each
Admin
Strings are much harder than they appear to be, so yes, they're very often the problem. But switching to cuneiform won't help, unless you also switch to clay tablets instead of ones based on silicon.
Admin
Re "nullable types- the entire point is to be able to handle null values without having to worry about exceptions getting tossed around", I disagree strongly with this. The entire point of nullable types is to be able to have a null value for a value type. Otherwise, there is no sentinel value you can use for not-present. With a bare value type, you don't have to worry about exceptions getting tossed around. With nullable types, you DO have to worry about exceptions getting tossed around unless you check to make sure a value exists, which is the same as with reference types where you have to check to make sure it's not null.