CC-2997: Daylight savings time affects repeating shows.

-should work now
This commit is contained in:
Martin Konecny 2011-10-25 16:49:44 -04:00
parent 43721e138d
commit d26e410799

View file

@ -604,16 +604,18 @@ class Application_Model_Show {
return $showInstances;
}
public function hasInstanceOnDate($p_timestamp){
return (!is_null($this->getInstanceOnDate($p_timestamp)));
public function hasInstanceOnDate($p_dateTime){
return (!is_null($this->getInstanceOnDate($p_dateTeim)));
}
public function getInstanceOnDate($p_timestamp){
public function getInstanceOnDate($p_dateTime){
global $CC_DBC;
$timestamp = $p_dateTime->getTimestamp();
$showId = $this->getId();
$sql = "SELECT id FROM cc_show_instances"
." WHERE date(starts) = date(TIMESTAMP '$p_timestamp') "
." WHERE date(starts) = date(TIMESTAMP '$timestamp') "
." AND show_id = $showId";
$row = $CC_DBC->GetOne($sql);
@ -889,18 +891,20 @@ class Application_Model_Show {
* has looked at.
*
* @param int $p_showId
* @param string $p_endTimestamp
* In the format "YYYY-MM-DD HH:mm:ss, non-UTC"
* @param DateTime $p_dateTime
* DateTime object in UTC time.
*/
public static function populateShowUntil($p_showId, $p_endTimestamp = NULL)
public static function populateShowUntil($p_showId, $p_dateTime = NULL)
{
global $CC_DBC;
if (is_null($p_endTimestamp)) {
$p_endTimestamp = Application_Model_Preference::GetShowsPopulatedUntil();
$date = Application_Model_Preference::GetShowsPopulatedUntil();
if ($p_endTimestamp == "") {
$today_timestamp = date("Y-m-d");
Application_Model_Preference::SetShowsPopulatedUntil($today_timestamp);
if ($date == "") {
$p_dateTime = new DateTime("now", new DateTimeZone('UTC'));
Application_Model_Preference::SetShowsPopulatedUntil($p_dateTime->format("Y-m-d"));
} else {
$p_dateTime = new DateTime($date, new DateTimeZone('UTC'));
}
}
@ -908,29 +912,48 @@ class Application_Model_Show {
$res = $CC_DBC->GetAll($sql);
foreach ($res as $showRow) {
Application_Model_Show::populateShow($showRow, $p_endTimestamp);
Application_Model_Show::populateShow($showRow, $p_dateTime);
}
}
private static function populateShow($p_showRow, $p_endTimestamp) {
/**
* We are going to use cc_show_days as a template, to generate Show Instances. This function
* is basically a dispatcher that looks at the show template, and sends it to the correct function
* so that Show Instance generation can begin. After the all show instances have been created, pushes
* the schedule to Pypo.
*
* @param array $p_showRow
* A row from cc_show_days table
* @param DateTime $p_dateTime
* DateTime object in UTC time.
*/
private static function populateShow($p_showRow, $p_dateTime) {
if($p_showRow["repeat_type"] == -1) {
Application_Model_Show::populateNonRepeatingShow($p_showRow, $p_endTimestamp);
Application_Model_Show::populateNonRepeatingShow($p_showRow, $p_dateTime);
}
else if($p_showRow["repeat_type"] == 0) {
Application_Model_Show::populateRepeatingShow($p_showRow, $p_endTimestamp, '7 days');
Application_Model_Show::populateRepeatingShow($p_showRow, $p_dateTime, 'P7D');
}
else if($p_showRow["repeat_type"] == 1) {
Application_Model_Show::populateRepeatingShow($p_showRow, $p_endTimestamp, '14 days');
Application_Model_Show::populateRepeatingShow($p_showRow, $p_dateTime, 'P14D');
}
else if($p_showRow["repeat_type"] == 2) {
Application_Model_Show::populateRepeatingShow($p_showRow, $p_endTimestamp, '1 month');
Application_Model_Show::populateRepeatingShow($p_showRow, $p_dateTime, 'P1M');
}
Application_Model_RabbitMq::PushSchedule();
}
//for a show with repeat_type == -1
private static function populateNonRepeatingShow($p_showRow, $end_timestamp)
/**
* Creates a single show instance. If the show is recorded, it may have multiple
* rebroadcast dates, and so this function will create those as well.
*
* @param array $p_showRow
* A row from cc_show_days table
* @param DateTime $p_dateTime
* DateTime object in UTC time.
*/
private static function populateNonRepeatingShow($p_showRow, $p_dateTime)
{
global $CC_DBC;
@ -943,14 +966,13 @@ class Application_Model_Show {
$timezone = $p_showRow["timezone"];
$start = $first_show." ".$start_time;
$utcStartDateTime = Application_Model_DateHelper::ConvertToUtcDateTime($start, $timezone);
if ($utcStartDateTime->getTimestamp() < $p_dateTime->getTimestamp()) {
$utcStart = $utcStartDateTime->format("Y-m-d H:i:s");
if (strtotime($utcStart) < strtotime($end_timestamp)) {
$sql = "SELECT timestamp '{$utcStart}' + interval '{$duration}'";
$utcEnd = $CC_DBC->GetOne($sql);
$utcEndDateTime = new DateTime($CC_DBC->GetOne($sql), new DateTimeZone("UTC"));
$date = new Application_Model_DateHelper();
$currentUtcTimestamp = $date->getUtcTimestamp();
@ -959,16 +981,15 @@ class Application_Model_Show {
if ($show->hasInstance()){
$ccShowInstance = $show->getInstance();
$newInstance = false;
}
else {
} else {
$ccShowInstance = new CcShowInstances();
$newInstance = true;
}
if ($newInstance || $ccShowInstance->getDbStarts() > $currentUtcTimestamp){
$ccShowInstance->setDbShowId($show_id);
$ccShowInstance->setDbStarts($utcStart);
$ccShowInstance->setDbEnds($utcEnd);
$ccShowInstance->setDbStarts($utcStartDateTime);
$ccShowInstance->setDbEnds($utcEndDateTime);
$ccShowInstance->setDbRecord($record);
$ccShowInstance->save();
}
@ -981,34 +1002,26 @@ class Application_Model_Show {
}
$sql = "SELECT * FROM cc_show_rebroadcast WHERE show_id={$show_id}";
Logging::log($sql);
$rebroadcasts = $CC_DBC->GetAll($sql);
foreach($rebroadcasts as $rebroadcast) {
$timeinfo = explode(" ", $utcStart);
$sql = "SELECT timestamp '{$timeinfo[0]}' + interval '{$rebroadcast["day_offset"]}' + interval '{$rebroadcast["start_time"]}'";
$rebroadcast_start_time = $CC_DBC->GetOne($sql);
$sql = "SELECT timestamp '{$rebroadcast_start_time}' + interval '{$duration}'";
$rebroadcast_end_time = $CC_DBC->GetOne($sql);
if ($rebroadcast_start_time > $currentUtcTimestamp){
$newRebroadcastInstance = new CcShowInstances();
$newRebroadcastInstance->setDbShowId($show_id);
$newRebroadcastInstance->setDbStarts($rebroadcast_start_time);
$newRebroadcastInstance->setDbEnds($rebroadcast_end_time);
$newRebroadcastInstance->setDbRecord(0);
$newRebroadcastInstance->setDbRebroadcast(1);
$newRebroadcastInstance->setDbOriginalShow($show_instance_id);
$newRebroadcastInstance->save();
}
}
self::createRebroadcastInstances($rebroadcasts, $currentUtcTimestamp, $show_id, $show_instance_id, $utcStartDateTime, $duration);
}
}
//for a show with repeat_type == 0,1,2
private static function populateRepeatingShow($p_showRow, $end_timestamp, $interval)
/**
* Creates a 1 or more than 1 show instances (user has stated this show repeats). If the show
* is recorded, it may have multiple rebroadcast dates, and so this function will create
* those as well.
*
* @param array $p_showRow
* A row from cc_show_days table
* @param DateTime $p_dateTime
* DateTime object in UTC time.
* @param string $p_interval
* Period of time between repeating shows
*/
private static function populateRepeatingShow($p_showRow, $p_dateTime, $p_interval)
{
global $CC_DBC;
@ -1030,7 +1043,6 @@ class Application_Model_Show {
}
$utcStartDateTime = Application_Model_DateHelper::ConvertToUtcDateTime($start, $timezone);
$utcStart = $utcStartDateTime->format("Y-m-d H:i:s");
$sql = "SELECT * FROM cc_show_rebroadcast WHERE show_id={$show_id}";
$rebroadcasts = $CC_DBC->GetAll($sql);
@ -1039,13 +1051,15 @@ class Application_Model_Show {
$date = new Application_Model_DateHelper();
$currentUtcTimestamp = $date->getUtcTimestamp();
while(strtotime($utcStart) <= strtotime($end_timestamp) && (strtotime($utcStart) < strtotime($last_show) || is_null($last_show))) {
while($utcStartDateTime->getTimestamp() <= $p_dateTime->getTimestamp() &&
($utcStartDateTime->getTimestamp() < strtotime($last_show) || is_null($last_show))) {
$utcStart = $utcStartDateTime->format("Y-m-d H:i:s");
$sql = "SELECT timestamp '{$utcStart}' + interval '{$duration}'";
$utcEnd = $CC_DBC->GetOne($sql);
$utcEndDateTime = new DateTime($CC_DBC->GetOne($sql), new DateTimeZone("UTC"));
if ($show->hasInstanceOnDate($utcStart)){
$ccShowInstance = $show->getInstanceOnDate($utcStart);
if ($show->hasInstanceOnDate($utcStartDateTime)){
$ccShowInstance = $show->getInstanceOnDate($utcStartDateTime);
$newInstance = false;
} else {
$ccShowInstance = new CcShowInstances();
@ -1057,8 +1071,8 @@ class Application_Model_Show {
*/
if ($newInstance || $ccShowInstance->getDbStarts() > $currentUtcTimestamp){
$ccShowInstance->setDbShowId($show_id);
$ccShowInstance->setDbStarts($utcStart);
$ccShowInstance->setDbEnds($utcEnd);
$ccShowInstance->setDbStarts($utcStartDateTime);
$ccShowInstance->setDbEnds($utcEndDateTime);
$ccShowInstance->setDbRecord($record);
$ccShowInstance->save();
}
@ -1066,39 +1080,48 @@ class Application_Model_Show {
$show_instance_id = $ccShowInstance->getDbId();
$showInstance = new Application_Model_ShowInstance($show_instance_id);
/* If we are updating a show then make sure that the scheduled content within
* the show is updated to the correct time. */
if (!$newInstance){
$showInstance->correctScheduleStartTimes();
}
foreach($rebroadcasts as $rebroadcast) {
self::createRebroadcastInstances($rebroadcasts, $currentUtcTimestamp, $show_id, $show_instance_id, $utcStartDateTime, $duration);
$timeinfo = explode(" ", $utcStart);
$dt = new DateTime($start, new DateTimeZone($timezone));
$dt->add(new DateInterval($p_interval));
$start = $dt->format("Y-m-d H:i:s");
$sql = "SELECT timestamp '{$timeinfo[0]}' + interval '{$rebroadcast["day_offset"]}' + interval '{$rebroadcast["start_time"]}'";
$dt->setTimezone(new DateTimeZone('UTC'));
$utcStartDateTime = $dt;
}
Application_Model_Show::setNextPop($start, $show_id, $day);
}
private static function createRebroadcastInstances($p_rebroadcasts, $p_currentUtcTimestamp, $p_showId, $p_showInstanceId, $p_utcStartDateTime, $p_duration){
global $CC_DBC;
foreach($p_rebroadcasts as $rebroadcast) {
$timeinfo = $p_utcStartDateTime->format("Y-m-d H:i:s");
$sql = "SELECT timestamp '{$timeinfo}' + interval '{$rebroadcast["day_offset"]}' + interval '{$rebroadcast["start_time"]}'";
$rebroadcast_start_time = $CC_DBC->GetOne($sql);
$sql = "SELECT timestamp '{$rebroadcast_start_time}' + interval '{$duration}'";
$sql = "SELECT timestamp '{$rebroadcast_start_time}' + interval '{$p_duration}'";
$rebroadcast_end_time = $CC_DBC->GetOne($sql);
if ($rebroadcast_start_time > $currentUtcTimestamp){
if ($rebroadcast_start_time > $p_currentUtcTimestamp){
$newRebroadcastInstance = new CcShowInstances();
$newRebroadcastInstance->setDbShowId($show_id);
$newRebroadcastInstance->setDbShowId($p_showId);
$newRebroadcastInstance->setDbStarts($rebroadcast_start_time);
$newRebroadcastInstance->setDbEnds($rebroadcast_end_time);
$newRebroadcastInstance->setDbRecord(0);
$newRebroadcastInstance->setDbRebroadcast(1);
$newRebroadcastInstance->setDbOriginalShow($show_instance_id);
$newRebroadcastInstance->setDbOriginalShow($p_showInstanceId);
$newRebroadcastInstance->save();
}
}
$sql = "SELECT timestamp '{$start}' + interval '{$interval}'";
$start = $CC_DBC->GetOne($sql);
$utcStartDateTime = Application_Model_DateHelper::ConvertToUtcDateTime($start, $timezone);
$utcStart = $utcStartDateTime->format("Y-m-d H:i:s");
}
Application_Model_Show::setNextPop($start, $show_id, $day);
}
/**