Some missed bits from 0.x.
[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 "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)
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         s = floor (ss);
60         t = int (round (1000 * (ss - s) / 4));
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 = raw_convert<int> (b[0]);
82         m = raw_convert<int> (b[1]);
83         s = raw_convert<int> (b[2]);
84         t = raw_convert<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         return a < b || a == b;
103 }
104
105 bool
106 dcp::operator>= (Time const & a, Time const & b)
107 {
108         return a > b || a == b;
109 }
110
111 bool
112 dcp::operator< (Time const & a, Time const & b)
113 {
114         if (a.h != b.h) {
115                 return a.h < b.h;
116         }
117
118         if (a.m != b.m) {
119                 return a.m < b.m;
120         }
121
122         if (a.s != b.s) {
123                 return a.s < b.s;
124         }
125
126         if (a.t != b.t) {
127                 return a.t < b.t;
128         }
129
130         return true;
131 }
132
133 bool
134 dcp::operator> (Time const & a, Time const & b)
135 {
136         if (a.h != b.h) {
137                 return a.h > b.h;
138         }
139
140         if (a.m != b.m) {
141                 return a.m > b.m;
142         }
143
144         if (a.s != b.s) {
145                 return a.s > b.s;
146         }
147
148         if (a.t != b.t) {
149                 return a.t > b.t;
150         }
151
152         return true;
153 }
154
155 ostream &
156 dcp::operator<< (ostream& s, Time const & t)
157 {
158         s << t.h << ":" << t.m << ":" << t.s << "." << t.t;
159         return s;
160 }
161
162 dcp::Time
163 dcp::operator+ (Time a, Time const & b)
164 {
165         Time r;
166
167         r.t = a.t + b.t;
168         if (r.t >= 250) {
169                 r.t -= 250;
170                 r.s++;
171         }
172
173         r.s += a.s + b.s;
174         if (r.s >= 60) {
175                 r.s -= 60;
176                 r.m++;
177         }
178
179         r.m += a.m + b.m;
180         if (r.m >= 60) {
181                 r.m -= 60;
182                 r.h++;
183         }
184
185         r.h += a.h + b.h;
186
187         return r;
188 }
189
190 dcp::Time
191 dcp::operator- (Time a, Time const & b)
192 {
193         Time r;
194
195         r.t = a.t - b.t;
196         if (r.t < 0) {
197                 r.t += 250;
198                 r.s--;
199         }
200
201         r.s += (a.s - b.s);
202         if (r.s < 0) {
203                 r.s += 60;
204                 r.m--;
205         }
206
207         r.m += (a.m - b.m);
208         if (r.m < 0) {
209                 r.m += 60;
210                 r.h--;
211         }
212
213         r.h += (a.h - b.h);
214
215         return r;
216 }
217
218 float
219 dcp::operator/ (Time a, Time const & b)
220 {
221         int64_t const at = a.h * 3600 * 250 + a.m * 60 * 250 + a.s * 250 + a.t;
222         int64_t const bt = b.h * 3600 * 250 + b.m * 60 * 250 + b.s * 250 + b.t;
223         return float (at) / bt;
224 }
225
226 /** @return A string of the form h:m:s:t */
227 string
228 Time::to_string () const
229 {
230         stringstream str;
231         str << h << ":" << m << ":" << s << ":" << t;
232         return str.str ();
233 }
234
235 /** @return This time in ticks */
236 int64_t
237 Time::to_ticks () const
238 {
239         return int64_t(t) + int64_t(s) * 250 + int64_t(m) * 60 * 250 + int64_t(h) * 60 * 60 * 250;
240 }
241
242 double
243 Time::to_seconds () const
244 {
245         return double (to_ticks ()) / 250;
246 }