Agora RTC Objective-C API Reference Refactor

time_utils.h
Go to the documentation of this file.
1 //
2 // Agora Media SDK
3 //
4 // Copyright (c) 2021 Agora IO. All rights reserved.
5 //
6
7 #pragma once
8 #include <stdint.h>
9
10 namespace agora {
11 namespace base {
12
13 class NtpTime {
14 public:
15 static const uint64_t ntpFracPerSecond = 4294967296;
16
17 NtpTime() : ms_(0) {}
18
19 NtpTime(uint64_t ms) : ms_(ms) {}
20
21 NtpTime(uint32_t seconds, uint32_t fractions) {
22 const double fracMs = fractions * 1000.0 / static_cast<double>(ntpFracPerSecond);
23 ms_ = static_cast<uint64_t>(seconds) * 1000 + static_cast<uint64_t>(0.5 + fracMs);
24 }
25
26 operator uint64_t() const { return ms_; }
27
33 uint64_t Ms() const {
34 return ms_;
35 }
36
42 bool Valid() const { return ms_ != 0; }
43
49 uint32_t ToSeconds() const {
50 return static_cast<uint32_t>(ms_ / 1000);
51 }
52
58 uint32_t ToFractions() const {
59 return static_cast<uint32_t>((ms_ % 1000) * static_cast<double>(ntpFracPerSecond) / 1000.0);
60 }
61
70 uint64_t ToTimestamp() const {
71 return ToSeconds() * ntpFracPerSecond + ToFractions();
72 }
73
74 private:
75 uint64_t ms_;
76 };
77
78 inline bool operator==(const NtpTime& n1, const NtpTime& n2) {
79 return static_cast<uint64_t>(n1) == static_cast<uint64_t>(n2);
80 }
81
82 inline bool operator!=(const NtpTime& n1, const NtpTime& n2) { return !(n1 == n2); }
83
84 } // namespace base
85 } // namespace agora
agora::base::NtpTime::NtpTime
NtpTime(uint64_t ms)
Definition: time_utils.h:19
agora::base::NtpTime::Valid
bool Valid() const
Definition: time_utils.h:42
agora::base::NtpTime
Definition: time_utils.h:13
agora::base::operator!=
bool operator!=(const NtpTime &n1, const NtpTime &n2)
Definition: time_utils.h:82
agora::base::NtpTime::ms_
uint64_t ms_
Definition: time_utils.h:75
agora::base::operator==
bool operator==(const NtpTime &n1, const NtpTime &n2)
Definition: time_utils.h:78
agora::base::NtpTime::NtpTime
NtpTime()
Definition: time_utils.h:17
agora
Definition: AgoraAtomicOps.h:21
agora::base::NtpTime::ToSeconds
uint32_t ToSeconds() const
Definition: time_utils.h:49
agora::base::NtpTime::ntpFracPerSecond
static const uint64_t ntpFracPerSecond
Definition: time_utils.h:15
agora::base::NtpTime::Ms
uint64_t Ms() const
Definition: time_utils.h:33
agora::base::NtpTime::NtpTime
NtpTime(uint32_t seconds, uint32_t fractions)
Definition: time_utils.h:21
agora::base::NtpTime::ToFractions
uint32_t ToFractions() const
Definition: time_utils.h:58
agora::base::NtpTime::ToTimestamp
uint64_t ToTimestamp() const
Definition: time_utils.h:70