Simplify time representation; better in-tree DCP subtitle parser.
[libsub.git] / src / sub_time.h
1 /*
2     Copyright (C) 2014-2015 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 #ifndef LIBSUB_SUB_TIME_H
21 #define LIBSUB_SUB_TIME_H
22
23 #include <boost/optional.hpp>
24
25 namespace sub {
26         
27 class Rational
28 {
29 public:
30         Rational (int numerator_, int denominator_)
31                 : numerator (numerator_)
32                 , denominator (denominator_)
33         {}
34         
35         int numerator;
36         int denominator;
37
38         double fraction () const {
39                 return double (numerator) / denominator;
40         }
41 };
42
43 class Time
44 {
45 public:
46         Time ()
47                 : _seconds (0)
48                 , _frames (0)
49         {}
50
51         int hours () const;
52         int minutes () const;
53         int seconds () const;
54
55         int frames_at (Rational rate) const;
56         int milliseconds () const;
57
58         static Time from_hmsf (int h, int m, int s, int f, boost::optional<Rational> rate = boost::optional<Rational> ());
59         static Time from_hms (int h, int m, int s, int ms);
60         
61 private:
62         friend bool operator< (Time const & a, Time const & b);
63         friend bool operator> (Time const & a, Time const & b);
64         friend bool operator== (Time const & a, Time const & b);
65         friend std::ostream& operator<< (std::ostream& s, Time const & t);
66
67         Time (int seconds, int frames, boost::optional<Rational> rate)
68                 : _seconds (seconds)
69                 , _frames (frames)
70                 , _rate (rate)
71         {}
72         
73         int _seconds;
74         int _frames;
75         boost::optional<Rational> _rate;
76 };
77
78 bool operator< (Time const & a, Time const & b);
79 bool operator> (Time const & a, Time const & b);
80 bool operator== (Time const & a, Time const & b);
81 bool operator!= (Time const & a, Time const & b);
82 std::ostream& operator<< (std::ostream& s, Time const & t);
83         
84 }
85
86 #endif