From c277acdba3da1167834e4283ed3eaa93f2e6c978 Mon Sep 17 00:00:00 2001 From: Scott Theisen Date: Tue, 4 Jun 2024 02:04:23 -0400 Subject: [PATCH] Scheduler::UpdateManuals(): prevent scheduling recordings before the given start date MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- mythtv/programs/mythbackend/scheduler.cpp | 68 +++++++++++------------ 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/mythtv/programs/mythbackend/scheduler.cpp b/mythtv/programs/mythbackend/scheduler.cpp index 4da87fd5794..69cf223c524 100644 --- a/mythtv/programs/mythbackend/scheduler.cpp +++ b/mythtv/programs/mythbackend/scheduler.cpp @@ -3745,40 +3745,42 @@ 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 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, @@ -3786,24 +3788,20 @@ void Scheduler::UpdateManuals(uint 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) " @@ -3811,8 +3809,8 @@ void Scheduler::UpdateManuals(uint recordid) " :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); @@ -3827,10 +3825,6 @@ void Scheduler::UpdateManuals(uint recordid) return; } } - - daysoff += skipdays; - startdt = QDateTime(lstartdt.date().addDays(daysoff), - lstartdt.time(), Qt::LocalTime).toUTC(); } }