More tests.
[libdcp.git] / src / dcp_time.cc
index 0cc6b264894c00be6c11b257ddbe73084c523f17..4033e5dd5f3a4671b97e797c53e214876deec949 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 */
 
 /** @file  src/dcp_time.cc
- *  @brief A representation of time within a DCP.
+ *  @brief Time class.
  */
 
+#include "raw_convert.h"
+#include "dcp_time.h"
+#include "exceptions.h"
+#include <boost/algorithm/string.hpp>
 #include <iostream>
+#include <vector>
 #include <cmath>
-#include "dcp_time.h"
 
 using namespace std;
-using namespace libdcp;
+using namespace boost;
+using namespace dcp;
 
 Time::Time (int frame, int frames_per_second)
        : h (0)
@@ -34,52 +39,208 @@ Time::Time (int frame, int frames_per_second)
        , s (0)
        , t (0)
 {
-       float sec_float = float (frame) / frames_per_second;
-       t = (int (floor (sec_float * 1000)) % 1000) / 4;
-       s = floor (sec_float);
+       set (double (frame) / frames_per_second);
+}
+
+Time::Time (int64_t ticks)
+{
+       h = ticks / (60 * 60 * 250);
+       ticks -= int64_t (h) * 60 * 60 * 250;
+       m = ticks / (60 * 250);
+       ticks -= int64_t (m) * 60 * 250;
+       s = ticks / 250;
+       ticks -= int64_t (s) * 250;
+       t = ticks;
+}
+
+void
+Time::set (double ss)
+{
+       s = floor (ss);
+       t = int (round (1000 * (ss - s) / 4));
 
-       if (s > 60) {
+       if (s >= 60) {
                m = s / 60;
                s -= m * 60;
        }
 
-       if (m > 60) {
+       if (m >= 60) {
                h = m / 60;
                m -= h * 60;
        }
 }
 
+Time::Time (string time)
+{
+       vector<string> b;
+       split (b, time, is_any_of (":"));
+       if (b.size() != 4) {
+               boost::throw_exception (DCPReadError ("unrecognised time specification"));
+       }
+       
+       h = raw_convert<int> (b[0]);
+       m = raw_convert<int> (b[1]);
+       s = raw_convert<int> (b[2]);
+       t = raw_convert<int> (b[3]);
+}
+
 bool
-libdcp::operator== (Time const & a, Time const & b)
+dcp::operator== (Time const & a, Time const & b)
 {
        return (a.h == b.h && a.m == b.m && a.s == b.s && a.t == b.t);
 }
 
 bool
-libdcp::operator<= (Time const & a, Time const & b)
+dcp::operator!= (Time const & a, Time const & b)
+{
+       return !(a == b);
+}
+
+bool
+dcp::operator<= (Time const & a, Time const & b)
+{
+       return a < b || a == b;
+}
+
+bool
+dcp::operator>= (Time const & a, Time const & b)
+{
+       return a > b || a == b;
+}
+
+bool
+dcp::operator< (Time const & a, Time const & b)
 {
        if (a.h != b.h) {
-               return a.h <= b.h;
+               return a.h < b.h;
        }
 
        if (a.m != b.m) {
-               return a.m <= b.m;
+               return a.m < b.m;
        }
 
        if (a.s != b.s) {
-               return a.s <= b.s;
+               return a.s < b.s;
        }
 
        if (a.t != b.t) {
-               return a.t <= b.t;
+               return a.t < b.t;
+       }
+
+       return true;
+}
+
+bool
+dcp::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)
+dcp::operator<< (ostream& s, Time const & t)
 {
        s << t.h << ":" << t.m << ":" << t.s << "." << t.t;
        return s;
 }
+
+dcp::Time
+dcp::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;
+}
+
+dcp::Time
+dcp::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
+dcp::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;
+}
+
+/** @return A string of the form h:m:s:t */
+string
+Time::to_string () const
+{
+       stringstream str;
+       str << h << ":" << m << ":" << s << ":" << t;
+       return str.str ();
+}
+
+/** @return This time in ticks */
+int64_t
+Time::to_ticks () const
+{
+       return int64_t(t) + int64_t(s) * 250 + int64_t(m) * 60 * 250 + int64_t(h) * 60 * 60 * 250;
+}
+
+double
+Time::to_seconds () const
+{
+       return double (to_ticks ()) / 250;
+}