Interesting new implementation of DateTime.Now.Date
-
Hello there. The codebase I have been thrust into contains many an interesting snippet. Today I shall present to you GetDayBeginning, or as sane people say, DateTime.Now.Date. And yes, I've checked, the Date property has been there since .NET 1.1.
public static DateTime GetDayBeginning(this DateTime pValue) { DateTime result = DateTime.MinValue; try { result = result.AddYears(DateTime.Now.Year - 1); result = result.AddMonths(DateTime.Now.Month - 1); result = result.AddDays(DateTime.Now.Day - 1); } catch (Exception) { } return result; }
Marvel not only at the complete insanity of reinventing the wheel, but also doing it in the shape of a pentagram. Ask yourself this: Which exceptions could possibly be thrown here? If by some miracle an exception is thrown, why does the developer prefer a wrong result to propagating this exception? Would the developer be less crazy had he returned
new DateTime(pValue.Year, pValue.Month, pValue.Day)
? Why is the parameter using Hungarian notation anyway? Will calling this method awaken dead Cthulhu from his slumber of aeons?As luck would have it, the method was completely unused and was quickly cast back into the dark abyss it once came from.
-
@iughruthlir said in Interesting new implementation of DateTime.Now.Date:
Would the developer be less crazy had he returned new DateTime(pValue.Year, pValue.Month, pValue.Day)?
Certainly, although this raises the question of whether he actually wanted pValue.Date or DateTime.Now.Date. Given that the method was not being used anywhere, it's hard to tell.
But it would be difficult to make this worse. Maybe if you pulled out the year, month, and day values, and multiplied them each by a corresponding number of ticks (with an error in calculating one of the conversion constants), and then used that number of ticks to create your DateTime.
-
This post is deleted!
-
@Scarlet_Manuka said in Interesting new implementation of DateTime.Now.Date:
@iughruthlir said in Interesting new implementation of DateTime.Now.Date:
Would the developer be less crazy had he returned new DateTime(pValue.Year, pValue.Month, pValue.Day)?
Certainly, although this raises the question of whether he actually wanted pValue.Date or DateTime.Now.Date. Given that the method was not being used anywhere, it's hard to tell.
But it would be difficult to make this worse. Maybe if you pulled out the year, month, and day values, and multiplied them each by a corresponding number of ticks (with an error in calculating one of the conversion constants), and then used that number of ticks to create your DateTime.
A valid point. This raises a further question: Why does this method take a parameter?
-
@iughruthlir said in Interesting new implementation of DateTime.Now.Date:
Why does this method take a parameter?
It wouldn't have been nearly as bewildering a without one. Granted, that may not have been the coder's primary intention, but judging by the rest of the method, I wouldn't rule it out either.
-
@iughruthlir said in Interesting new implementation of DateTime.Now.Date:
Why does this method take a parameter?
It's a
this
parameter, which makes it an extension method so you can domyDateTime.GetDayBeginning()
, and get the wrong result unless the DateTime object is some time in the current day.@iughruthlir said in Interesting new implementation of DateTime.Now.Date:
And yes, I've checked, the Date property has been there since .NET 1.1.
So has
DateTime.Today
-
@iughruthlir said in Interesting new implementation of DateTime.Now.Date:
And yes, I've checked, the Date property has been there since .NET 1.1.
So has
DateTime.Today
Umm... the parameter threw me off? Yes, that was definitely it.
-
Wait, so this is an extension method that doesn't use its "this" parameter?
So DateTime.Now.GetDayBeginning() == GetMyBirthday().GetDayBeginning()?
-
@iughruthlir said in Interesting new implementation of DateTime.Now.Date:
As luck would have it, the method was completely unused and was quickly cast back into the dark abyss it once came from.
I guess someone found this already, but just change it back to DateTime.Now.Date or DateTime.Today, without bother to remove the method.
But there would be too much sanity to make a TDWTF article this way.
-
@CreatedToDislikeThis Look, it gets the beginning of the day, what more do you want? I don't care when your birthday is, the beginning of the day was still just under 18 hours ago. Or 10 hours ago if we're talking UTC.
If you wanted "the beginning of whatever day we're talking about" then you should have said so.
-
@CreatedToDislikeThis said in Interesting new implementation of DateTime.Now.Date:
Wait, so this is an extension method that doesn't use its "this" parameter?
So DateTime.Now.GetDayBeginning() == GetMyBirthday().GetDayBeginning()?If it were defined for a class type (e.g.
DateTime?
), you could even call it with null.
-
@iughruthlir said in Interesting new implementation of DateTime.Now.Date:
If it were defined for a class type (e.g. DateTime?), you could even call it with null.
I was convinced that wouldn't work, and that calling
nullDateTime.GetDayBeginning()
would crash with a NRE, but you're right.
-
You see Ivan, when you write empty class with many extension method, you never need to worry about NullReferenceException, because you get null-object pattern for free.
-
This post is deleted!
-
@iughruthlir That's pretty amazingly awful.
It took me forever to figure out how it "works". Bonus points: taking a param, then utterly ignoring it.
-
@Jaloopa said in Interesting new implementation of DateTime.Now.Date:
It's a
this
parameter, which makes it an extension method so you can do myDateTime.GetDayBeginning(), and get the wrong result unless the DateTime object is some time in the current day.But...the method is
static
! There can be nothis
.
-
@boomzilla Extension methods are weird.
Extension methods are defined as static methods but are called by using instance method syntax
-
@Jaloopa Yuck.
-
@boomzilla said in Interesting new implementation of DateTime.Now.Date:
@Jaloopa Yuck.
It's actually pretty useful. Linq and other fluid interfaces would look much uglier without.
-
@iughruthlir said in Interesting new implementation of DateTime.Now.Date:
It's actually pretty useful.
I never said it wasn't. Just a hacky way of declaring them.
-
@boomzilla said in Interesting new implementation of DateTime.Now.Date:
Just a hacky way of declaring them
I don't trust the aesthetic choices of someone with your heretical brace style
-
@Jaloopa said in Interesting new implementation of DateTime.Now.Date:
I don't trust the aesthetic choices of someone with your heretical brace style
It's not aesthetic, it seems like a good idea to declare one's intention to make something an extension method. I'm sure experienced users are used to seeing that, but it seems like a poor design choice.
Also, it's not my fault that you've been brainwashed into accepting a sub-optimal brace style.
-
@boomzilla said in Interesting new implementation of DateTime.Now.Date:
it seems like a good idea to declare one's intention to make something an extension method
vb.net uses a
<Extension()>
annotation.
-
@boomzilla said in Interesting new implementation of DateTime.Now.Date:
But...the method is static! There can be no this.
Jaloopa is not using the magical words "Extension Method". Things are easier to look up when you know what they're called.
-
@blakeyrat Jesus Christ man. The very post you quoted included me saying it was an extension method. My reply to what you quoted had a link to the msdn documentation on extension methods and later on I mentioned the vb way of doing it where you explicitly annotate a method as an extension
-
@Jaloopa I am not Jesus Christ, but it's an understandable mistake.
-
@blakeyrat no, but you are apparently illiterate, insane and whatever else it is you call yourself
-
@blakeyrat said in Interesting new implementation of DateTime.Now.Date:
@Jaloopa I am not Jesus Christ, but it's an understandable mistake.
That particular joke is so old that Methuselah stumbled over its beard.
-
@Rhywden Wanna hear the one about someone using "fuck" as an expletive but, ha ha ha, what you do is pretend they're talking about the actual physical act of love and misinterpret it on purpose? Hilarious!
-
@blakeyrat No, it's much funnier when you call yourself stupid and an idiot and pretend other people did it.
-
@blakeyrat said in Interesting new implementation of DateTime.Now.Date:
fuck
@blakeyrat said in Interesting new implementation of DateTime.Now.Date:
the actual physical act of love
..... yeah, those are not the same thing.
swap love for lust in that last one and you've got something that's funny.
-
@accalia said in Interesting new implementation of DateTime.Now.Date:
those are not the same thing
Newsflash: alien who doesn't understand metaphor or ambiguity doesn't understand jokes that revolve around ambiguity. Film at 11!
-
@FrostCat ...... i should have seen that coming.
-
@accalia said in Interesting new implementation of DateTime.Now.Date:
@FrostCat ...... i should have seen that coming.
Make sure you're wearing e_protection?
-
I love how the method is declared as an extension method, yet always returns Today.
-
@AlexMedia said in Interesting new implementation of DateTime.Now.Date:
I love how the method is declared as an extension method, yet always returns Today.
Welcome to the thread! loves you!
-
@CreatedToDislikeThis said in Interesting new implementation of DateTime.Now.Date:
Wait, so this is an extension method that doesn't use its "this" parameter?
So DateTime.Now.GetDayBeginning() == GetMyBirthday().GetDayBeginning()?Happy birthday!
-
@ben_lubar said in Interesting new implementation of DateTime.Now.Date:
@CreatedToDislikeThis said in Interesting new implementation of DateTime.Now.Date:
Wait, so this is an extension method that doesn't use its "this" parameter?
So DateTime.Now.GetDayBeginning() == GetMyBirthday().GetDayBeginning()?Happy birthday!
A very merry un-birthday to you!
-
@Tsaukpaetra said in Interesting new implementation of DateTime.Now.Date:
un-birthday
Is that when you think it's your birthday but someone accidentally messed up zero-based versus one-based indexing so it's actually July 18th instead of June 17th?
Because, if so, thanks!
-
@ben_lubar said in Interesting new implementation of DateTime.Now.Date:
@Tsaukpaetra said in Interesting new implementation of DateTime.Now.Date:
un-birthday
Is that when you think it's your birthday but someone accidentally messed up zero-based versus one-based indexing so it's actually July 18th instead of June 17th?
Because, if so, thanks!
Too consistent. Months are zero based, days and years are one based.
Filed under: No, really., Days of the week are zero based but days of the month are one based.
-
-
@ben_lubar said in Interesting new implementation of DateTime.Now.Date:
@error said in Interesting new implementation of DateTime.Now.Date:
Months are zero based
And Sekrets!
-
By comparison, it's reasonable to mishandle some times in the year -292277022399.
-
@ben_lubar said in Interesting new implementation of DateTime.Now.Date:
@error said in Interesting new implementation of DateTime.Now.Date:
Months are zero based
At least Go got it right. I can't understand people who think months should be zero indexed.
-
@boomzilla days of the week starting with Sunday=0 makes sense because you can satisfy the 0-index, 1-index, Sunday-first, and Monday-first people all with one constant.
-
@ben_lubar Yeah, days of the week are weird and not normally referred to by a number outside of computers, so it's not flamingly retarded as picking the obviously wrong numbers the way most languages are stuck with months.
-
@ben_lubar said in Interesting new implementation of DateTime.Now.Date:
days of the week starting with Sunday=0 makes sense because you can satisfy the 0-index, 1-index, Sunday-first, and Monday-first people all with one constant.
Actually you can do a little bit better by just using the value mod 7. Then the Sunday-at-the-end people () can just use 1-based indexing and pretend it all works.
People who insist that the week starts on Saturday are SOL.
-
@dkf said in Interesting new implementation of DateTime.Now.Date:
People who insist that the week starts on Saturday are SOL.
I start a week on Thursday. This way I only work two days, have two days off and then work three days, since working five days in a row is just too exhausting.
-
@Maciejasjmj at the expense of having no week-end to speak of...
-
@iughruthlir said in Interesting new implementation of DateTime.Now.Date:
As luck would have it, the method was completely unused and was quickly cast back into the dark abyss it once came from.
Please tell us that the developer who wrote it was cast into the same abyss.