Is it a leap year?
-
We have this wonderful code snippet to check if the given
year
is a leap year in C#.try { new DateTime(year, 2, 29); return true; } catch { return false; }
An actual solution (Java)
Of course, the only correct solution is to outsource ti to a library that has it correctly implemented.
import org.joda.time.DateTime; import org.joda.time.Chronology; import org.joda.time.chrono.GJChronology;
// ...
// assume these are in a class (PREEMPTIVE PEDANTRY DEFENSE)public boolean isLeapYear(int year) {
Chronology ch = GJChronology.getInstance(); // assume Gregorian/Julian calendar with switchover (most of western world)
DateTime time = new DateTime(year, 0, 0, 0, 0, ch);
return time.year().isLeap();
}public boolean isInstantInLeapYear(long unixTimeMillis) {
return new DateTime(unixTimeMillis, GJChronology.getInstance()).year().isLeap();
}
-
Good solution.
When the fuck did Dicksuck get expandos? I needed one like a week ago and couldn't find shit. DISCOVERABLE!
-
When the fuck did Dicksuck get expandos?
Sorry, no nice solution here: http://what.thedailywtf.com/raw/3306/1Manually wrote HTML with
<details>
. Protip: Use<details open>
when composing
-
Phew, for a second there I thought you might actually support a BBCode feature from like 15 years ago. Good to know your BBCode implementation is just as half-assed as everything else around here.
-
I still see you as part of the Discourse-team... so couldn't you just write a simple "Plug-in" that adds a button to your "button-bard" that just adds that HTML to the reply you are composing?
Asking you like this is probably faster than asking Jeff to implement it in any markdown way (and less error-prone, too, as people like to screw around with Markdown in this forum)I don't really see the WTF-ness in the code. Yeah, it's a bit hacky but it works. C# does indeed throw an out of range exception and you don't have to bother with it. Why not let the language handle the testing? (unless you are logging out of range-errors in which case you get a bigger log-file).
Filed Under: How long does it take somebody on the Discourse-Team to create a plugin, anyway?
Edit:
I take it back: http://msdn.microsoft.com/de-de/library/system.datetime.isleapyear(v=vs.110).aspx exists.... why not use that?
-
I take it back: http://msdn.microsoft.com/de-de/library/system.datetime.isleapyear(v=vs.110).aspx exists.... why not use that?
Ignorance, most likely.
-
Protip: Use <details open> when composing
BUG:
Can't see what I'm doing when I use
-
I don't really see the WTF-ness in the code. Yeah, it's a bit hacky but it works.
It's using an exception for flow control.
I'm pretty sure everybody agreed that that was a WTF a while ago.
EDIT: Oh, and the exception is triggered in the common case - even worse.
-
Obviously, the correct code is this:
DateTime bleh; return DateTime.TryParse("2/29/{0}".Format(year), out bleh);
Especially if you're in Europe.
-
psh, clearly the best way to do it is this:
[code]
private static bool isLeapYear(){
return (new DateTime( DateTime.Now.Year, 3, 1, 0, 0, 0)).Subtract( new DateTime(DateTime.Now.Year, 2, 28, 0, 0, 0)).Hours == 48;
}
[/code]edit - oops, had some things too java-y for c#
even tested in .NET fiddle