2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 libdcp is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libdcp. If not, see <http://www.gnu.org/licenses/>.
19 In addition, as a special exception, the copyright holders give
20 permission to link the code of portions of this program with the
21 OpenSSL library under certain conditions as described in each
22 individual source file, and distribute linked combinations
25 You must obey the GNU General Public License in all respects
26 for all of the code used other than OpenSSL. If you modify
27 file(s) with this exception, you may extend this exception to your
28 version of the file(s), but you are not obligated to do so. If you
29 do not wish to do so, delete this exception statement from your
30 version. If you delete this exception statement from all source
31 files in the program, then also delete it here.
34 /** @file src/dcp_time.cc
38 #include "raw_convert.h"
40 #include "exceptions.h"
41 #include <boost/algorithm/string.hpp>
47 using namespace boost;
50 Time::Time (int frame, double frames_per_second, int tcr_)
52 set (double (frame) / frames_per_second, tcr_);
55 /** Construct a Time from a number of seconds and a timecode rate.
57 * @param seconds A number of seconds.
58 * @param tcr_ Timecode rate.
60 Time::Time (double seconds, int tcr_)
65 /** Construct a Time with specified timecode rate and using the supplied
68 * @param seconds A number of seconds.
69 * @param tcr_ Timecode rate to use.
72 Time::set (double seconds, int tcr_)
77 e = int (round ((seconds - s) * tcr));
94 /** @param time String of the form HH:MM:SS:EE[E] */
95 Time::Time (string time, int tcr_)
99 split (b, time, is_any_of (":"));
101 boost::throw_exception (DCPReadError ("unrecognised time specification"));
104 h = raw_convert<int> (b[0]);
105 m = raw_convert<int> (b[1]);
106 s = raw_convert<int> (b[2]);
107 e = raw_convert<int> (b[3]);
111 dcp::operator== (Time const & a, Time const & b)
113 return (a.h == b.h && a.m == b.m && a.s == b.s && (a.e * b.tcr) == (b.e * a.tcr));
117 dcp::operator!= (Time const & a, Time const & b)
123 dcp::operator<= (Time const & a, Time const & b)
125 return a < b || a == b;
129 dcp::operator>= (Time const & a, Time const & b)
131 return a > b || a == b;
135 dcp::operator< (Time const & a, Time const & b)
149 if ((a.e * b.tcr) != (b.e * a.tcr)) {
150 return (a.e * b.tcr) < (b.e * a.tcr);
157 dcp::operator> (Time const & a, Time const & b)
171 if ((a.e * b.tcr) != (b.e * a.tcr)) {
172 return (a.e * b.tcr) > (b.e * a.tcr);
179 dcp::operator<< (ostream& s, Time const & t)
181 s << t.h << ":" << t.m << ":" << t.s << "." << t.e;
186 dcp::operator+ (Time a, Time b)
190 /* Make sure we have a common tcr */
191 if (a.tcr != b.tcr) {
194 r.tcr = a.tcr * b.tcr;
223 dcp::operator- (Time a, Time b)
227 /* Make sure we have a common tcr */
228 if (a.tcr != b.tcr) {
231 r.tcr = a.tcr * b.tcr;
260 dcp::operator/ (Time a, Time const & b)
262 int64_t const at = a.h * 3600 + a.m * 60 + a.s * float (a.e) / a.tcr;
263 int64_t const bt = b.h * 3600 + b.m * 60 + b.s * float (b.e) / b.tcr;
264 return float (at) / bt;
267 /** @return A string of the form h:m:s:e padded as in 00:00:00:000 (for Interop) or 00:00:00:00 (for SMPTE) */
269 Time::as_string (Standard standard) const
272 str << setw(2) << setfill('0') << h << ":"
273 << setw(2) << setfill('0') << m << ":"
274 << setw(2) << setfill('0') << s << ":";
276 if (standard == SMPTE) {
277 str << setw(2) << setfill('0') << e;
279 str << setw(3) << setfill('0') << e;
285 /** @param tcr_ Timecode rate with which the return value should be counted.
286 * @return the total number of editable units that this time consists of at the specified timecode rate, rounded up
287 * to the nearest editable unit. For example, as_editable_units (24) returns the total time in frames at 24fps.
290 Time::as_editable_units (int tcr_) const
292 return ceil (int64_t(e) * double (tcr_) / tcr) + int64_t(s) * tcr_ + int64_t(m) * 60 * tcr_ + int64_t(h) * 60 * 60 * tcr_;
295 /** @return the total number of seconds that this time consists of */
297 Time::as_seconds () const
299 return h * 3600 + m * 60 + s + double(e) / tcr;
302 /** @param tcr_ New timecode rate.
303 * @return A new Time which is this time at the spcified new timecode rate.
306 Time::rebase (int tcr_) const
308 return Time (h, m, s, floor (float (e) * tcr_ / tcr), tcr_);