Tighten up time parsing, and also allow the previously
[libdcp.git] / src / dcp_time.h
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 /** @file  src/dcp_time.h
35  *  @brief Time class.
36  */
37
38 #ifndef LIBDCP_TIME_H
39 #define LIBDCP_TIME_H
40
41 #include "types.h"
42 #include <boost/optional.hpp>
43 #include <stdint.h>
44 #include <string>
45 #include <iostream>
46
47 namespace dcp {
48
49 /** @class Time
50  *  @brief A representation of time within a DCP.
51  */
52 class Time
53 {
54 public:
55         /** Construct a zero Time */
56         Time () : h (0), m (0), s (0), e (0), tcr (1) {}
57
58         /** Construct a Time.
59          *  @param Frame index (starting from 0).
60          *  @param frames_per_second Frames per second.
61          *  @param tcr Timecode rate.
62          */
63         Time (int frame, double frames_per_second, int tcr);
64
65         /** Construct a Time from hours, minutes, seconds, editable units and a timecode rate.
66          *  @param h_ Hours.
67          *  @param m_ Minutes.
68          *  @param s_ Seconds.
69          *  @param e_ Editable units (where 1 editable unit is 1 / tcr_ seconds)
70          *  @param tcr_ Timecode rate; i.e. number of editable units per second.
71          */
72         Time (int h_, int m_, int s_, int e_, int tcr_)
73                 : h (h_)
74                 , m (m_)
75                 , s (s_)
76                 , e (e_)
77                 , tcr (tcr_)
78         {}
79
80         Time (double seconds, int tcr);
81
82         Time (std::string time, boost::optional<int> tcr);
83
84         int h; ///<   hours
85         int m; ///<   minutes
86         int s; ///<   seconds
87         int e; ///<   editable units (where 1 editable unit is 1 / tcr_ seconds)
88         int tcr; ///< timecode rate: the number of editable units per second.
89
90         std::string as_string (Standard standard) const;
91         double as_seconds () const;
92         int64_t as_editable_units (int tcr_) const;
93         Time rebase (int tcr_) const;
94
95 private:
96         void set (double seconds, int tcr);
97 };
98
99 extern bool operator== (Time const & a, Time const & b);
100 extern bool operator!= (Time const & a, Time const & b);
101 extern bool operator<= (Time const & a, Time const & b);
102 extern bool operator< (Time const & a, Time const & b);
103 extern bool operator> (Time const & a, Time const & b);
104 extern bool operator>= (Time const & a, Time const & b);
105 extern std::ostream & operator<< (std::ostream & s, Time const & t);
106 extern Time operator+ (Time a, Time b);
107 extern Time operator- (Time a, Time b);
108 extern float operator/ (Time a, Time const & b);
109
110 }
111
112 #endif