Some maths operations with Time.
[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         float sec_float = float (frame) / frames_per_second;
43         t = (int (floor (sec_float * 1000)) % 1000) / 4;
44         s = floor (sec_float);
45
46         if (s > 60) {
47                 m = s / 60;
48                 s -= m * 60;
49         }
50
51         if (m > 60) {
52                 h = m / 60;
53                 m -= h * 60;
54         }
55 }
56
57 Time::Time (string time)
58 {
59         vector<string> b;
60         split (b, time, is_any_of (":"));
61         if (b.size() != 4) {
62                 throw DCPReadError ("unrecognised time specification");
63         }
64         
65         h = lexical_cast<int> (b[0]);
66         m = lexical_cast<int> (b[1]);
67         s = lexical_cast<int> (b[2]);
68         t = lexical_cast<int> (b[3]);
69 }
70
71 bool
72 libdcp::operator== (Time const & a, Time const & b)
73 {
74         return (a.h == b.h && a.m == b.m && a.s == b.s && a.t == b.t);
75 }
76
77 bool
78 libdcp::operator<= (Time const & a, Time const & b)
79 {
80         if (a.h != b.h) {
81                 return a.h <= b.h;
82         }
83
84         if (a.m != b.m) {
85                 return a.m <= b.m;
86         }
87
88         if (a.s != b.s) {
89                 return a.s <= b.s;
90         }
91
92         if (a.t != b.t) {
93                 return a.t <= b.t;
94         }
95
96         return true;
97 }
98
99 bool
100 libdcp::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 libdcp::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 ostream &
144 libdcp::operator<< (ostream& s, Time const & t)
145 {
146         s << t.h << ":" << t.m << ":" << t.s << "." << t.t;
147         return s;
148 }
149
150 libdcp::Time
151 libdcp::operator+ (Time a, Time const & b)
152 {
153         Time r;
154
155         r.t = a.t + b.t;
156         if (r.t >= 250) {
157                 r.t -= 250;
158                 r.s++;
159         }
160
161         r.s += a.s + b.s;
162         if (r.s >= 60) {
163                 r.s -= 60;
164                 r.m++;
165         }
166
167         r.m += a.m + b.m;
168         if (r.m >= 60) {
169                 r.m -= 60;
170                 r.h++;
171         }
172
173         r.h += a.h + b.h;
174
175         return r;
176 }
177
178 libdcp::Time
179 libdcp::operator- (Time a, Time const & b)
180 {
181         Time r;
182
183         r.t = a.t - b.t;
184         if (r.t < 0) {
185                 r.t += 250;
186                 r.s--;
187         }
188
189         r.s += (a.s - b.s);
190         if (r.s < 0) {
191                 r.s += 60;
192                 r.m--;
193         }
194
195         r.m += (a.m - b.m);
196         if (r.m < 0) {
197                 r.m += 60;
198                 r.h--;
199         }
200
201         r.h += (a.h - b.h);
202
203         return r;
204 }
205
206 float
207 libdcp::operator/ (Time a, Time const & b)
208 {
209         int64_t const at = a.h * 3600 * 250 + a.m * 60 * 250 + a.s * 250 + a.t;
210         int64_t const bt = b.h * 3600 * 250 + b.m * 60 * 250 + b.s * 250 + b.t;
211         return float (at) / bt;
212 }