X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fdcpomatic_time.h;h=8b2bcce05d0f10cb61d8ece42df965187923ff7c;hb=d0ecbc635fa3931fe6a588c3ffb038d1f3e1b11c;hp=16d93ca28bc83abc01c8273c6ece6cd092b42359;hpb=c8be0644833dce6ff39202430bba0ab358f3e096;p=dcpomatic.git diff --git a/src/lib/dcpomatic_time.h b/src/lib/dcpomatic_time.h index 16d93ca28..8b2bcce05 100644 --- a/src/lib/dcpomatic_time.h +++ b/src/lib/dcpomatic_time.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2014-2016 Carl Hetherington + Copyright (C) 2014-2017 Carl Hetherington This file is part of DCP-o-matic. @@ -28,6 +28,7 @@ #include "frame_rate_change.h" #include "dcpomatic_assert.h" #include +#include #include #include #include @@ -157,7 +158,13 @@ public: return ::ceil (_t * double(r) / HZ); } - /** @param r Frames per second */ + /** Split a time into hours, minutes, seconds and frames. + * @param r Frames per second. + * @param h Returned hours. + * @param m Returned minutes. + * @param s Returned seconds. + * @param f Returned frames. + */ template void split (T r, int& h, int& m, int& s, int& f) const { @@ -260,7 +267,7 @@ public: return TimePeriod (from + o, to + o); } - boost::optional > overlap (TimePeriod const & other) { + boost::optional > overlap (TimePeriod const & other) const { T const max_from = std::max (from, other.from); T const min_to = std::min (to, other.to); @@ -291,6 +298,47 @@ public: } }; +/** @param A Period which is subtracted from. + * @param B Periods to subtract from `A', must be in ascending order of start time and must not overlap. + */ +template +std::list > subtract (TimePeriod A, std::list > const & B) +{ + std::list > result; + result.push_back (A); + + BOOST_FOREACH (TimePeriod i, B) { + std::list > new_result; + BOOST_FOREACH (TimePeriod j, result) { + boost::optional > ov = i.overlap (j); + if (ov) { + if (*ov == i) { + /* A contains all of B */ + if (i.from != j.from) { + new_result.push_back (TimePeriod (j.from, i.from)); + } + if (i.to != j.to) { + new_result.push_back (TimePeriod (i.to, j.to)); + } + } else if (*ov == j) { + /* B contains all of A */ + } else if (i.from < j.from) { + /* B overlaps start of A */ + new_result.push_back (TimePeriod (i.to, j.to)); + } else if (i.to > j.to) { + /* B overlaps end of A */ + new_result.push_back (TimePeriod (j.from, i.from)); + } + } else { + new_result.push_back (j); + } + } + result = new_result; + } + + return result; +} + typedef TimePeriod ContentTimePeriod; typedef TimePeriod DCPTimePeriod;