Connect with us

Humor

Time Changes

[ad_1]

Dates and times are way more complicated than we usually think they are, especially when we’re talking about historical dates and times. The solution, of course, is to always use someone else’s work, whether it’s a library or your language’s built-ins, never implement date handling yourself.

For a long time, though, Java’s date handling left something to be desired. Which is why Sven found this history lesson in his company’s code base:



boolean is16mei1940 = year == 1940 && month == 4 && day == 16;




boolean is1juli1937 = year == 1937 && month == 6 && day == 1;




boolean is1mei1916 = year == 1916 && month == 5 && day == 1;

calendar.setLenient(is16mei1940 || is1juli1937 || is1mei1916);
calendar.set(year, month, day);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);

I’ll let Sven explain the bit about the bicycles:

The comment about Germans not returning our bicycles is an old joke in the Netherlands referring to the second World War during which the Germans confiscated large numbers of bicycles from Dutch citizens during their occupation of the Netherlands.

Given how much of this centers on Nazi occupation and also World War I, I wouldn’t call this a fun history lesson, so let’s get into the code.

The setLenient function controls how strict date parsing is. From the docs:

With lenient interpretation, a date such as “February 942, 1996” will be treated as being equivalent to the 941st day after February 1, 1996. With strict (non-lenient) interpretation, such dates will cause an exception to be thrown. The default is lenient.

You’ll note, though, that we’re not using parsing here. Which raises the question of: why is this happening? I can’t say that I’m sure. Perhaps the documentation is misleading and the original developer found an edge case? Do they explicitly want to disable lenient parsing for some future parse operation Sven didn’t submit?

But then we set various properties. The set method is overloaded, as you can see here, but what you don’t see is that there’s a 5 parameter version which lets you set the year, month, day, hour, and minute, all in a single operation. So I suspect that this could easily be a one-liner.

But, as always, the real WTF is how complicated date times actually are.

[Advertisement]
BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!

[ad_2]

Remy Porter

Source link