One of Michael's employer's clients wanted a system to catalog and retrieve security videos. The resulting product was partly written in C++, partly written in PHP. The code is like poetry...really, really bad poetry.

One day, the client complained that the dates and times weren't being recorded right. Michael took a look in the database and double-checked the UNIX timestamps of a few on the web page. He confirmed that yes - they were recorded just fine. Surely the client must have been imagining things, Michael thought as he closed the bug as 'couldn't reproduce'.

However, that didn't satisfy the client - they insisted that he take another look, but this time at a specific video. Sure enough, he found that despite his earlier checking, the date was indeed wrong. At first, Michael suspected the bug was a result of the system correcting for UTC or daylight savings twice or something, but the videos were off by different amounts; an hour earlier here, two hours later there, and only in the evening. All the videos taken in the morning were fine.

The fact that only a few videos were impacted explained why he couldn't find the issue before, but when Michael finally found the problem part of the code, he had to wonder if HE was imagining things.

function getTimeFromISODateTime($date) {
  list ($day, $time) = split (" ", $date);
  list ($hour, $min, $sec) = split (":", $time);

  if ($hour > 12) {
    $hour = 24 - $hour;
    $ampm = "PM";
  } else {
    $ampm = "AM";

  if (strlen($hour) == 1) $hour = "0" . $hour;

  return $hour . ":" . $min . ":" . $sec . " " . $ampm; 
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!