From a9bd34fbcb414165e9a369b128229dc5ad2a5408 Mon Sep 17 00:00:00 2001 From: Albert Santoni Date: Wed, 11 Dec 2013 18:20:40 -0500 Subject: [PATCH] CC-5627: Check all Application_Common_DateHelper calculations that use timezone. * Eliminate more strtotime usage, could fix on air light weirdness in some circumstances. --- airtime_mvc/application/models/Schedule.php | 23 +++++++++------ airtime_mvc/application/models/Show.php | 31 ++++++++++++++------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/airtime_mvc/application/models/Schedule.php b/airtime_mvc/application/models/Schedule.php index 6271f9a4d..d10f8e435 100644 --- a/airtime_mvc/application/models/Schedule.php +++ b/airtime_mvc/application/models/Schedule.php @@ -78,11 +78,11 @@ SQL; $utcNow = clone $displayNow; $utcNow->setTimezone(new DateTimeZone("UTC")); - $shows = Application_Model_Show::getPrevCurrentNext($utcNow->format("Y-m-d H:i:s")); + $shows = Application_Model_Show::getPrevCurrentNext($utcNow); $previousShowID = count($shows['previousShow'])>0?$shows['previousShow'][0]['instance_id']:null; $currentShowID = count($shows['currentShow'])>0?$shows['currentShow'][0]['instance_id']:null; $nextShowID = count($shows['nextShow'])>0?$shows['nextShow'][0]['instance_id']:null; - $results = self::GetPrevCurrentNext($previousShowID, $currentShowID, $nextShowID, $utcNow->format("Y-m-d H:i:s")); + $results = self::GetPrevCurrentNext($previousShowID, $currentShowID, $nextShowID, $utcNow); $range = array("env"=>APPLICATION_ENV, "schedulerTime"=> $displayNow->format("Y-m-d H:i:s"), @@ -108,8 +108,12 @@ SQL; * through this mechanism a call is made to the old way of querying * the database to find the track info. **/ - public static function GetPrevCurrentNext($p_previousShowID, $p_currentShowID, $p_nextShowID, $p_timeNow) + public static function GetPrevCurrentNext($p_previousShowID, $p_currentShowID, $p_nextShowID, $utcNow) { + $timeZone = new DateTimeZone("UTC"); //This function works entirely in UTC. + assert(get_class($utcNow) === "DateTime"); + assert($utcNow->getTimeZone() == $timeZone); + if ($p_previousShowID == null && $p_currentShowID == null && $p_nextShowID == null) { return; } @@ -167,14 +171,17 @@ SQL; $results['current'] = null; $results['next'] = null; - $timeNowAsMillis = strtotime($p_timeNow); for ($i = 0; $i < $numberOfRows; ++$i) { + // if the show is overbooked, then update the track end time to the end of the show time. if ($rows[$i]['ends'] > $rows[$i]["show_ends"]) { $rows[$i]['ends'] = $rows[$i]["show_ends"]; } - - if ((strtotime($rows[$i]['starts']) <= $timeNowAsMillis) && (strtotime($rows[$i]['ends']) >= $timeNowAsMillis)) { + + $curShowStartTime = new DateTime($rows[$i]['starts'], $timeZone); + $curShowEndTime = new DateTime($rows[$i]['ends'], $timeZone); + + if (($curShowStartTime <= $utcNow) && ($curShowEndTime >= $utcNow)) { if ($i - 1 >= 0) { $results['previous'] = array("name"=>$rows[$i-1]["artist_name"]." - ".$rows[$i-1]["track_title"], "starts"=>$rows[$i-1]["starts"], @@ -195,10 +202,10 @@ SQL; } break; } - if (strtotime($rows[$i]['ends']) < $timeNowAsMillis ) { + if ($curShowEndTime < $utcNow ) { $previousIndex = $i; } - if (strtotime($rows[$i]['starts']) > $timeNowAsMillis) { + if ($curShowStartTime > $utcNow) { $results['next'] = array("name"=>$rows[$i]["artist_name"]." - ".$rows[$i]["track_title"], "starts"=>$rows[$i]["starts"], "ends"=>$rows[$i]["ends"], diff --git a/airtime_mvc/application/models/Show.php b/airtime_mvc/application/models/Show.php index 674533ad7..2065ed425 100644 --- a/airtime_mvc/application/models/Show.php +++ b/airtime_mvc/application/models/Show.php @@ -1116,9 +1116,17 @@ SQL; /** * Gets the current show, previous and next with an 2day window from * the given timeNow, so timeNow-2days and timeNow+2days. + * + * @param $utcNow A DateTime object containing the current time in UTC. + * @return An array (with stupid sub-arrays) containing the previous show id, + * current show id, and next show id. */ - public static function getPrevCurrentNext($p_timeNow) + public static function getPrevCurrentNext($utcNow) { + $timeZone = new DateTimeZone("UTC"); //This function works entirely in UTC. + assert(get_class($utcNow) === "DateTime"); + assert($utcNow->getTimeZone() == $timeZone); + $CC_CONFIG = Config::getConfig(); $con = Propel::getConnection(); // @@ -1144,9 +1152,10 @@ ORDER BY si.starts SQL; $stmt = $con->prepare($sql); - - $stmt->bindValue(':timeNow1', $p_timeNow); - $stmt->bindValue(':timeNow2', $p_timeNow); + + $utcNowStr = $utcNow->format("Y-m-d H:i:s"); + $stmt->bindValue(':timeNow1', $utcNowStr); + $stmt->bindValue(':timeNow2', $utcNowStr); if ($stmt->execute()) { $rows = $stmt->fetchAll(); @@ -1161,12 +1170,14 @@ SQL; $results['currentShow'] = array(); $results['nextShow'] = array(); - $timeNowAsMillis = strtotime($p_timeNow); - for ($i = 0; $i < $numberOfRows; ++$i) { + //All shows start/end times are stored in the database as UTC. + $showStartTime = new DateTime($rows[$i]['starts'], $timeZone); + $showEndTime = new DateTime($rows[$i]['ends'], $timeZone); + //Find the show that is within the current time. - if ((strtotime($rows[$i]['starts']) <= $timeNowAsMillis) - && (strtotime($rows[$i]['ends']) > $timeNowAsMillis)) { + if (($showStartTime <= $utcNow) && ($showEndTime > $utcNow)) + { if ($i-1 >= 0) { $results['previousShow'][0] = array( "id" => $rows[$i-1]['id'], @@ -1199,11 +1210,11 @@ SQL; break; } //Previous is any row that ends after time now capture it in case we need it later. - if (strtotime($rows[$i]['ends']) < $timeNowAsMillis ) { + if ($showEndTime < $utcNow ) { $previousShowIndex = $i; } //if we hit this we know we've gone to far and can stop looping. - if (strtotime($rows[$i]['starts']) > $timeNowAsMillis) { + if ($showStartTime > $utcNow) { $results['nextShow'][0] = array( "id" => $rows[$i]['id'], "instance_id" => $rows[$i]['instance_id'],