rebase() would round up and so it was possible for ticks to go out of range.
authorCarl Hetherington <cth@carlh.net>
Wed, 2 May 2018 21:16:03 +0000 (22:16 +0100)
committerCarl Hetherington <cth@carlh.net>
Wed, 2 May 2018 21:16:03 +0000 (22:16 +0100)
Now it still rounds up but then copes with overflow.

src/dcp_time.cc
test/dcp_time_test.cc

index f0b7e231c6935669d6942467f75b6c25380e06bb..acc9723f99045a242822e1d94cde67c2c6d36334 100644 (file)
@@ -39,6 +39,7 @@
 #include "dcp_time.h"
 #include "exceptions.h"
 #include "compose.hpp"
+#include "dcp_assert.h"
 #include <boost/algorithm/string.hpp>
 #include <boost/optional.hpp>
 #include <iostream>
@@ -356,5 +357,22 @@ Time::as_seconds () const
 Time
 Time::rebase (int tcr_) const
 {
-       return Time (h, m, s, lrintf (float (e) * tcr_ / tcr), tcr_);
+       long int e_ = lrintf (float (e) * tcr_ / tcr);
+       int s_ = s;
+       if (e_ >= tcr_) {
+               e_ -= tcr_;
+               ++s_;
+       }
+       int m_ = m;
+       if (s_ >= 60) {
+               s_ -= 60;
+               ++m_;
+       }
+       int h_ = h;
+       if (m_ >= 60) {
+               m_ -= 60;
+               ++h_;
+       }
+
+       return Time (h_, m_, s_, e_, tcr_);
 }
index 48a35b5a3c079185673a61dd21a21aefd935b65e..405640f89b60266d54197722f768edb9693b4690 100644 (file)
@@ -102,7 +102,9 @@ BOOST_AUTO_TEST_CASE (dcp_time)
        b = dcp::Time (9, 12, 41, 17, 99);
        BOOST_CHECK_EQUAL (b.rebase(250), dcp::Time(9, 12, 41, 43, 250));
        a = dcp::Time (0, 2, 57, 999, 1000);
-       BOOST_CHECK_EQUAL (a.rebase(250), dcp::Time(0, 2, 57, 250, 250));
+       BOOST_CHECK_EQUAL (a.rebase(250), dcp::Time(0, 2, 58, 0, 250));
+       a = dcp::Time (0, 47, 9, 998, 1000);
+       BOOST_CHECK_EQUAL (a.rebase(250), dcp::Time(0, 47, 10, 0, 250));
 
        /* Check some allowed constructions from string */