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