summaryrefslogtreecommitdiff
path: root/src/dcp_time.cc
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2012-08-22 00:11:18 +0100
committerCarl Hetherington <cth@carlh.net>2012-08-22 00:11:18 +0100
commit9b84debc374f426bb3a00baa82bae5fdd88a018e (patch)
tree71cc6972a48f469cd83dc0b0ee60953645390fd4 /src/dcp_time.cc
parentdf6ed597b720399f02e7b75a7cf448d0956c89a1 (diff)
Some maths operations with Time.
Diffstat (limited to 'src/dcp_time.cc')
-rw-r--r--src/dcp_time.cc127
1 files changed, 127 insertions, 0 deletions
diff --git a/src/dcp_time.cc b/src/dcp_time.cc
index 0cc6b264..bc15c367 100644
--- a/src/dcp_time.cc
+++ b/src/dcp_time.cc
@@ -22,10 +22,15 @@
*/
#include <iostream>
+#include <vector>
+#include <boost/algorithm/string.hpp>
+#include <boost/lexical_cast.hpp>
#include <cmath>
#include "dcp_time.h"
+#include "exceptions.h"
using namespace std;
+using namespace boost;
using namespace libdcp;
Time::Time (int frame, int frames_per_second)
@@ -49,6 +54,20 @@ Time::Time (int frame, int frames_per_second)
}
}
+Time::Time (string time)
+{
+ vector<string> b;
+ split (b, time, is_any_of (":"));
+ if (b.size() != 4) {
+ throw DCPReadError ("unrecognised time specification");
+ }
+
+ h = lexical_cast<int> (b[0]);
+ m = lexical_cast<int> (b[1]);
+ s = lexical_cast<int> (b[2]);
+ t = lexical_cast<int> (b[3]);
+}
+
bool
libdcp::operator== (Time const & a, Time const & b)
{
@@ -77,9 +96,117 @@ libdcp::operator<= (Time const & a, Time const & b)
return true;
}
+bool
+libdcp::operator< (Time const & a, Time const & b)
+{
+ if (a.h != b.h) {
+ return a.h < b.h;
+ }
+
+ if (a.m != b.m) {
+ return a.m < b.m;
+ }
+
+ if (a.s != b.s) {
+ return a.s < b.s;
+ }
+
+ if (a.t != b.t) {
+ return a.t < b.t;
+ }
+
+ return true;
+}
+
+bool
+libdcp::operator> (Time const & a, Time const & b)
+{
+ if (a.h != b.h) {
+ return a.h > b.h;
+ }
+
+ if (a.m != b.m) {
+ return a.m > b.m;
+ }
+
+ if (a.s != b.s) {
+ return a.s > b.s;
+ }
+
+ if (a.t != b.t) {
+ return a.t > b.t;
+ }
+
+ return true;
+}
+
ostream &
libdcp::operator<< (ostream& s, Time const & t)
{
s << t.h << ":" << t.m << ":" << t.s << "." << t.t;
return s;
}
+
+libdcp::Time
+libdcp::operator+ (Time a, Time const & b)
+{
+ Time r;
+
+ r.t = a.t + b.t;
+ if (r.t >= 250) {
+ r.t -= 250;
+ r.s++;
+ }
+
+ r.s += a.s + b.s;
+ if (r.s >= 60) {
+ r.s -= 60;
+ r.m++;
+ }
+
+ r.m += a.m + b.m;
+ if (r.m >= 60) {
+ r.m -= 60;
+ r.h++;
+ }
+
+ r.h += a.h + b.h;
+
+ return r;
+}
+
+libdcp::Time
+libdcp::operator- (Time a, Time const & b)
+{
+ Time r;
+
+ r.t = a.t - b.t;
+ if (r.t < 0) {
+ r.t += 250;
+ r.s--;
+ }
+
+ r.s += (a.s - b.s);
+ if (r.s < 0) {
+ r.s += 60;
+ r.m--;
+ }
+
+ r.m += (a.m - b.m);
+ if (r.m < 0) {
+ r.m += 60;
+ r.h--;
+ }
+
+ r.h += (a.h - b.h);
+
+ return r;
+}
+
+float
+libdcp::operator/ (Time a, Time const & b)
+{
+ int64_t const at = a.h * 3600 * 250 + a.m * 60 * 250 + a.s * 250 + a.t;
+ int64_t const bt = b.h * 3600 * 250 + b.m * 60 * 250 + b.s * 250 + b.t;
+ return float (at) / bt;
+}