2 Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 #include "exceptions.h"
29 using boost::optional;
33 sub::operator< (sub::Time const & a, sub::Time const & b)
35 if (a._seconds != b._seconds) {
36 return a._seconds < b._seconds;
39 if (!a._rate && !b._rate) {
40 /* Can compare if neither has a specified frame rate */
41 return a._frames < b._frames;
44 if ((a._rate && !b._rate) || (!a._rate && b._rate)) {
45 throw UnknownFrameRateError ();
48 return (a._frames * a._rate.get().numerator * b._rate.get().denominator) < (b._frames * b._rate.get().numerator * a._rate.get().denominator);
52 sub::operator> (sub::Time const & a, sub::Time const & b)
54 if (a._seconds != b._seconds) {
55 return a._seconds > b._seconds;
58 if (!a._rate && !b._rate) {
59 /* Can compare if neither has a specified frame rate */
60 return a._frames > b._frames;
63 if ((a._rate && !b._rate) || (!a._rate && b._rate)) {
64 throw UnknownFrameRateError ();
67 return (a._frames * a._rate.get().numerator * b._rate.get().denominator) > (b._frames * b._rate.get().numerator * a._rate.get().denominator);
71 sub::operator== (sub::Time const & a, sub::Time const & b)
73 if (!a._rate && !b._rate) {
74 /* Can compare if neither has a specified frame rate */
75 return (a._seconds == b._seconds && a._frames == b._frames);
78 if ((a._rate && !b._rate) || (!a._rate && b._rate)) {
79 throw UnknownFrameRateError ();
82 if (a._seconds != b._seconds) {
86 return (a._frames * a._rate.get().numerator * b._rate.get().denominator) == (b._frames * b._rate.get().numerator * a._rate.get().denominator);
90 sub::operator!= (sub::Time const & a, sub::Time const & b)
96 sub::operator<< (ostream& s, Time const & t)
98 s << setw (2) << setfill('0') << t.hours() << ":"
99 << setw (2) << setfill('0') << t.minutes() << ":"
100 << setw (2) << setfill('0') << t.seconds() << ":"
104 s << " @ " << t._rate.get().numerator << "/" << t._rate.get().denominator;
113 return _seconds / 3600;
117 Time::minutes () const
119 return (_seconds - hours() * 3600) / 60;
123 Time::seconds () const
125 return (_seconds - hours() * 3600 - minutes() * 60);
129 Time::frames_at (Rational rate) const
132 throw UnknownFrameRateError ();
135 return rint (double (_frames) * _rate.get().denominator * rate.numerator / (_rate.get().numerator * rate.denominator));
139 Time::milliseconds () const
141 return frames_at (Rational (1000, 1));
145 Time::from_hmsf (int h, int m, int s, int f, optional<Rational> rate)
147 return Time (h * 3600 + m * 60 + s, f, rate);
151 Time::from_hms (int h, int m, int s, int ms)
153 return Time (h * 3600 + m * 60 + s, ms, Rational (1000, 1));
157 Time::all_as_seconds () const
160 throw UnknownFrameRateError ();
163 return _seconds + double (_frames) / _rate.get().fraction ();