Add operator<= for HMSF.
[dcpomatic.git] / src / lib / dcpomatic_time.cc
1 /*
2     Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "dcpomatic_time.h"
23 #include <inttypes.h>
24
25
26 using std::string;
27 using namespace dcpomatic;
28
29
30 bool
31 dcpomatic::operator<=(HMSF const& a, HMSF const& b)
32 {
33         if (a.h != b.h) {
34                 return a.h <= b.h;
35         }
36
37         if (a.m != b.m) {
38                 return a.m <= b.m;
39         }
40
41         if (a.s != b.s) {
42                 return a.s <= b.s;
43         }
44
45         return a.f <= b.f;
46 }
47
48
49 template <>
50 Time<ContentTimeDifferentiator, DCPTimeDifferentiator>::Time (DCPTime d, FrameRateChange f)
51         : _t (llrint(d.get() * f.speed_up))
52 {
53
54 }
55
56
57 template <>
58 Time<DCPTimeDifferentiator, ContentTimeDifferentiator>::Time (ContentTime d, FrameRateChange f)
59         : _t (llrint(d.get() / f.speed_up))
60 {
61
62 }
63
64
65 DCPTime
66 dcpomatic::min (DCPTime a, DCPTime b)
67 {
68         if (a < b) {
69                 return a;
70         }
71
72         return b;
73 }
74
75
76 DCPTime
77 dcpomatic::max (DCPTime a, DCPTime b)
78 {
79         if (a > b) {
80                 return a;
81         }
82
83         return b;
84 }
85
86
87 ContentTime
88 dcpomatic::min (ContentTime a, ContentTime b)
89 {
90         if (a < b) {
91                 return a;
92         }
93
94         return b;
95 }
96
97
98 ContentTime
99 dcpomatic::max (ContentTime a, ContentTime b)
100 {
101         if (a > b) {
102                 return a;
103         }
104
105         return b;
106 }
107
108
109 string
110 dcpomatic::to_string (ContentTime t)
111 {
112         char buffer[64];
113 #ifdef DCPOMATIC_WINDOWS
114         __mingw_snprintf (buffer, sizeof(buffer), "[CONT %" PRId64 " %fs]", t.get(), t.seconds());
115 #else
116         snprintf (buffer, sizeof(buffer), "[CONT %" PRId64 " %fs]", t.get(), t.seconds());
117 #endif
118         return buffer;
119 }
120
121
122 string
123 dcpomatic::to_string (DCPTime t)
124 {
125         char buffer[64];
126 #ifdef DCPOMATIC_WINDOWS
127         __mingw_snprintf (buffer, sizeof(buffer), "[DCP %" PRId64 " %fs]", t.get(), t.seconds());
128 #else
129         snprintf (buffer, sizeof(buffer), "[DCP %" PRId64 " %fs]", t.get(), t.seconds());
130 #endif
131         return buffer;
132 }
133
134
135 string
136 dcpomatic::to_string (DCPTimePeriod p)
137 {
138         char buffer[64];
139 #ifdef DCPOMATIC_WINDOWS
140         __mingw_snprintf (buffer, sizeof(buffer), "[DCP %" PRId64 " %fs -> %" PRId64 " %fs]", p.from.get(), p.from.seconds(), p.to.get(), p.to.seconds());
141 #else
142         snprintf (buffer, sizeof(buffer), "[DCP %" PRId64 " %fs -> %" PRId64 " %fs]", p.from.get(), p.from.seconds(), p.to.get(), p.to.seconds());
143 #endif
144         return buffer;
145 }