Add CPL::unset_version_number().
[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
51 extern bool operator== (Time const & a, Time const & b);
52 extern bool operator!= (Time const & a, Time const & b);
53 extern bool operator<= (Time const & a, Time const & b);
54 extern bool operator< (Time const & a, Time const & b);
55 extern bool operator> (Time const & a, Time const & b);
56 extern bool operator>= (Time const & a, Time const & b);
57 extern std::ostream & operator<< (std::ostream & s, Time const & t);
58 extern Time operator+ (Time a, Time b);
59 extern Time operator- (Time a, Time b);
60 extern float operator/ (Time a, Time const & b);
61
62 /** @class Time
63  *  @brief A representation of time within a DCP.
64  */
65 class Time
66 {
67 public:
68         /** Construct a zero Time */
69         Time () : h (0), m (0), s (0), e (0), tcr (1) {}
70
71         /** Construct a Time.
72          *  @param frame Frame index (starting from 0).
73          *  @param frames_per_second Frames per second.
74          *  @param tcr Timecode rate.
75          */
76         Time (int frame, double frames_per_second, int tcr);
77
78         /** Construct a Time from hours, minutes, seconds, editable units and a timecode rate.
79          *  @param h_ Hours.
80          *  @param m_ Minutes.
81          *  @param s_ Seconds.
82          *  @param e_ Editable units (where 1 editable unit is 1 / tcr_ seconds)
83          *  @param tcr_ Timecode rate; i.e. number of editable units per second.
84          */
85         Time (int h_, int m_, int s_, int e_, int tcr_)
86                 : h (h_)
87                 , m (m_)
88                 , s (s_)
89                 , e (e_)
90                 , tcr (tcr_)
91         {}
92
93         Time (double seconds, int tcr);
94
95         Time (std::string time, boost::optional<int> tcr);
96
97         int h; ///<   hours
98         int m; ///<   minutes
99         int s; ///<   seconds
100         int e; ///<   editable units (where 1 editable unit is 1 / tcr_ seconds)
101         int tcr; ///< timecode rate: the number of editable units per second.
102
103         std::string as_string (Standard standard) const;
104         double as_seconds () const;
105         int64_t as_editable_units (int tcr_) const;
106         Time rebase (int tcr_) const;
107
108         Time& operator+= (Time const & o) {
109                 *this = *this + o;
110                 return *this;
111         }
112
113 private:
114         void set (double seconds, int tcr);
115 };
116
117 }
118
119 #endif