More tests.
[libdcp.git] / src / dcp_time.cc
index 5376b9724e08a9f7ddef6907ec7fd05cd4edf0c4..4033e5dd5f3a4671b97e797c53e214876deec949 100644 (file)
  *  @brief Time class.
  */
 
+#include "raw_convert.h"
 #include "dcp_time.h"
 #include "exceptions.h"
 #include <boost/algorithm/string.hpp>
-#include <boost/lexical_cast.hpp>
 #include <iostream>
 #include <vector>
 #include <cmath>
@@ -56,15 +56,15 @@ Time::Time (int64_t ticks)
 void
 Time::set (double ss)
 {
-       t = (int (round (ss * 1000)) % 1000) / 4;
        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;
        }
@@ -78,10 +78,10 @@ Time::Time (string time)
                boost::throw_exception (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]);
+       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
@@ -99,23 +99,13 @@ dcp::operator!= (Time const & a, Time const & b)
 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 a < b || a == b;
+}
 
-       return true;
+bool
+dcp::operator>= (Time const & a, Time const & b)
+{
+       return a > b || a == b;
 }
 
 bool
@@ -249,3 +239,8 @@ 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;
+}