No-op; Fix GPL address and mention libdcp by name.
[libdcp.git] / src / dcp_time.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
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.
10
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.
15
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/>.
18
19 */
20
21 /** @file  src/dcp_time.cc
22  *  @brief Time class.
23  */
24
25 #include "raw_convert.h"
26 #include "dcp_time.h"
27 #include "exceptions.h"
28 #include <boost/algorithm/string.hpp>
29 #include <iostream>
30 #include <vector>
31 #include <cmath>
32
33 using namespace std;
34 using namespace boost;
35 using namespace dcp;
36
37 Time::Time (int frame, double frames_per_second, int tcr_)
38 {
39         set (double (frame) / frames_per_second, tcr_);
40 }
41
42 /** Construct a Time from a number of seconds and a timecode rate.
43  *
44  *  @param seconds A number of seconds.
45  *  @param tcr_ Timecode rate.
46  */
47 Time::Time (double seconds, int tcr_)
48 {
49         set (seconds, tcr_);
50 }
51
52 /** Construct a Time with specified timecode rate and using the supplied
53  *  number of seconds.
54  *
55  *  @param seconds A number of seconds.
56  *  @param tcr_ Timecode rate to use.
57  */
58 void
59 Time::set (double seconds, int tcr_)
60 {
61         s = floor (seconds);
62         tcr = tcr_;
63
64         e = int (round ((seconds - s) * tcr));
65
66         if (s >= 60) {
67                 m = s / 60;
68                 s -= m * 60;
69         } else {
70                 m = 0;
71         }
72
73         if (m >= 60) {
74                 h = m / 60;
75                 m -= h * 60;
76         } else {
77                 h = 0;
78         }
79 }
80
81 /** @param time String of the form HH:MM:SS:EE[E] */
82 Time::Time (string time, int tcr_)
83         : tcr (tcr_)
84 {
85         vector<string> b;
86         split (b, time, is_any_of (":"));
87         if (b.size() != 4) {
88                 boost::throw_exception (DCPReadError ("unrecognised time specification"));
89         }
90
91         h = raw_convert<int> (b[0]);
92         m = raw_convert<int> (b[1]);
93         s = raw_convert<int> (b[2]);
94         e = raw_convert<int> (b[3]);
95 }
96
97 bool
98 dcp::operator== (Time const & a, Time const & b)
99 {
100         return (a.h == b.h && a.m == b.m && a.s == b.s && (a.e * b.tcr) == (b.e * a.tcr));
101 }
102
103 bool
104 dcp::operator!= (Time const & a, Time const & b)
105 {
106         return !(a == b);
107 }
108
109 bool
110 dcp::operator<= (Time const & a, Time const & b)
111 {
112         return a < b || a == b;
113 }
114
115 bool
116 dcp::operator>= (Time const & a, Time const & b)
117 {
118         return a > b || a == b;
119 }
120
121 bool
122 dcp::operator< (Time const & a, Time const & b)
123 {
124         if (a.h != b.h) {
125                 return a.h < b.h;
126         }
127
128         if (a.m != b.m) {
129                 return a.m < b.m;
130         }
131
132         if (a.s != b.s) {
133                 return a.s < b.s;
134         }
135
136         if ((a.e * b.tcr) != (b.e * a.tcr)) {
137                 return (a.e * b.tcr) < (b.e * a.tcr);
138         }
139
140         return true;
141 }
142
143 bool
144 dcp::operator> (Time const & a, Time const & b)
145 {
146         if (a.h != b.h) {
147                 return a.h > b.h;
148         }
149
150         if (a.m != b.m) {
151                 return a.m > b.m;
152         }
153
154         if (a.s != b.s) {
155                 return a.s > b.s;
156         }
157
158         if ((a.e * b.tcr) != (b.e * a.tcr)) {
159                 return (a.e * b.tcr) > (b.e * a.tcr);
160         }
161
162         return true;
163 }
164
165 ostream &
166 dcp::operator<< (ostream& s, Time const & t)
167 {
168         s << t.h << ":" << t.m << ":" << t.s << "." << t.e;
169         return s;
170 }
171
172 dcp::Time
173 dcp::operator+ (Time a, Time b)
174 {
175         Time r;
176
177         /* Make sure we have a common tcr */
178         if (a.tcr != b.tcr) {
179                 a.e *= b.tcr;
180                 b.e *= a.tcr;
181                 r.tcr = a.tcr * b.tcr;
182         } else {
183                 r.tcr = a.tcr;
184         }
185
186         r.e = a.e + b.e;
187         if (r.e >= r.tcr) {
188                 r.e -= r.tcr;
189                 r.s++;
190         }
191
192         r.s += a.s + b.s;
193         if (r.s >= 60) {
194                 r.s -= 60;
195                 r.m++;
196         }
197
198         r.m += a.m + b.m;
199         if (r.m >= 60) {
200                 r.m -= 60;
201                 r.h++;
202         }
203
204         r.h += a.h + b.h;
205
206         return r;
207 }
208
209 dcp::Time
210 dcp::operator- (Time a, Time b)
211 {
212         Time r;
213
214         /* Make sure we have a common tcr */
215         if (a.tcr != b.tcr) {
216                 a.e *= b.tcr;
217                 b.e *= a.tcr;
218                 r.tcr = a.tcr * b.tcr;
219         } else {
220                 r.tcr = a.tcr;
221         }
222
223         r.e = a.e - b.e;
224         if (r.e < 0) {
225                 r.e += r.tcr;
226                 r.s--;
227         }
228
229         r.s += (a.s - b.s);
230         if (r.s < 0) {
231                 r.s += 60;
232                 r.m--;
233         }
234
235         r.m += (a.m - b.m);
236         if (r.m < 0) {
237                 r.m += 60;
238                 r.h--;
239         }
240
241         r.h += (a.h - b.h);
242
243         return r;
244 }
245
246 float
247 dcp::operator/ (Time a, Time const & b)
248 {
249         int64_t const at = a.h * 3600 + a.m * 60 + a.s * float (a.e) / a.tcr;
250         int64_t const bt = b.h * 3600 + b.m * 60 + b.s * float (b.e) / b.tcr;
251         return float (at) / bt;
252 }
253
254 /** @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) */
255 string
256 Time::as_string (Standard standard) const
257 {
258         stringstream str;
259         str << setw(2) << setfill('0') << h << ":"
260             << setw(2) << setfill('0') << m << ":"
261             << setw(2) << setfill('0') << s << ":";
262
263         if (standard == SMPTE) {
264                 str << setw(2) << setfill('0') << e;
265         } else {
266                 str << setw(3) << setfill('0') << e;
267         }
268
269         return str.str ();
270 }
271
272 /** @param tcr_ Timecode rate with which the return value should be counted.
273  *  @return the total number of editable units that this time consists of at the specified timecode rate, rounded up
274  *  to the nearest editable unit. For example, as_editable_units (24) returns the total time in frames at 24fps.
275  */
276 int64_t
277 Time::as_editable_units (int tcr_) const
278 {
279         return ceil (int64_t(e) * double (tcr_) / tcr) + int64_t(s) * tcr_ + int64_t(m) * 60 * tcr_ + int64_t(h) * 60 * 60 * tcr_;
280 }
281
282 /** @return the total number of seconds that this time consists of */
283 double
284 Time::as_seconds () const
285 {
286         return h * 3600 + m * 60 + s + double(e) / tcr;
287 }
288
289 /** @param tcr_ New timecode rate.
290  *  @return A new Time which is this time at the spcified new timecode rate.
291  */
292 Time
293 Time::rebase (int tcr_) const
294 {
295         return Time (h, m, s, floor (float (e) * tcr_ / tcr), tcr_);
296 }