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