danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | #ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_ |
| 11 | #define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_ |
| 12 | |
pbos | c7c26a0 | 2017-01-02 08:42:32 -0800 | [diff] [blame] | 13 | #include <stdint.h> |
| 14 | |
danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 15 | namespace webrtc { |
| 16 | |
| 17 | class NtpTime { |
| 18 | public: |
danilchap | 27260ce | 2017-02-15 01:18:15 -0800 | [diff] [blame^] | 19 | static constexpr uint64_t kFractionsPerSecond = 0x100000000; |
| 20 | NtpTime() : value_(0) {} |
| 21 | explicit NtpTime(uint64_t value) : value_(value) {} |
danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 22 | NtpTime(uint32_t seconds, uint32_t fractions) |
danilchap | 27260ce | 2017-02-15 01:18:15 -0800 | [diff] [blame^] | 23 | : value_(seconds * kFractionsPerSecond + fractions) {} |
danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 24 | |
| 25 | NtpTime(const NtpTime&) = default; |
| 26 | NtpTime& operator=(const NtpTime&) = default; |
danilchap | 27260ce | 2017-02-15 01:18:15 -0800 | [diff] [blame^] | 27 | explicit operator uint64_t() const { return value_; } |
danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 28 | |
danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 29 | void Set(uint32_t seconds, uint32_t fractions) { |
danilchap | 27260ce | 2017-02-15 01:18:15 -0800 | [diff] [blame^] | 30 | value_ = seconds * kFractionsPerSecond + fractions; |
danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 31 | } |
danilchap | 27260ce | 2017-02-15 01:18:15 -0800 | [diff] [blame^] | 32 | void Reset() { value_ = 0; } |
danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 33 | |
danilchap | 3795376 | 2017-02-09 11:15:25 -0800 | [diff] [blame] | 34 | int64_t ToMs() const { |
| 35 | static constexpr double kNtpFracPerMs = 4.294967296E6; // 2^32 / 1000. |
danilchap | 27260ce | 2017-02-15 01:18:15 -0800 | [diff] [blame^] | 36 | const double frac_ms = static_cast<double>(fractions()) / kNtpFracPerMs; |
| 37 | return 1000 * static_cast<int64_t>(seconds()) + |
danilchap | 3795376 | 2017-02-09 11:15:25 -0800 | [diff] [blame] | 38 | static_cast<int64_t>(frac_ms + 0.5); |
| 39 | } |
danilchap | 27260ce | 2017-02-15 01:18:15 -0800 | [diff] [blame^] | 40 | // NTP standard (RFC1305, section 3.1) explicitly state value 0 is invalid. |
| 41 | bool Valid() const { return value_ != 0; } |
danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 42 | |
danilchap | 27260ce | 2017-02-15 01:18:15 -0800 | [diff] [blame^] | 43 | uint32_t seconds() const { return value_ / kFractionsPerSecond; } |
| 44 | uint32_t fractions() const { return value_ % kFractionsPerSecond; } |
danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 45 | |
| 46 | private: |
danilchap | 27260ce | 2017-02-15 01:18:15 -0800 | [diff] [blame^] | 47 | uint64_t value_; |
danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | inline bool operator==(const NtpTime& n1, const NtpTime& n2) { |
danilchap | 27260ce | 2017-02-15 01:18:15 -0800 | [diff] [blame^] | 51 | return static_cast<uint64_t>(n1) == static_cast<uint64_t>(n2); |
danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 52 | } |
| 53 | inline bool operator!=(const NtpTime& n1, const NtpTime& n2) { |
| 54 | return !(n1 == n2); |
| 55 | } |
| 56 | |
| 57 | } // namespace webrtc |
| 58 | #endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_ |