Article 4JFX Coded Smorgasbord: Dating Woes

Coded Smorgasbord: Dating Woes

by
Ellis Morning
from The Daily WTF on (#4JFX)

As we all know- especially those of us who just "sprung ahead" this weekend with Daylight Saving Time- dates, times, and time zones are complicated beasts. Handling them programmatically is even harder. Computerphile put together a great video on this topic, which is worth watching to understand why we receive so many Error'd submissions regarding downloads that claim they won't finish until after the heat death of the universe.

Here are some examples of mind-bending code attempting to wrangle with an already mind-bending topic...

First up, Tom discovered this date formatting gem in an inherited application, in the Page_Load of several pages:

// To display the date in the UK fromat.string[] Months ={ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };int Day, Month, Year;string dt = DateTime.Today.ToShortDateString();string dt = DateTime.Today.ToString("MM/dd/yyyy");string[] temp = dt.Split('/');string date = null;Month = int.Parse(temp[0]) - 1;Day = int.Parse(temp[1]);Year = int.Parse(temp[2]);date = Day + "-" + Months[Month] + "-" + Year;

Next, this highly questionable conditional Andrew found in his product- three times. Maybe next time, someone should just spend ten seconds Googling what DateTime.MaxValue is...

if (date.Date == new DateTime(9999, 12, 31)){ date.Date = DateTime.MaxValue;}

Brian found a lovely business day function in PHP that had been committed with the message, "fix for memorial day 2012:"

private function isBusinessDay( DateTime $date ) { $dateStr = $date->format('Y-m-d'); $dayOfWeek = (int) $date->format('w'); return !( $dayOfWeek === 0 || $dayOfWeek === 6 || $dateStr == "2012-05-28" ); }

An Anonymous friend writes: "As a teaching assistant on a course on software architecture I frequently encounter code which makes me cringe. However, I've rarely seen such a commitment to statically typed languages before..."

public void Remove(Calendar calendar) { // Preconditions Calendar c = new Calendar(); if (c.GetType() != calendar.GetType()) { throw new InvalidOperationException("calendar param has wrong type."); } ... }

Finally, a delightful Perl sample from Jim N. "One of the other programmers here is particularly fond of 'clever' Perl. This kind of date string generation exists throughout our application. There's lots of other areas of code that could be replaced with old, stable CPAN libraries, but... nope. This whole slug of WTFery can be replaced with: DateTime->now()->strfmtime("%Y_%m_%d_%H%M");"

sub get_timestamp{ my $year = (localtime)[5] + 1900; my $month = (localtime)[4] + 1; if ( $month =~ /^\d{1}$/ ) { $month = '0' . $month; } my $day = (localtime)[3]; if ( $day =~ /^\d{1}$/ ) { $day = '0' . $day; } my $hour = (localtime)[2]; if ( $hour =~ /^\d{1}$/ ) { $hour = '0' . $hour; } my $min = (localtime)[1]; if ( $min =~ /^\d{1}$/ ) { $min = '0' . $min; } my $sec = (localtime)[0]; if ( $sec =~ /^\d{1}$/ ) { $sec = '0' . $sec; } my $res = $year . '_' . $month . '_' . $day . '_' . $hour . $min; return $res;}
inedo50.png[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! TheDailyWtf?d=yIl2AUoC8zADaut8m0KlGg
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments