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