aboutsummaryrefslogtreecommitdiff
path: root/cast/common/certificate/types.cc
blob: 82d513587a5e57e187f945198d52b35f2eafdc7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cast/common/certificate/types.h"

#include "platform/api/logging.h"

namespace cast {
namespace certificate {

bool operator<(const DateTime& a, const DateTime& b) {
  if (a.year < b.year) {
    return true;
  } else if (a.year > b.year) {
    return false;
  }
  if (a.month < b.month) {
    return true;
  } else if (a.month > b.month) {
    return false;
  }
  if (a.day < b.day) {
    return true;
  } else if (a.day > b.day) {
    return false;
  }
  if (a.hour < b.hour) {
    return true;
  } else if (a.hour > b.hour) {
    return false;
  }
  if (a.minute < b.minute) {
    return true;
  } else if (a.minute > b.minute) {
    return false;
  }
  if (a.second < b.second) {
    return true;
  } else if (a.second > b.second) {
    return false;
  }
  return false;
}

bool operator>(const DateTime& a, const DateTime& b) {
  return (b < a);
}

bool DateTimeFromSeconds(uint64_t seconds, DateTime* time) {
  struct tm tm = {};
  time_t sec = static_cast<time_t>(seconds);
  OSP_DCHECK_GE(sec, 0);
  OSP_DCHECK_EQ(static_cast<uint64_t>(sec), seconds);
  if (!gmtime_r(&sec, &tm)) {
    return false;
  }

  time->second = tm.tm_sec;
  time->minute = tm.tm_min;
  time->hour = tm.tm_hour;
  time->day = tm.tm_mday;
  time->month = tm.tm_mon + 1;
  time->year = tm.tm_year + 1900;

  return true;
}

}  // namespace certificate
}  // namespace cast