OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/cryptauth/data_with_timestamp.h" |
| 6 |
| 7 #include <iomanip> |
| 8 |
| 9 #include "base/logging.h" |
| 10 |
| 11 namespace cryptauth { |
| 12 |
| 13 DataWithTimestamp::DataWithTimestamp(const std::string& data, |
| 14 const int64_t start_timestamp_ms, |
| 15 const int64_t end_timestamp_ms) |
| 16 : data(data), |
| 17 start_timestamp_ms(start_timestamp_ms), |
| 18 end_timestamp_ms(end_timestamp_ms) { |
| 19 DCHECK(start_timestamp_ms < end_timestamp_ms); |
| 20 DCHECK(data.size()); |
| 21 } |
| 22 |
| 23 DataWithTimestamp::DataWithTimestamp(const DataWithTimestamp& other) |
| 24 : data(other.data), |
| 25 start_timestamp_ms(other.start_timestamp_ms), |
| 26 end_timestamp_ms(other.end_timestamp_ms) { |
| 27 DCHECK(start_timestamp_ms < end_timestamp_ms); |
| 28 DCHECK(data.size()); |
| 29 } |
| 30 |
| 31 // static. |
| 32 std::string DataWithTimestamp::ToDebugString( |
| 33 const std::vector<DataWithTimestamp>& data_with_timestamps) { |
| 34 std::stringstream ss; |
| 35 ss << "["; |
| 36 for (const DataWithTimestamp& data : data_with_timestamps) { |
| 37 ss << "\n (" << data.start_timestamp_ms << ": " << data.DataInHex() |
| 38 << "),"; |
| 39 } |
| 40 ss << "\n]"; |
| 41 return ss.str(); |
| 42 } |
| 43 |
| 44 bool DataWithTimestamp::ContainsTime(const int64_t timestamp_ms) const { |
| 45 return start_timestamp_ms <= timestamp_ms && timestamp_ms < end_timestamp_ms; |
| 46 } |
| 47 |
| 48 std::string DataWithTimestamp::DataInHex() const { |
| 49 std::stringstream ss; |
| 50 ss << "0x"; |
| 51 for (uint8_t byte : data) { |
| 52 ss << std::hex << std::setfill('0') << std::setw(2) |
| 53 << static_cast<uint64_t>(byte); |
| 54 } |
| 55 return ss.str(); |
| 56 } |
| 57 |
| 58 bool DataWithTimestamp::operator==(const DataWithTimestamp& other) const { |
| 59 return data == other.data && start_timestamp_ms == other.start_timestamp_ms && |
| 60 end_timestamp_ms == other.end_timestamp_ms; |
| 61 } |
| 62 |
| 63 } // namespace cryptauth |
OLD | NEW |