Comment / tidy.
[libdcp.git] / src / dcp_time.cc
index bc15c367054ca20183b16d9673c8246013c731d4..5376b9724e08a9f7ddef6907ec7fd05cd4edf0c4 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 <iostream>
-#include <vector>
+#include "dcp_time.h"
+#include "exceptions.h"
 #include <boost/algorithm/string.hpp>
 #include <boost/lexical_cast.hpp>
+#include <iostream>
+#include <vector>
 #include <cmath>
-#include "dcp_time.h"
-#include "exceptions.h"
 
 using namespace std;
 using namespace boost;
-using namespace libdcp;
+using namespace dcp;
 
 Time::Time (int frame, int frames_per_second)
        : h (0)
@@ -39,9 +39,25 @@ 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)
+{
+       t = (int (round (ss * 1000)) % 1000) / 4;
+       s = floor (ss);
 
        if (s > 60) {
                m = s / 60;
@@ -59,7 +75,7 @@ Time::Time (string time)
        vector<string> b;
        split (b, time, is_any_of (":"));
        if (b.size() != 4) {
-               throw DCPReadError ("unrecognised time specification");
+               boost::throw_exception (DCPReadError ("unrecognised time specification"));
        }
        
        h = lexical_cast<int> (b[0]);
@@ -69,13 +85,19 @@ Time::Time (string time)
 }
 
 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)
 {
        if (a.h != b.h) {
                return a.h <= b.h;
@@ -97,7 +119,7 @@ libdcp::operator<= (Time const & a, Time const & b)
 }
 
 bool
-libdcp::operator< (Time const & a, Time const & b)
+dcp::operator< (Time const & a, Time const & b)
 {
        if (a.h != b.h) {
                return a.h < b.h;
@@ -119,7 +141,7 @@ libdcp::operator< (Time const & a, Time const & b)
 }
 
 bool
-libdcp::operator> (Time const & a, Time const & b)
+dcp::operator> (Time const & a, Time const & b)
 {
        if (a.h != b.h) {
                return a.h > b.h;
@@ -141,14 +163,14 @@ libdcp::operator> (Time const & a, Time const & b)
 }
 
 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;
 }
 
-libdcp::Time
-libdcp::operator+ (Time a, Time const & b)
+dcp::Time
+dcp::operator+ (Time a, Time const & b)
 {
        Time r;
 
@@ -175,8 +197,8 @@ libdcp::operator+ (Time a, Time const & b)
        return r;
 }
 
-libdcp::Time
-libdcp::operator- (Time a, Time const & b)
+dcp::Time
+dcp::operator- (Time a, Time const & b)
 {
        Time r;
 
@@ -204,9 +226,26 @@ libdcp::operator- (Time a, Time const & b)
 }
 
 float
-libdcp::operator/ (Time a, Time const & b)
+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;
+}
+