From a96c2551efe9f37d4bb71c73468c25343a3e5723 Mon Sep 17 00:00:00 2001 From: Albert Santoni Date: Thu, 12 Dec 2013 13:28:51 -0500 Subject: [PATCH] CC-5627 : Check all Application_Common_DateHelper calculations that use timezone. * Fixed GetPlayOrderRange to be consistently return everything in UTC. * ApiController liveInfoAction now returns everything consistently in the station timezone. This fixes negative remaining time that could occur in the embeddable JS Airtime widgets if you were logged in to Airtime while you viewed your website. (The widgets display in the browser's local time.) * ScheduleController getCurrentPlaylistAction() returns everything consistently in the user timezone now. --- airtime_mvc/application/common/DateHelper.php | 23 +++++++++++++++++++ .../application/controllers/ApiController.php | 9 ++++++++ .../controllers/ScheduleController.php | 8 ++++++- airtime_mvc/application/models/Schedule.php | 20 ++++++++-------- 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/airtime_mvc/application/common/DateHelper.php b/airtime_mvc/application/common/DateHelper.php index 823151c2f..07ef3bd18 100644 --- a/airtime_mvc/application/common/DateHelper.php +++ b/airtime_mvc/application/common/DateHelper.php @@ -45,6 +45,29 @@ class Application_Common_DateHelper return gmdate("H:i:s", $this->_dateTime); } + /** Get the abbreviated timezone for the currently logged in user. + * @return A string containing the short form of the timezone set in the preferences for the current user (eg. EST, CEST, etc.) + */ + public static function getUserTimezoneAbbreviation() + { + return self::getTimezoneAbbreviation(Application_Model_Preference::GetUserTimezone()); + } + + /** Get the abbreviated timezone string of the timezone the station is set to. + * @return A string containing the short form of the station's timezone (eg. EST, CEST, etc.) + */ + public static function getStationTimezoneAbbreviation() + { + return self::getTimezoneAbbreviation(Application_Model_Preference::GetDefaultTimezone()); + } + + private static function getTimezoneAbbreviation($fullTimeZoneName) + { + $timeZone = new DateTimeZone($fullTimeZoneName); + $now = new DateTime("now", $timeZone); + return $now->format("T"); + } + public static function getUserTimezoneOffset() { $userTimezone = new DateTimeZone(Application_Model_Preference::GetUserTimezone()); diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index da671cd80..65306306a 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -185,6 +185,7 @@ class ApiController extends Zend_Controller_Action } } + //Used by the SaaS monitoring public function onAirLightAction() { $this->view->layout()->disableLayout(); @@ -286,6 +287,14 @@ class ApiController extends Zend_Controller_Action $next["name"] = htmlspecialchars($next["name"]); } + //For consistency, all times here are being sent in the station timezone, which + //seems to be what we've normalized everything to. + + //Convert the UTC scheduler time ("now") to the station timezone. + $result["schedulerTime"] = Application_Common_DateHelper::UTCStringToStationTimezoneString($result["schedulerTime"]); + $result["timezone"] = Application_Common_DateHelper::getStationTimezoneAbbreviation(); + $result["timezoneOffset"] = Application_Common_DateHelper::getStationTimezoneOffset(); + //Convert from UTC to station time for Web Browser. Application_Common_DateHelper::convertTimestamps($result["currentShow"], array("starts", "ends", "start_timestamp", "end_timestamp"), diff --git a/airtime_mvc/application/controllers/ScheduleController.php b/airtime_mvc/application/controllers/ScheduleController.php index 9737d6231..f77282926 100644 --- a/airtime_mvc/application/controllers/ScheduleController.php +++ b/airtime_mvc/application/controllers/ScheduleController.php @@ -263,6 +263,8 @@ class ScheduleController extends Zend_Controller_Action $show = Application_Model_Show::getCurrentShow(); /* Convert all UTC times to localtime before sending back to user. */ + $range["schedulerTime"] = Application_Common_DateHelper::UTCStringToUserTimezoneString($range["schedulerTime"]); + if (isset($range["previous"])) { $range["previous"]["starts"] = Application_Common_DateHelper::UTCStringToUserTimezoneString($range["previous"]["starts"]); $range["previous"]["ends"] = Application_Common_DateHelper::UTCStringToUserTimezoneString($range["previous"]["ends"]); @@ -275,7 +277,7 @@ class ScheduleController extends Zend_Controller_Action $range["next"]["starts"] = Application_Common_DateHelper::UTCStringToUserTimezoneString($range["next"]["starts"]); $range["next"]["ends"] = Application_Common_DateHelper::UTCStringToUserTimezoneString($range["next"]["ends"]); } - + Application_Common_DateHelper::convertTimestamps( $range["currentShow"], array("starts", "ends", "start_timestamp", "end_timestamp"), @@ -287,6 +289,10 @@ class ScheduleController extends Zend_Controller_Action "user" ); + //TODO: Add timezone and timezoneOffset back into the ApiController's results. + $range["timezone"] = Application_Common_DateHelper::getUserTimezoneAbbreviation(); + $range["timezoneOffset"] = Application_Common_DateHelper::getUserTimezoneOffset(); + $source_status = array(); $switch_status = array(); $live_dj = Application_Model_Preference::GetSourceStatus("live_dj"); diff --git a/airtime_mvc/application/models/Schedule.php b/airtime_mvc/application/models/Schedule.php index 596cbe4fc..2a4a442ea 100644 --- a/airtime_mvc/application/models/Schedule.php +++ b/airtime_mvc/application/models/Schedule.php @@ -65,6 +65,8 @@ SQL; */ public static function GetPlayOrderRange($p_prev = 1, $p_next = 1) { + //Everything in this function must be done in UTC. You will get a swift kick in the pants if you mess that up. + if (!is_int($p_prev) || !is_int($p_next)) { //must enter integers to specify ranges Logging::info("Invalid range parameters: $p_prev or $p_next"); @@ -72,12 +74,8 @@ SQL; return array(); } - $displayTimeZone = new DateTimeZone(Application_Model_Preference::GetTimezone()); - $displayNow = new DateTime("now", $displayTimeZone); + $utcNow = new DateTime("now", new DateTimeZone("UTC")); - $utcNow = clone $displayNow; - $utcNow->setTimezone(new DateTimeZone("UTC")); - $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; @@ -85,14 +83,14 @@ SQL; $results = self::GetPrevCurrentNext($previousShowID, $currentShowID, $nextShowID, $utcNow); $range = array("env"=>APPLICATION_ENV, - "schedulerTime"=> $displayNow->format("Y-m-d H:i:s"), + "schedulerTime"=> $utcNow->format("Y-m-d H:i:s"), + //Previous, current, next songs! "previous"=>$results['previous'] !=null?$results['previous']:(count($shows['previousShow'])>0?$shows['previousShow'][0]:null), "current"=>$results['current'] !=null?$results['current']:((count($shows['currentShow'])>0 && $shows['currentShow'][0]['record'] == 1)?$shows['currentShow'][0]:null), "next"=> $results['next'] !=null?$results['next']:(count($shows['nextShow'])>0?$shows['nextShow'][0]:null), + //Current and next shows "currentShow"=>$shows['currentShow'], "nextShow"=>$shows['nextShow'], - "timezone"=> $displayNow->format("T"), - "timezoneOffset"=> $displayNow->format("Z") ); return $range; @@ -831,18 +829,18 @@ SQL; { $CC_CONFIG = Config::getConfig(); - $scheduleTimeZone = new DateTimeZone('UTC'); + $utcTimeZone = new DateTimeZone('UTC'); /* if $p_fromDateTime and $p_toDateTime function parameters are null, then set range * from "now" to "now + 24 hours". */ if (is_null($p_fromDateTime)) { - $t1 = new DateTime("@".time(), $scheduleTimeZone); + $t1 = new DateTime("@".time(), $utcTimeZone); $range_start = $t1->format("Y-m-d H:i:s"); } else { $range_start = Application_Model_Schedule::PypoTimeToAirtimeTime($p_fromDateTime); } if (is_null($p_fromDateTime)) { - $t2 = new DateTime("@".time(), $scheduleTimeZone); + $t2 = new DateTime("@".time(), $utcTimeZone); $cache_ahead_hours = $CC_CONFIG["cache_ahead_hours"];