blob: 43ef161a690107b3e6944bd7803cb40c8bdd65a9 [file] [log] [blame]
danilchapb1ac2032015-11-26 09:01:10 -08001/*
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*/
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#ifndef SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_
11#define SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_
danilchapb1ac2032015-11-26 09:01:10 -080012
pbosc7c26a02017-01-02 08:42:32 -080013#include <stdint.h>
14
danilchapb1ac2032015-11-26 09:01:10 -080015namespace webrtc {
16
17class NtpTime {
18 public:
danilchap27260ce2017-02-15 01:18:15 -080019 static constexpr uint64_t kFractionsPerSecond = 0x100000000;
20 NtpTime() : value_(0) {}
21 explicit NtpTime(uint64_t value) : value_(value) {}
danilchapb1ac2032015-11-26 09:01:10 -080022 NtpTime(uint32_t seconds, uint32_t fractions)
danilchap27260ce2017-02-15 01:18:15 -080023 : value_(seconds * kFractionsPerSecond + fractions) {}
danilchapb1ac2032015-11-26 09:01:10 -080024
25 NtpTime(const NtpTime&) = default;
26 NtpTime& operator=(const NtpTime&) = default;
danilchap27260ce2017-02-15 01:18:15 -080027 explicit operator uint64_t() const { return value_; }
danilchapb1ac2032015-11-26 09:01:10 -080028
danilchapb1ac2032015-11-26 09:01:10 -080029 void Set(uint32_t seconds, uint32_t fractions) {
danilchap27260ce2017-02-15 01:18:15 -080030 value_ = seconds * kFractionsPerSecond + fractions;
danilchapb1ac2032015-11-26 09:01:10 -080031 }
danilchap27260ce2017-02-15 01:18:15 -080032 void Reset() { value_ = 0; }
danilchapb1ac2032015-11-26 09:01:10 -080033
danilchap37953762017-02-09 11:15:25 -080034 int64_t ToMs() const {
35 static constexpr double kNtpFracPerMs = 4.294967296E6; // 2^32 / 1000.
danilchap27260ce2017-02-15 01:18:15 -080036 const double frac_ms = static_cast<double>(fractions()) / kNtpFracPerMs;
37 return 1000 * static_cast<int64_t>(seconds()) +
danilchap37953762017-02-09 11:15:25 -080038 static_cast<int64_t>(frac_ms + 0.5);
39 }
danilchap27260ce2017-02-15 01:18:15 -080040 // NTP standard (RFC1305, section 3.1) explicitly state value 0 is invalid.
41 bool Valid() const { return value_ != 0; }
danilchapb1ac2032015-11-26 09:01:10 -080042
danilchap27260ce2017-02-15 01:18:15 -080043 uint32_t seconds() const { return value_ / kFractionsPerSecond; }
44 uint32_t fractions() const { return value_ % kFractionsPerSecond; }
danilchapb1ac2032015-11-26 09:01:10 -080045
46 private:
danilchap27260ce2017-02-15 01:18:15 -080047 uint64_t value_;
danilchapb1ac2032015-11-26 09:01:10 -080048};
49
50inline bool operator==(const NtpTime& n1, const NtpTime& n2) {
danilchap27260ce2017-02-15 01:18:15 -080051 return static_cast<uint64_t>(n1) == static_cast<uint64_t>(n2);
danilchapb1ac2032015-11-26 09:01:10 -080052}
53inline bool operator!=(const NtpTime& n1, const NtpTime& n2) {
54 return !(n1 == n2);
55}
56
57} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020058#endif // SYSTEM_WRAPPERS_INCLUDE_NTP_TIME_H_