2 Copyright (C) 2012-2016 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 "compose.hpp"
42 #include <boost/algorithm/string.hpp>
43 #include <boost/optional.hpp>
49 using namespace boost;
52 Time::Time (int frame, double frames_per_second, int tcr_)
54 set (double (frame) / frames_per_second, tcr_);
57 /** Construct a Time from a number of seconds and a timecode rate.
59 * @param seconds A number of seconds.
60 * @param tcr_ Timecode rate.
62 Time::Time (double seconds, int tcr_)
67 /** Construct a Time with specified timecode rate and using the supplied
70 * @param seconds A number of seconds.
71 * @param tcr_ Timecode rate to use.
74 Time::set (double seconds, int tcr_)
79 e = int (round ((seconds - s) * tcr));
96 /** @param time String of the form
97 * HH:MM:SS:EE for SMPTE
98 * HH:MM:SS:E[E[E]] or HH:MM:SS.s[s[s]] for Interop
99 * where HH are hours, MM minutes, SS seconds, EE editable units and
102 * @param tcr_ Timecode rate if this is a SMPTE time, otherwise empty for an Interop time.
104 Time::Time (string time, optional<int> tcr_)
107 split (b, time, is_any_of (":"));
109 if (b.size() < 3 || b[0].empty() || b[1].empty() || b[0].length() > 2 || b[1].length() > 2) {
110 boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1", time)));
116 /* HH:MM:SS.s[s[s]] */
118 split (bs, b[2], is_any_of ("."));
119 if (bs.size() != 2) {
120 boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1", time)));
123 h = raw_convert<int> (b[0]);
124 m = raw_convert<int> (b[1]);
125 if (bs[0].empty() || bs[0].length() > 2) {
126 boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, bs[0])));
128 s = raw_convert<int> (bs[0]);
129 if (bs[1].empty() || bs[1].length() > 3) {
130 boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, bs[1])));
132 e = raw_convert<int> (bs[1]);
134 } else if (b.size() == 4) {
136 h = raw_convert<int> (b[0]);
137 m = raw_convert<int> (b[1]);
138 if (b[2].empty() || b[2].length() > 2) {
139 boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[2])));
141 s = raw_convert<int> (b[2]);
142 if (b[3].empty() || b[3].length() > 3) {
143 boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[3])));
145 e = raw_convert<int> (b[3]);
148 boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1", time)));
152 /* SMPTE: HH:MM:SS:EE */
153 split (b, time, is_any_of (":"));
155 boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1; does not have 4 parts", time)));
158 h = raw_convert<int> (b[0]);
159 m = raw_convert<int> (b[1]);
160 if (b[2].empty() || b[2].length() > 2) {
161 boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[2])));
163 s = raw_convert<int> (b[2]);
164 if (b[3].empty() || b[3].length() > 2) {
165 boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[3])));
167 e = raw_convert<int> (b[3]);
173 dcp::operator== (Time const & a, Time const & b)
175 return (a.h == b.h && a.m == b.m && a.s == b.s && (a.e * b.tcr) == (b.e * a.tcr));
179 dcp::operator!= (Time const & a, Time const & b)
185 dcp::operator<= (Time const & a, Time const & b)
187 return a < b || a == b;
191 dcp::operator>= (Time const & a, Time const & b)
193 return a > b || a == b;
197 dcp::operator< (Time const & a, Time const & b)
211 return (a.e * b.tcr) < (b.e * a.tcr);
215 dcp::operator> (Time const & a, Time const & b)
229 return (a.e * b.tcr) > (b.e * a.tcr);
233 dcp::operator<< (ostream& s, Time const & t)
235 s << t.h << ":" << t.m << ":" << t.s << "." << t.e;
240 dcp::operator+ (Time a, Time b)
244 /* Make sure we have a common tcr */
245 if (a.tcr != b.tcr) {
248 r.tcr = a.tcr * b.tcr;
277 dcp::operator- (Time a, Time b)
281 /* Make sure we have a common tcr */
282 if (a.tcr != b.tcr) {
285 r.tcr = a.tcr * b.tcr;
314 dcp::operator/ (Time a, Time const & b)
316 int64_t const at = a.h * 3600 + a.m * 60 + a.s * float (a.e) / a.tcr;
317 int64_t const bt = b.h * 3600 + b.m * 60 + b.s * float (b.e) / b.tcr;
318 return float (at) / bt;
321 /** @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) */
323 Time::as_string (Standard standard) const
327 if (standard == SMPTE) {
328 snprintf (buffer, sizeof(buffer), "%02d:%02d:%02d:%02d", h, m, s, e);
330 snprintf (buffer, sizeof(buffer), "%02d:%02d:%02d:%03d", h, m, s, e);
336 /** @param tcr_ Timecode rate with which the return value should be counted.
337 * @return the total number of editable units that this time consists of at the specified timecode rate, rounded up
338 * to the nearest editable unit. For example, as_editable_units (24) returns the total time in frames at 24fps.
341 Time::as_editable_units (int tcr_) const
343 return ceil (int64_t(e) * double (tcr_) / tcr) + int64_t(s) * tcr_ + int64_t(m) * 60 * tcr_ + int64_t(h) * 60 * 60 * tcr_;
346 /** @return the total number of seconds that this time consists of */
348 Time::as_seconds () const
350 return h * 3600 + m * 60 + s + double(e) / tcr;
353 /** @param tcr_ New timecode rate.
354 * @return A new Time which is this time at the spcified new timecode rate.
357 Time::rebase (int tcr_) const
359 return Time (h, m, s, floor (float (e) * tcr_ / tcr), tcr_);