Supporters update.
[dcpomatic.git] / src / lib / kdm_util.cc
1 /*
2     Copyright (C) 2023 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 "kdm_util.h"
23 #include "screen.h"
24 #include <dcp/certificate.h>
25 #include <boost/optional.hpp>
26
27 #include "i18n.h"
28
29
30 using std::list;
31 using std::pair;
32 using std::shared_ptr;
33 using std::string;
34 using boost::optional;
35
36
37 KDMCertificatePeriod
38 check_kdm_and_certificate_validity_periods(
39         string const& cinema_name,
40         string const& screen_name,
41         dcp::Certificate const& recipient,
42         dcp::LocalTime kdm_from,
43         dcp::LocalTime kdm_to
44         )
45 {
46         auto overlaps = [](dcp::LocalTime from_a, dcp::LocalTime to_a, dcp::LocalTime from_b, dcp::LocalTime to_b) {
47                 return std::max(from_a, from_b) < std::min(to_a, to_b);
48         };
49
50         auto contains = [](dcp::LocalTime bigger_from, dcp::LocalTime bigger_to, dcp::LocalTime smaller_from, dcp::LocalTime smaller_to) {
51                 return bigger_from <= smaller_from && bigger_to >= smaller_to;
52         };
53
54         KDMCertificatePeriod period(
55                 cinema_name,
56                 screen_name,
57                 recipient.not_before(),
58                 recipient.not_after()
59                 );
60
61         if (contains(recipient.not_before(), recipient.not_after(), kdm_from, kdm_to)) {
62                 period.overlap = KDMCertificateOverlap::KDM_WITHIN_CERTIFICATE;
63                 return period;
64         }
65
66         if (overlaps(recipient.not_before(), recipient.not_after(), kdm_from, kdm_to)) {
67                 /* The KDM overlaps the certificate validity: maybe not the end of the world */
68                 period.overlap = KDMCertificateOverlap::KDM_OVERLAPS_CERTIFICATE;
69                 return period;
70         } else {
71                 /* The KDM validity is totally outside the certificate validity: bad news */
72                 period.overlap = KDMCertificateOverlap::KDM_OUTSIDE_CERTIFICATE;
73                 return period;
74         }
75 }
76