summaryrefslogtreecommitdiff
path: root/chromium/components/cryptauth/data_with_timestamp.cc
blob: 7fbc96c275323f9b44bbca7e605be5cfc72fcb37 (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
// Copyright 2017 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 "components/cryptauth/data_with_timestamp.h"

#include <iomanip>

#include "base/logging.h"

namespace cryptauth {

DataWithTimestamp::DataWithTimestamp(const std::string& data,
                                     const int64_t start_timestamp_ms,
                                     const int64_t end_timestamp_ms)
    : data(data),
      start_timestamp_ms(start_timestamp_ms),
      end_timestamp_ms(end_timestamp_ms) {
  DCHECK(start_timestamp_ms < end_timestamp_ms);
  DCHECK(data.size());
}

DataWithTimestamp::DataWithTimestamp(const DataWithTimestamp& other)
    : data(other.data),
      start_timestamp_ms(other.start_timestamp_ms),
      end_timestamp_ms(other.end_timestamp_ms) {
  DCHECK(start_timestamp_ms < end_timestamp_ms);
  DCHECK(data.size());
}

// static.
std::string DataWithTimestamp::ToDebugString(
    const std::vector<DataWithTimestamp>& data_with_timestamps) {
  std::stringstream ss;
  ss << "[";
  for (const DataWithTimestamp& data : data_with_timestamps) {
    ss << "\n  (" << data.start_timestamp_ms << ": " << data.DataInHex()
       << "),";
  }
  ss << "\n]";
  return ss.str();
}

bool DataWithTimestamp::ContainsTime(const int64_t timestamp_ms) const {
  return start_timestamp_ms <= timestamp_ms && timestamp_ms < end_timestamp_ms;
}

std::string DataWithTimestamp::DataInHex() const {
  std::stringstream ss;
  ss << "0x";
  for (uint8_t byte : data) {
    ss << std::hex << std::setfill('0') << std::setw(2)
       << static_cast<uint64_t>(byte);
  }
  return ss.str();
}

bool DataWithTimestamp::operator==(const DataWithTimestamp& other) const {
  return data == other.data && start_timestamp_ms == other.start_timestamp_ms &&
         end_timestamp_ms == other.end_timestamp_ms;
}

}  // namespace cryptauth