Date and Time computations aren’t easy. Unless you’re fortunate enough to use Metric Time, there are a whole lot of uneven measurements to work with. Sixty seconds in a minute. Twenty four hours in a day. Thirty, thirty one, twenty eight, maybe twenty nine days in a month. Fifty two point something weeks in a year. It’s just ugly.

Fortunately, just about every programming language out there has library code to help with the math. Naturally, many “certain” programmers chose to ignore this library code and hack their own. Like Yuri’s predecessor. He was presented with a simple problem: add nineteen seconds to the current date/time in Perl.

my @date = gmtime(time);

my $year = $date[5]+1900;
my $month = $date[4]+1;
my $day = $date[3];
my $hour = $date[2]+4;
my $min = $date[1];
my $sec = $date[0];

if ($sec < 41){$sec = $sec + 19;}
else{
   $sec = $sec + 19 - 60;
   if ($min < 59){$min++;}
   else{
      $min = 0;
      if ($hour < 23){$hour++;}
      else{
         $hour = 0;
         if ($month < 8){
            if (($month%2)!= 0){
               if ($day < 31){$day++;}
               else{$day = 1;$month++;}
            }
            else{
               if ($month == 2){
                  if (($year == 2008)||($year == 2012)){
                     if ($day < 29){$day++;}
                     else{$day = 1;$month = 3;}
                  }
                  else{
                     if ($day < 28){$day++;}
                     else{$day = 1;$month = 3;}
                  }
               }
               elsif ($day < 30){$day++;}
               elsif ($day == 30){$day = 1;$month++;}
            }
         }
         elsif ($month < 12){
            if (($month%2) == 0){
               if ($day < 31){$day++;}
               else{$day = 1;$month++;}
            }
            else{
               if ($day < 30){$day++;}
               else {$day = 1;$month++;
               }
            }
         }
         elsif ($month == 12){$day = 1;$month = 1;$year++;}
      }
   }
}

Interestingly enough, the coder did use the gmtime library, but only to split the current date/time (“time” function, which returns the number of seconds since epoch), so he could add nineteen seconds to it. Yuri replaced the function with his own code:

my @ date = gmtime (time + 19);


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