summaryrefslogtreecommitdiff
path: root/src/sub_time.cc
blob: 763b4e44aba4e1389f118ba401fd3a0670bf3031 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
    Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "sub_time.h"
#include "sub_assert.h"
#include "exceptions.h"
#include <cmath>
#include <iomanip>
#include <iostream>

using std::ostream;
using std::cout;
using std::setw;
using std::setfill;
using boost::optional;
using namespace sub;

bool
sub::operator< (sub::Time const & a, sub::Time const & b)
{
	if (a._seconds != b._seconds) {
		return a._seconds < b._seconds;
	}

	if (!a._rate && !b._rate) {
		/* Can compare if neither has a specified frame rate */
		return a._frames < b._frames;
	}

	if ((a._rate && !b._rate) || (!a._rate && b._rate)) {
		throw UnknownFrameRateError ();
	}

	return (a._frames * a._rate.get().numerator * b._rate.get().denominator) < (b._frames * b._rate.get().numerator * a._rate.get().denominator);
}

bool
sub::operator> (sub::Time const & a, sub::Time const & b)
{
	if (a._seconds != b._seconds) {
		return a._seconds > b._seconds;
	}

	if (!a._rate && !b._rate) {
		/* Can compare if neither has a specified frame rate */
		return a._frames > b._frames;
	}

	if ((a._rate && !b._rate) || (!a._rate && b._rate)) {
		throw UnknownFrameRateError ();
	}

	return (a._frames * a._rate.get().numerator * b._rate.get().denominator) > (b._frames * b._rate.get().numerator * a._rate.get().denominator);
}

bool
sub::operator== (sub::Time const & a, sub::Time const & b)
{
	if (!a._rate && !b._rate) {
		/* Can compare if neither has a specified frame rate */
		return (a._seconds == b._seconds && a._frames == b._frames);
	}

	if ((a._rate && !b._rate) || (!a._rate && b._rate)) {
		throw UnknownFrameRateError ();
	}

	if (a._seconds != b._seconds) {
		return false;
	}

	return (a._frames * a._rate.get().numerator * b._rate.get().denominator) == (b._frames * b._rate.get().numerator * a._rate.get().denominator);
}

bool
sub::operator!= (sub::Time const & a, sub::Time const & b)
{
	return !(a == b);
}

ostream&
sub::operator<< (ostream& s, Time const & t)
{
	s << setw (2) << setfill('0') << t.hours() << ":"
	  << setw (2) << setfill('0') << t.minutes() << ":"
	  << setw (2) << setfill('0') << t.seconds() << ":"
	  << t._frames;

	if (t._rate) {
		s << " @ " << t._rate.get().numerator << "/" << t._rate.get().denominator;
	}

	return s;
}

int
Time::hours () const
{
	return _seconds / 3600;
}

int
Time::minutes () const
{
	return (_seconds - hours() * 3600) / 60;
}

int
Time::seconds () const
{
	return (_seconds - hours() * 3600 - minutes() * 60);
}

int
Time::frames_at (Rational rate) const
{
	if (!_rate) {
		throw UnknownFrameRateError ();
	}

	return rint (double (_frames) * _rate.get().denominator * rate.numerator / (_rate.get().numerator * rate.denominator));
}

int
Time::milliseconds () const
{
	return frames_at (Rational (1000, 1));
}

Time
Time::from_hmsf (int h, int m, int s, int f, optional<Rational> rate)
{
	return Time (h * 3600 + m * 60 + s, f, rate);
}

Time
Time::from_hms (int h, int m, int s, int ms)
{
	return Time (h * 3600 + m * 60 + s, ms, Rational (1000, 1));
}

/** Create a Time from a number of frames.
 *  rate must be integer.
 */
Time
Time::from_frames (int f, Rational rate)
{
	SUB_ASSERT (rate.denominator != 0);
	SUB_ASSERT (rate.integer ());
	return Time (f / rate.integer_fraction(), f % rate.integer_fraction(), rate);
}

double
Time::all_as_seconds () const
{
	return _seconds + double(milliseconds()) / 1000;
}

/** Add a time to this one.  Both *this and t must have a specified _rate */
void
Time::add (Time t)
{
	SUB_ASSERT (_rate);
	SUB_ASSERT (t._rate);

	Rational result_rate = max (*_rate, *t._rate);
	*this = Time::from_frames((all_as_seconds() + t.all_as_seconds()) * result_rate.fraction(), result_rate);
}

void
Time::scale (float f)
{
	SUB_ASSERT (_rate);
	SUB_ASSERT (_rate->denominator != 0);
	SUB_ASSERT (_rate->integer ());

	double const s = Time::all_as_seconds() * f;
	_seconds = floor (s);
	_frames = rint ((s - _seconds) * _rate->fraction());
}