From dc226d8dec948a104f9c6ecb9f08f1efacc4b214 Mon Sep 17 00:00:00 2001 From: randomdan Date: Tue, 4 Mar 2014 23:16:09 +0000 Subject: Incorperate proper datetimes and durations now the core does --- p2pvr/ice/common.ice | 2 +- p2pvr/ice/commonHelpers.cpp | 55 ++++++++++++++-------------------- p2pvr/ice/commonHelpers.h | 12 ++++++-- p2pvr/ice/p2pvr.ice | 6 ++-- p2pvr/lib/recorder.cpp | 6 ++-- p2pvr/lib/sql/Schedules_selectAll.sql | 2 +- p2pvr/lib/sql/Schedules_selectById.sql | 2 +- p2pvr/util/p2Helpers.cpp | 25 +++++++++++----- p2pvr/util/p2Helpers.h | 8 +++++ 9 files changed, 67 insertions(+), 51 deletions(-) diff --git a/p2pvr/ice/common.ice b/p2pvr/ice/common.ice index 8296f93..14ca155 100644 --- a/p2pvr/ice/common.ice +++ b/p2pvr/ice/common.ice @@ -10,7 +10,7 @@ module Common { short Minute; }; - struct TimeOfDay { + struct Duration { short Hour; short Minute; }; diff --git a/p2pvr/ice/commonHelpers.cpp b/p2pvr/ice/commonHelpers.cpp index 64a1c61..46c7fad 100644 --- a/p2pvr/ice/commonHelpers.cpp +++ b/p2pvr/ice/commonHelpers.cpp @@ -1,46 +1,37 @@ #include "commonHelpers.h" #include -#include +#include namespace Common { - std::string operator-(const Common::DateTime & a, const Common::DateTime & b) + boost::posix_time::ptime operator*(const Common::DateTime & dt) { - struct tm tma { - 0, a.Minute, a.Hour, - a.Day, a.Month - 1, a.Year - 1900, - 0, 0, 0, 0, 0}; - struct tm tmb { - 0, b.Minute, b.Hour, - b.Day, b.Month - 1, b.Year - 1900, - 0, 0, 0, 0, 0}; - auto secs = mktime(&tma) - mktime(&tmb); - return stringbf("%02d:%02d:%02d", secs / 3600, (secs / 60) % 60, secs % 60); + return boost::posix_time::ptime( + boost::gregorian::date(dt.Year, dt.Month, dt.Day), + boost::posix_time::time_duration(dt.Hour, dt.Minute, 0)); } - time_t operator-(const Common::DateTime & cdt, const std::string & interval) + boost::posix_time::time_duration operator*(const Common::Duration & d) { - struct tm dt { - 0, cdt.Minute, cdt.Hour, - cdt.Day, cdt.Month - 1, cdt.Year - 1900, - 0, 0, 0, 0, 0}; - unsigned short hours, minutes, seconds; - if (sscanf(interval.c_str(), "%hu:%hu:%hu", &hours, &minutes, &seconds) < 3) { - throw std::runtime_error("Couldn't parse interval"); - } - return mktime(&dt) - (seconds + (60 * (minutes + (60 * hours)))); + return boost::posix_time::time_duration(d.Hour, d.Minute, 0); } +} - time_t operator+(const Common::DateTime & cdt, const std::string & interval) - { - struct tm dt { - 0, cdt.Minute, cdt.Hour, - cdt.Day, cdt.Month - 1, cdt.Year - 1900, - 0, 0, 0, 0, 0}; - unsigned short hours, minutes, seconds; - if (sscanf(interval.c_str(), "%hu:%hu:%hu", &hours, &minutes, &seconds) < 3) { - throw std::runtime_error("Couldn't parse interval"); +namespace boost { + namespace posix_time { + Common::DateTime operator*(const boost::posix_time::ptime & dt) + { + return { + dt.date().year(), dt.date().month(), dt.date().day(), + boost::numeric_cast(dt.time_of_day().hours()), + boost::numeric_cast(dt.time_of_day().minutes()) }; + } + + Common::Duration operator*(const boost::posix_time::time_duration & d) + { + return { + boost::numeric_cast(d.hours()), + boost::numeric_cast(d.minutes()) }; } - return mktime(&dt) + (seconds + (60 * (minutes + (60 * hours)))); } } diff --git a/p2pvr/ice/commonHelpers.h b/p2pvr/ice/commonHelpers.h index 645ad1c..cc3cac1 100644 --- a/p2pvr/ice/commonHelpers.h +++ b/p2pvr/ice/commonHelpers.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace Common { template @@ -17,9 +18,14 @@ namespace Common { << ":" << std::setw(2) << std::setfill('0') << dt.Minute; return o; } - time_t operator-(const Common::DateTime & cdt, const std::string & interval); - time_t operator+(const Common::DateTime & cdt, const std::string & interval); - std::string operator-(const Common::DateTime & a, const Common::DateTime & b); + boost::posix_time::ptime operator*(const Common::DateTime &); + boost::posix_time::time_duration operator*(const Common::Duration &); +} +namespace boost { + namespace posix_time { + Common::DateTime operator*(const boost::posix_time::ptime &); + Common::Duration operator*(const boost::posix_time::time_duration &); + } } #endif diff --git a/p2pvr/ice/p2pvr.ice b/p2pvr/ice/p2pvr.ice index 984f18a..75bfbf8 100644 --- a/p2pvr/ice/p2pvr.ice +++ b/p2pvr/ice/p2pvr.ice @@ -16,7 +16,7 @@ module P2PVR { optional(2) string Subtitle; optional(3) string Description; Common::DateTime StartTime; - string Duration; + Common::Duration Duration; }; sequence RecordingList; @@ -28,8 +28,8 @@ module P2PVR { optional(3) string Title; optional(4) string Search; int Priority; - string Early; - string Late; + Common::Duration Early; + Common::Duration Late; bool Repeats; }; sequence ScheduleList; diff --git a/p2pvr/lib/recorder.cpp b/p2pvr/lib/recorder.cpp index 98c7c4e..bdd18c6 100644 --- a/p2pvr/lib/recorder.cpp +++ b/p2pvr/lib/recorder.cpp @@ -47,7 +47,7 @@ Recorder::RefreshSchedules(const Ice::Current &) auto service = si->GetService(s->ServiceId); auto event = si->GetEvent(s->ServiceId, s->EventId); - auto startIn = std::max(event->StartTime - schedule->Early - time(NULL), 0); + auto startIn = std::max((*event->StartTime - *schedule->Early - boost::posix_time::second_clock::universal_time()).total_seconds(), 0); IceUtil::TimerTaskPtr startTimer = new BindTimerTask(boost::bind(&Recorder::StartRecording, this, schedule, service, event)); timer->schedule(startTimer, IceUtil::Time::seconds(startIn)); pendingRecordings.push_back(startTimer); @@ -75,11 +75,11 @@ Recorder::StartRecording(P2PVR::SchedulePtr schedule, DVBSI::ServicePtr service, service->Name ? *service->Name : "", service->ServiceId); recordings->NewRecording(new P2PVR::Recording(0, storage->ice_toString(), id, schedule->ScheduleId, event->Title, event->Subtitle, - event->Description, event->StartTime, event->StopTime - event->StartTime)); + event->Description, event->StartTime, *(*event->StopTime - *event->StartTime))); auto newCurrent = CurrentPtr(new Current({muxer, store, ss, schedule, service, event, IceUtil::TimerTaskPtr()})); currentRecordings.insert(newCurrent); - auto stopIn = event->StopTime + schedule->Late - time(NULL); + auto stopIn = (*event->StopTime + *schedule->Late - boost::posix_time::second_clock::universal_time()).total_seconds(); newCurrent->stopTimer = new BindTimerTask(boost::bind(&Recorder::StopRecording, this, newCurrent)); timer->schedule(newCurrent->stopTimer, IceUtil::Time::seconds(stopIn)); Logger()->messagebf(LOG_DEBUG, "Recording %s scheduled stop in %s seconds", event->Title, stopIn); diff --git a/p2pvr/lib/sql/Schedules_selectAll.sql b/p2pvr/lib/sql/Schedules_selectAll.sql index 5970dc1..e9e500e 100644 --- a/p2pvr/lib/sql/Schedules_selectAll.sql +++ b/p2pvr/lib/sql/Schedules_selectAll.sql @@ -1,3 +1,3 @@ -SELECT scheduleid, serviceid, eventid, title, search, priority, early::text early, late::text late, repeats +SELECT scheduleid, serviceid, eventid, title, search, priority, early, late, repeats FROM schedules ORDER BY scheduleId diff --git a/p2pvr/lib/sql/Schedules_selectById.sql b/p2pvr/lib/sql/Schedules_selectById.sql index 2eb7b0b..4990418 100644 --- a/p2pvr/lib/sql/Schedules_selectById.sql +++ b/p2pvr/lib/sql/Schedules_selectById.sql @@ -1,4 +1,4 @@ -SELECT scheduleid, serviceid, eventid, title, search, priority, early::text early, late::text late, repeats +SELECT scheduleid, serviceid, eventid, title, search, priority, early, late, repeats FROM schedules WHERE scheduleid = ? ORDER BY scheduleId diff --git a/p2pvr/util/p2Helpers.cpp b/p2pvr/util/p2Helpers.cpp index 081905d..c58ff1a 100644 --- a/p2pvr/util/p2Helpers.cpp +++ b/p2pvr/util/p2Helpers.cpp @@ -1,12 +1,27 @@ #include #include "p2Helpers.h" +#include + +template <> +VariableType & +operator<<(VariableType & vt, const Common::Duration & d) +{ + return (vt = *d); +} template <> VariableType & operator<<(VariableType & vt, const Common::DateTime & dt) { - vt = boost::posix_time::ptime(boost::gregorian::date(dt.Year, dt.Month, dt.Day), - boost::posix_time::time_duration(dt.Hour, dt.Minute, 0)); + return (vt = *dt); +} + +template <> +const VariableType & +operator>>(const VariableType & vt, Common::Duration & d) +{ + const boost::posix_time::time_duration & dur = vt; + d = *dur; return vt; } @@ -15,11 +30,7 @@ const VariableType & operator>>(const VariableType & vt, Common::DateTime & dt) { const boost::posix_time::ptime & date = vt; - dt.Year = date.date().year(); - dt.Month = date.date().month(); - dt.Day = date.date().day(); - dt.Hour = date.time_of_day().hours(); - dt.Minute = date.time_of_day().minutes(); + dt = *date; return vt; } diff --git a/p2pvr/util/p2Helpers.h b/p2pvr/util/p2Helpers.h index f01be78..3b73f1b 100644 --- a/p2pvr/util/p2Helpers.h +++ b/p2pvr/util/p2Helpers.h @@ -12,6 +12,10 @@ operator>>(const VariableType & vt, T & v) return vt; } +template <> +const VariableType & +operator>>(const VariableType & vt, Common::Duration & d); + template <> const VariableType & operator>>(const VariableType & vt, Common::DateTime & dt); @@ -42,6 +46,10 @@ operator<<(VariableType & vt, const T & v) return vt; } +template <> +VariableType & +operator<<(VariableType & vt, const Common::Duration & d); + template <> VariableType & operator<<(VariableType & vt, const Common::DateTime & dt); -- cgit v1.2.3