Skip to content

Commit c277acd

Browse files
committed
Scheduler::UpdateManuals(): prevent scheduling recordings before the 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.
1 parent b64cda9 commit c277acd

File tree

1 file changed

+31
-37
lines changed

1 file changed

+31
-37
lines changed

mythtv/programs/mythbackend/scheduler.cpp

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3745,74 +3745,72 @@ void Scheduler::UpdateManuals(uint recordid)
37453745
while (query.next())
37463746
chanidlist.push_back(query.value(0).toUInt());
37473747

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

37543760
switch (rectype)
37553761
{
37563762
case kSingleRecord:
37573763
case kOverrideRecord:
37583764
case kDontRecord:
3759-
progcount = 1;
3760-
skipdays = 1;
3761-
weekday = false;
3762-
daysoff = 0;
3765+
startList.push_back(startdt);
37633766
break;
37643767
case kDailyRecord:
3765-
progcount = 13;
3766-
skipdays = 1;
3767-
weekday = (lstartdt.date().dayOfWeek() < 6);
3768-
daysoff = lstartdt.date().daysTo(
3769-
MythDate::current().toLocalTime().date());
3770-
startdt = QDateTime(lstartdt.date().addDays(daysoff),
3771-
lstartdt.time(), Qt::LocalTime).toUTC();
3768+
for (int i = 0; i < daysInWeek * weeksToSchedule; i++)
3769+
{
3770+
if (startDate.dayOfWeek() < 6 && startDate.addDays(offset + i).dayOfWeek() >= 6)
3771+
{
3772+
continue;
3773+
}
3774+
startList.push_back(QDateTime(startDate.addDays(offset + i), startTime, Qt::LocalTime).toUTC());
3775+
}
37723776
break;
37733777
case kWeeklyRecord:
3774-
progcount = 2;
3775-
skipdays = 7;
3776-
weekday = false;
3777-
daysoff = lstartdt.date().daysTo(
3778-
MythDate::current().toLocalTime().date());
3779-
daysoff = (daysoff + 6) / 7 * 7;
3780-
startdt = QDateTime(lstartdt.date().addDays(daysoff),
3781-
lstartdt.time(), Qt::LocalTime).toUTC();
3778+
// round offset up to a whole number of weeks
3779+
offset = (offset + daysInWeek - 1) / daysInWeek * daysInWeek;
3780+
for (int i = 0; i < daysInWeek * weeksToSchedule; i += daysInWeek)
3781+
{
3782+
startList.push_back(QDateTime(startDate.addDays(offset + i), startTime, Qt::LocalTime).toUTC());
3783+
}
37823784
break;
37833785
default:
37843786
LOG(VB_GENERAL, LOG_ERR,
37853787
QString("Invalid rectype for manual recordid %1").arg(recordid));
37863788
return;
37873789
}
37883790

3789-
while (progcount--)
3791+
for (const QDateTime& start : startList)
37903792
{
37913793
if (subtitleWasEmpty)
37923794
{
3793-
subtitle = MythDate::toString(startdt, MythDate::kDatabase | MythDate::kOverrideLocal);
3795+
subtitle = MythDate::toString(start, MythDate::kDatabase | MythDate::kOverrideLocal);
37943796
}
37953797

37963798
if (descriptionWasEmpty)
37973799
{
3798-
description = startdt.toLocalTime().toString();
3800+
description = start.toLocalTime().toString();
37993801
}
38003802

38013803
for (uint id : chanidlist)
38023804
{
3803-
if (weekday && startdt.toLocalTime().date().dayOfWeek() >= 6)
3804-
continue;
3805-
3806-
38073805
query.prepare("REPLACE INTO program (chanid, starttime, endtime,"
38083806
" title, subtitle, description, manualid,"
38093807
" season, episode, inetref, originalairdate, generic) "
38103808
"VALUES (:CHANID, :STARTTIME, :ENDTIME, :TITLE,"
38113809
" :SUBTITLE, :DESCRIPTION, :RECORDID, "
38123810
" :SEASON, :EPISODE, :INETREF, :ORIGINALAIRDATE, 1)");
38133811
query.bindValue(":CHANID", id);
3814-
query.bindValue(":STARTTIME", startdt);
3815-
query.bindValue(":ENDTIME", startdt.addSecs(duration));
3812+
query.bindValue(":STARTTIME", start);
3813+
query.bindValue(":ENDTIME", start.addSecs(duration));
38163814
query.bindValue(":TITLE", title);
38173815
query.bindValue(":SUBTITLE", subtitle);
38183816
query.bindValue(":DESCRIPTION", description);
@@ -3827,10 +3825,6 @@ void Scheduler::UpdateManuals(uint recordid)
38273825
return;
38283826
}
38293827
}
3830-
3831-
daysoff += skipdays;
3832-
startdt = QDateTime(lstartdt.date().addDays(daysoff),
3833-
lstartdt.time(), Qt::LocalTime).toUTC();
38343828
}
38353829
}
38363830

0 commit comments

Comments
 (0)