Merge master.
[libdcp.git] / src / dcp_time.cc
1 /*
2     Copyright (C) 2012-2014 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 "dcp_time.h"
25 #include "exceptions.h"
26 #include <boost/algorithm/string.hpp>
27 #include <boost/lexical_cast.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)
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
56 void
57 Time::set (double ss)
58 {
59         t = (int (round (ss * 1000)) % 1000) / 4;
60         s = floor (ss);
61
62         if (s > 60) {
63                 m = s / 60;
64                 s -= m * 60;
65         }
66
67         if (m > 60) {
68                 h = m / 60;
69                 m -= h * 60;
70         }
71 }
72
73 Time::Time (string time)
74 {
75         vector<string> b;
76         split (b, time, is_any_of (":"));
77         if (b.size() != 4) {
78                 boost::throw_exception (DCPReadError ("unrecognised time specification"));
79         }
80         
81         h = lexical_cast<int> (b[0]);
82         m = lexical_cast<int> (b[1]);
83         s = lexical_cast<int> (b[2]);
84         t = lexical_cast<int> (b[3]);
85 }
86
87 bool
88 dcp::operator== (Time const & a, Time const & b)
89 {
90         return (a.h == b.h && a.m == b.m && a.s == b.s && a.t == b.t);
91 }
92
93 bool
94 dcp::operator!= (Time const & a, Time const & b)
95 {
96         return !(a == b);
97 }
98
99 bool
100 dcp::operator<= (Time const & a, Time const & b)
101 {
102         if (a.h != b.h) {
103                 return a.h <= b.h;
104         }
105
106         if (a.m != b.m) {
107                 return a.m <= b.m;
108         }
109
110         if (a.s != b.s) {
111                 return a.s <= b.s;
112         }
113
114         if (a.t != b.t) {
115                 return a.t <= b.t;
116         }
117
118         return true;
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.t != b.t) {
137                 return a.t < b.t;
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.t != b.t) {
159                 return a.t > b.t;
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.t;
169         return s;
170 }
171
172 dcp::Time
173 dcp::operator+ (Time a, Time const & b)
174 {
175         Time r;
176
177         r.t = a.t + b.t;
178         if (r.t >= 250) {
179                 r.t -= 250;
180                 r.s++;
181         }
182
183         r.s += a.s + b.s;
184         if (r.s >= 60) {
185                 r.s -= 60;
186                 r.m++;
187         }
188
189         r.m += a.m + b.m;
190         if (r.m >= 60) {
191                 r.m -= 60;
192                 r.h++;
193         }
194
195         r.h += a.h + b.h;
196
197         return r;
198 }
199
200 dcp::Time
201 dcp::operator- (Time a, Time const & b)
202 {
203         Time r;
204
205         r.t = a.t - b.t;
206         if (r.t < 0) {
207                 r.t += 250;
208                 r.s--;
209         }
210
211         r.s += (a.s - b.s);
212         if (r.s < 0) {
213                 r.s += 60;
214                 r.m--;
215         }
216
217         r.m += (a.m - b.m);
218         if (r.m < 0) {
219                 r.m += 60;
220                 r.h--;
221         }
222
223         r.h += (a.h - b.h);
224
225         return r;
226 }
227
228 float
229 dcp::operator/ (Time a, Time const & b)
230 {
231         int64_t const at = a.h * 3600 * 250 + a.m * 60 * 250 + a.s * 250 + a.t;
232         int64_t const bt = b.h * 3600 * 250 + b.m * 60 * 250 + b.s * 250 + b.t;
233         return float (at) / bt;
234 }
235
236 /** @return A string of the form h:m:s:t */
237 string
238 Time::to_string () const
239 {
240         stringstream str;
241         str << h << ":" << m << ":" << s << ":" << t;
242         return str.str ();
243 }
244
245 /** @return This time in ticks */
246 int64_t
247 Time::to_ticks () const
248 {
249         return int64_t(t) + int64_t(s) * 250 + int64_t(m) * 60 * 250 + int64_t(h) * 60 * 60 * 250;
250 }
251