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