2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file src/dcp_time.cc
21 * @brief A representation of time within a DCP.
26 #include <boost/algorithm/string.hpp>
29 #include "exceptions.h"
30 #include "raw_convert.h"
33 using namespace boost;
34 using namespace libdcp;
36 Time::Time (int frame, int frames_per_second, int tcr_)
43 set (double (frame) / frames_per_second, tcr);
46 /** @param s_ Seconds.
47 * @param tcr Timecode rate.
50 Time::set (double seconds, int tcr_)
55 e = int (round ((seconds - s) * tcr));
68 Time::Time (string time, int tcr_)
72 split (b, time, is_any_of (":"));
74 boost::throw_exception (DCPReadError ("unrecognised time specification"));
77 h = raw_convert<int> (b[0]);
78 m = raw_convert<int> (b[1]);
79 s = raw_convert<int> (b[2]);
80 e = raw_convert<int> (b[3]);
84 libdcp::operator== (Time const & a, Time const & b)
86 return (a.h == b.h && a.m == b.m && a.s == b.s && (a.e * b.tcr) == (b.e * a.tcr));
90 libdcp::operator!= (Time const & a, Time const & b)
96 libdcp::operator<= (Time const & a, Time const & b)
98 return a < b || a == b;
102 libdcp::operator< (Time const & a, Time const & b)
116 if ((a.e * b.tcr) != (b.e * a.tcr)) {
117 return (a.e * b.tcr) < (b.e * a.tcr);
124 libdcp::operator> (Time const & a, Time const & b)
138 if ((a.e * b.tcr) != (b.e * a.tcr)) {
139 return (a.e * b.tcr) > (b.e * a.tcr);
146 libdcp::operator>= (Time const & a, Time const & b)
148 return a == b || a > b;
152 libdcp::operator+ (Time a, Time b)
156 /* Make sure we have a common tcr */
157 if (a.tcr != b.tcr) {
160 r.tcr = a.tcr * b.tcr;
189 libdcp::operator- (Time a, Time b)
193 /* Make sure we have a common tcr */
194 if (a.tcr != b.tcr) {
197 r.tcr = a.tcr * b.tcr;
226 libdcp::operator/ (Time a, Time const & b)
228 float const as = a.h * 3600 + a.m * 60 + a.s * float (a.e) / a.tcr;
229 float const bs = b.h * 3600 + b.m * 60 + b.s * float (b.e) / b.tcr;
233 /** @return A string of the form h:m:s:e */
235 Time::to_string () const
238 str << h << ":" << m << ":" << s << ":" << e;
243 Time::to_editable_units (int tcr_) const
245 return (int64_t(e) * float (tcr_ / tcr)) + int64_t(s) * tcr_ + int64_t(m) * 60 * tcr_ + int64_t(h) * 60 * 60 * tcr_;
249 libdcp::operator<< (ostream& s, Time const & t)