1221fb328a33168f2aa73a22e2201f960e30e441
[libdcp.git] / src / dcp_time.h
1 /*
2     Copyright (C) 2012-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 /** @file  src/dcp_time.h
21  *  @brief Time class.
22  */
23
24 #ifndef LIBDCP_TIME_H
25 #define LIBDCP_TIME_H
26
27 #include <stdint.h>
28 #include <string>
29 #include <iostream>
30
31 namespace dcp {
32
33 /** @class Time
34  *  @brief A representation of time within a DCP.
35  */
36 class Time
37 {
38 public:
39         /** Construct a zero Time */
40         Time () : h (0), m (0), s (0), e (0), tcr (1) {}
41
42         /** Construct a Time.
43          *  @param Frame index (starting from 0).
44          *  @param frames_per_second Frames per second.
45          *  @param tcr Timecode rate.
46          */
47         Time (int frame, int frames_per_second, int tcr);
48
49         /** Construct a Time from hours, minutes, seconds, editable units and a timecode rate.
50          *  @param h_ Hours.
51          *  @param m_ Minutes.
52          *  @param s_ Seconds.
53          *  @param e_ Editable units (where 1 editable unit is 1 / tcr_ seconds)
54          *  @param tcr_ Timecode rate; i.e. number of editable units per second.
55          */
56         Time (int h_, int m_, int s_, int e_, int tcr_)
57                 : h (h_)
58                 , m (m_)
59                 , s (s_)
60                 , e (e_)
61                 , tcr (tcr_)
62         {}
63
64         Time (double seconds);
65
66         Time (std::string time, int tcr);
67
68         int h; ///<   hours
69         int m; ///<   minutes
70         int s; ///<   seconds
71         int e; ///<   editable units (where 1 editable unit is 1 / tcr_ seconds)
72         int tcr; ///< timecode rate: the number of editable units per second.
73
74         std::string as_string () const;
75         double as_seconds () const;
76         int64_t as_editable_units (int tcr_) const;
77         Time rebase (int tcr_) const;
78
79 private:
80         void set (double seconds, int tcr);
81 };
82
83 extern bool operator== (Time const & a, Time const & b);
84 extern bool operator!= (Time const & a, Time const & b);
85 extern bool operator<= (Time const & a, Time const & b);
86 extern bool operator< (Time const & a, Time const & b);
87 extern bool operator> (Time const & a, Time const & b);
88 extern bool operator>= (Time const & a, Time const & b);
89 extern std::ostream & operator<< (std::ostream & s, Time const & t);
90 extern Time operator+ (Time a, Time b);
91 extern Time operator- (Time a, Time b);
92 extern float operator/ (Time a, Time const & b);
93
94 }
95
96 #endif