Skip to content

Commit

Permalink
Scheduler::UpdateManuals(): prevent scheduling recordings before the …
Browse files Browse the repository at this point in the history
…given start date

and rewrite the confusing code that calculated the start times.

I thought about adding something to prevent scheduling recordings in the past,
but this function doesn’t have enough information to determine that.

`
if (MythDate::current().toLocalTime().time() > starttime)
{
    offset++;
}
`
The above code would prevent scheduling recordings that should have already
started.  However, the end time may still be in the future, so it should be
scheduled in that case.  Unfortunately, the startdate, starttime, enddate,
and endtime values do not account for starting/ending the recording early/late
(8 hours in either direction from the start and end), so this cannot be
determined from the information the function has.

Therefore, this function will continue to schedule recordings that may be
entirely in the past.
  • Loading branch information
ulmus-scott committed Jun 4, 2024
1 parent b64cda9 commit c277acd
Showing 1 changed file with 31 additions and 37 deletions.
68 changes: 31 additions & 37 deletions mythtv/programs/mythbackend/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3745,74 +3745,72 @@ void Scheduler::UpdateManuals(uint recordid)
while (query.next())
chanidlist.push_back(query.value(0).toUInt());

int progcount = 0;
int skipdays = 1;
bool weekday = false;
int daysoff = 0;
QDateTime lstartdt = startdt.toLocalTime();
std::vector<QDateTime> startList;
constexpr int weeksToSchedule = 2;
constexpr int daysInWeek = 7;
// use local date/time so the local time of the recording stays constant
// across daylight savings time changes and weekday/weekend detection works
// correctly
const QDate startDate = startdt.toLocalTime().date();
const QTime startTime = startdt.toLocalTime().time();
// don't schedule recordings before startDate
qint64 offset =
std::max(qint64{0}, startDate.daysTo(MythDate::current().toLocalTime().date()));

switch (rectype)
{
case kSingleRecord:
case kOverrideRecord:
case kDontRecord:
progcount = 1;
skipdays = 1;
weekday = false;
daysoff = 0;
startList.push_back(startdt);
break;
case kDailyRecord:
progcount = 13;
skipdays = 1;
weekday = (lstartdt.date().dayOfWeek() < 6);
daysoff = lstartdt.date().daysTo(
MythDate::current().toLocalTime().date());
startdt = QDateTime(lstartdt.date().addDays(daysoff),
lstartdt.time(), Qt::LocalTime).toUTC();
for (int i = 0; i < daysInWeek * weeksToSchedule; i++)
{
if (startDate.dayOfWeek() < 6 && startDate.addDays(offset + i).dayOfWeek() >= 6)
{
continue;
}
startList.push_back(QDateTime(startDate.addDays(offset + i), startTime, Qt::LocalTime).toUTC());
}
break;
case kWeeklyRecord:
progcount = 2;
skipdays = 7;
weekday = false;
daysoff = lstartdt.date().daysTo(
MythDate::current().toLocalTime().date());
daysoff = (daysoff + 6) / 7 * 7;
startdt = QDateTime(lstartdt.date().addDays(daysoff),
lstartdt.time(), Qt::LocalTime).toUTC();
// round offset up to a whole number of weeks
offset = (offset + daysInWeek - 1) / daysInWeek * daysInWeek;
for (int i = 0; i < daysInWeek * weeksToSchedule; i += daysInWeek)
{
startList.push_back(QDateTime(startDate.addDays(offset + i), startTime, Qt::LocalTime).toUTC());
}
break;
default:
LOG(VB_GENERAL, LOG_ERR,
QString("Invalid rectype for manual recordid %1").arg(recordid));
return;
}

while (progcount--)
for (const QDateTime& start : startList)
{
if (subtitleWasEmpty)
{
subtitle = MythDate::toString(startdt, MythDate::kDatabase | MythDate::kOverrideLocal);
subtitle = MythDate::toString(start, MythDate::kDatabase | MythDate::kOverrideLocal);
}

if (descriptionWasEmpty)
{
description = startdt.toLocalTime().toString();
description = start.toLocalTime().toString();
}

for (uint id : chanidlist)
{
if (weekday && startdt.toLocalTime().date().dayOfWeek() >= 6)
continue;


query.prepare("REPLACE INTO program (chanid, starttime, endtime,"
" title, subtitle, description, manualid,"
" season, episode, inetref, originalairdate, generic) "
"VALUES (:CHANID, :STARTTIME, :ENDTIME, :TITLE,"
" :SUBTITLE, :DESCRIPTION, :RECORDID, "
" :SEASON, :EPISODE, :INETREF, :ORIGINALAIRDATE, 1)");
query.bindValue(":CHANID", id);
query.bindValue(":STARTTIME", startdt);
query.bindValue(":ENDTIME", startdt.addSecs(duration));
query.bindValue(":STARTTIME", start);
query.bindValue(":ENDTIME", start.addSecs(duration));
query.bindValue(":TITLE", title);
query.bindValue(":SUBTITLE", subtitle);
query.bindValue(":DESCRIPTION", description);
Expand All @@ -3827,10 +3825,6 @@ void Scheduler::UpdateManuals(uint recordid)
return;
}
}

daysoff += skipdays;
startdt = QDateTime(lstartdt.date().addDays(daysoff),
lstartdt.time(), Qt::LocalTime).toUTC();
}
}

Expand Down

0 comments on commit c277acd

Please sign in to comment.