Merge branch '1.0' of ssh://main.carlh.net/home/carl/git/libdcp into 1.0
[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 Time::Time (double seconds)
42 {
43         set (seconds, 24);
44 }
45
46 void
47 Time::set (double seconds, int tcr_)
48 {
49         s = floor (seconds);
50         tcr = tcr_;
51         
52         e = int (round ((seconds - s) * tcr));
53
54         if (s >= 60) {
55                 m = s / 60;
56                 s -= m * 60;
57         } else {
58                 m = 0;
59         }
60
61         if (m >= 60) {
62                 h = m / 60;
63                 m -= h * 60;
64         } else {
65                 h = 0;
66         }
67 }
68
69 Time::Time (string time, int tcr_)
70         : tcr (tcr_)
71 {
72         vector<string> b;
73         split (b, time, is_any_of (":"));
74         if (b.size() != 4) {
75                 boost::throw_exception (DCPReadError ("unrecognised time specification"));
76         }
77         
78         h = raw_convert<int> (b[0]);
79         m = raw_convert<int> (b[1]);
80         s = raw_convert<int> (b[2]);
81         e = raw_convert<int> (b[3]);
82 }
83
84 bool
85 dcp::operator== (Time const & a, Time const & b)
86 {
87         return (a.h == b.h && a.m == b.m && a.s == b.s && (a.e * b.tcr) == (b.e * a.tcr));
88 }
89
90 bool
91 dcp::operator!= (Time const & a, Time const & b)
92 {
93         return !(a == b);
94 }
95
96 bool
97 dcp::operator<= (Time const & a, Time const & b)
98 {
99         return a < b || a == b;
100 }
101
102 bool
103 dcp::operator>= (Time const & a, Time const & b)
104 {
105         return a > b || a == b;
106 }
107
108 bool
109 dcp::operator< (Time const & a, Time const & b)
110 {
111         if (a.h != b.h) {
112                 return a.h < b.h;
113         }
114
115         if (a.m != b.m) {
116                 return a.m < b.m;
117         }
118
119         if (a.s != b.s) {
120                 return a.s < b.s;
121         }
122
123         if ((a.e * b.tcr) != (b.e * a.tcr)) {
124                 return (a.e * b.tcr) < (b.e * a.tcr);
125         }
126
127         return true;
128 }
129
130 bool
131 dcp::operator> (Time const & a, Time const & b)
132 {
133         if (a.h != b.h) {
134                 return a.h > b.h;
135         }
136
137         if (a.m != b.m) {
138                 return a.m > b.m;
139         }
140
141         if (a.s != b.s) {
142                 return a.s > b.s;
143         }
144
145         if ((a.e * b.tcr) != (b.e * a.tcr)) {
146                 return (a.e * b.tcr) > (b.e * a.tcr);
147         }
148
149         return true;
150 }
151
152 ostream &
153 dcp::operator<< (ostream& s, Time const & t)
154 {
155         s << t.h << ":" << t.m << ":" << t.s << "." << t.e;
156         return s;
157 }
158
159 dcp::Time
160 dcp::operator+ (Time a, Time b)
161 {
162         Time r;
163
164         /* Make sure we have a common tcr */
165         if (a.tcr != b.tcr) {
166                 a.e *= b.tcr;
167                 b.e *= a.tcr;
168                 r.tcr = a.tcr * b.tcr;
169         } else {
170                 r.tcr = a.tcr;
171         }
172
173         r.e = a.e + b.e;
174         if (r.e >= r.tcr) {
175                 r.e -= r.tcr;
176                 r.s++;
177         }
178
179         r.s += a.s + b.s;
180         if (r.s >= 60) {
181                 r.s -= 60;
182                 r.m++;
183         }
184
185         r.m += a.m + b.m;
186         if (r.m >= 60) {
187                 r.m -= 60;
188                 r.h++;
189         }
190
191         r.h += a.h + b.h;
192
193         return r;
194 }
195
196 dcp::Time
197 dcp::operator- (Time a, Time b)
198 {
199         Time r;
200
201         /* Make sure we have a common tcr */
202         if (a.tcr != b.tcr) {
203                 a.e *= b.tcr;
204                 b.e *= a.tcr;
205                 r.tcr = a.tcr * b.tcr;
206         } else {
207                 r.tcr = a.tcr;
208         }
209         
210         r.e = a.e - b.e;
211         if (r.e < 0) {
212                 r.e += r.tcr;
213                 r.s--;
214         }
215
216         r.s += (a.s - b.s);
217         if (r.s < 0) {
218                 r.s += 60;
219                 r.m--;
220         }
221
222         r.m += (a.m - b.m);
223         if (r.m < 0) {
224                 r.m += 60;
225                 r.h--;
226         }
227
228         r.h += (a.h - b.h);
229
230         return r;
231 }
232
233 float
234 dcp::operator/ (Time a, Time const & b)
235 {
236         int64_t const at = a.h * 3600 + a.m * 60 + a.s * float (a.e) / a.tcr;
237         int64_t const bt = b.h * 3600 + b.m * 60 + b.s * float (b.e) / b.tcr;
238         return float (at) / bt;
239 }
240
241 /** @return A string of the form h:m:s:e padded as in 00:00:00:000 */
242 string
243 Time::to_string () const
244 {
245         stringstream str;
246         str << setw(2) << setfill('0') << h << ":"
247             << setw(2) << setfill('0') << m << ":"
248             << setw(2) << setfill('0') << s << ":"
249             << setw(3) << setfill('0') << e;
250         return str.str ();
251 }
252
253 int64_t
254 Time::to_editable_units (int tcr_) const
255 {
256         return (int64_t(e) * float (tcr_ / tcr)) + int64_t(s) * tcr_ + int64_t(m) * 60 * tcr_ + int64_t(h) * 60 * 60 * tcr_;
257 }
258
259 double
260 Time::to_seconds () const
261 {
262         return h * 3600 + m * 60 + s + double(e) / tcr;
263 }
264
265 Time
266 Time::rebase (int tcr_) const
267 {
268         return Time (h, m, s, rint (float (e) * tcr_ / tcr), tcr_);
269 }