David works for a company that writes applications that care about when things happen. Needing a bit more time to work on other things in-house, they outsourced some module development to China.

In his own words,

They helped us to develop some modules, and my boss asked me to review one of them. It seemed to be working fine, so I decided to look into source codes. I shouldn't have do it.

  function weekday($year,$month,$day)
  {
      $corrected_year=$year;
      if(($month<3))
          $corrected_year--;
      $leap_years=(intval($corrected_year/4));
      switch($month)
      {
          default:
          case  1: $month_year_day=0; break;
          case  2: $month_year_day=31; break;
          case  3: $month_year_day=59; break;
          case  4: $month_year_day=90; break;
          case  5: $month_year_day=120; break;
          case  6: $month_year_day=151; break;
          case  7: $month_year_day=181; break;
          case  8: $month_year_day=212; break;
          case  9: $month_year_day=243; break;
          case 10: $month_year_day=273; break;
          case 11: $month_year_day=304; break;
          case 12: $month_year_day=334; break;
      }
      return (intval((intval((-473+365*($year-1970)+$leap_years-
        intval($leap_years/25)+((intval($leap_years % 25)<0) ? 1
        : 0)+intval((intval($leap_years/25))/4)+$month_year_day+
        $day-1) % 7)+7) % 7));
  }

After much contemplation, David thinks this code is supposed to return a UNIX timestamp for the start of a specific date, ignoring the strange leap year calculations. However, he wonders why the developer didn't use mktime(0,0,0,$month,$day,$year). Now David has less time: he found the code so he gets to rewrite the module.

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