-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathTimestamp.cpp
More file actions
182 lines (145 loc) · 7.14 KB
/
Timestamp.cpp
File metadata and controls
182 lines (145 loc) · 7.14 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <version>
#ifdef __cpp_lib_print
# include <format>
#endif
#include "avcpp/rational.h"
#include "avcpp/timestamp.h"
#ifdef _MSC_VER
# pragma warning(disable : 4702) // Disable warning: unreachable code
#endif
#if AVCPP_CXX_STANDARD < 20 || __cpp_lib_chrono < 201907L
#include <sstream>
#include <iostream>
#include <type_traits>
#include <chrono>
template<typename Rep, typename Period>
std::ostream& operator<<(std::ostream& ost, const std::chrono::duration<Rep, Period>& dur)
{
using namespace std::literals;
std::ostringstream s;
s.flags(ost.flags());
s.imbue(ost.getloc());
s.precision(ost.precision());
s << +dur.count();
// Ref: https://en.cppreference.com/w/cpp/chrono/duration/operator_ltlt.html
if constexpr (std::is_same_v<Period, std::atto>) s << "as"sv;
else if constexpr (std::is_same_v<Period, std::femto>) s << "fs"sv;
else if constexpr (std::is_same_v<Period, std::pico>) s << "fs"sv;
else if constexpr (std::is_same_v<Period, std::nano>) s << "ns"sv;
else if constexpr (std::is_same_v<Period, std::micro>) s << "us"sv;
else if constexpr (std::is_same_v<Period, std::milli>) s << "ms"sv;
else if constexpr (std::is_same_v<Period, std::centi>) s << "cs"sv;
else if constexpr (std::is_same_v<Period, std::deci>) s << "ds"sv;
else if constexpr (std::is_same_v<Period, std::ratio<1>>) s << "s"sv;
else if constexpr (std::is_same_v<Period, std::deca>) s << "das"sv;
else if constexpr (std::is_same_v<Period, std::hecto>) s << "hs"sv;
else if constexpr (std::is_same_v<Period, std::kilo>) s << "ks"sv;
else if constexpr (std::is_same_v<Period, std::mega>) s << "Ms"sv;
else if constexpr (std::is_same_v<Period, std::giga>) s << "Gs"sv;
else if constexpr (std::is_same_v<Period, std::tera>) s << "Ts"sv;
else if constexpr (std::is_same_v<Period, std::peta>) s << "Ps"sv;
else if constexpr (std::is_same_v<Period, std::exa>) s << "Es"sv;
else if constexpr (std::is_same_v<Period, std::ratio<60>>) s << "min"sv;
else if constexpr (std::is_same_v<Period, std::ratio<3600>>) s << "h"sv;
else if constexpr (std::is_same_v<Period, std::ratio<86400>>) s << "d"sv;
else if constexpr (Period::den == 1) s << '[' << Period::num << "]s"sv;
else
s << '[' << Period::num << '/' << Period::num << "]s"sv;
ost << std::move(s).str();
return ost;
}
#endif
#include <catch2/catch_test_macros.hpp>
TEST_CASE("Core::Timestamp", "Timestamp")
{
SECTION("Overflow operator+(a,b)")
{
{
auto const tsLimit = std::numeric_limits<int64_t>::max() / 48000;
av::Timestamp t {
tsLimit, av::Rational {1, 48000}
};
av::Timestamp inc {48000, t.timebase()}; // 1s
auto v1 = t;
v1 += inc;
auto v2 = t + inc;
auto v3 = inc + t;
CHECK(v1 == v2);
CHECK(v1 == v3);
}
}
SECTION("Floating point std::duration")
{
{
std::chrono::duration<double> seconds{1.53f};
av::Timestamp fromDuration{std::chrono::duration_cast<std::chrono::milliseconds>(seconds)};
INFO("std::chrono::duration<double>: " << seconds.count());
INFO("av::Timestamp::seconds: " << fromDuration.seconds());
INFO("av::Timestamp: " << fromDuration);
REQUIRE(std::abs(fromDuration.seconds() - seconds.count()) <= 0.001);
auto toDoubleDuration = fromDuration.toDuration<std::chrono::duration<double>>();
INFO("toDoubleDuration: " << toDoubleDuration);
REQUIRE(std::abs(toDoubleDuration.count() - seconds.count()) <= 0.001);
}
// Double and Ratio different to {1,1}
{
using Ratio = std::milli;
std::chrono::duration<double, Ratio> ms{1530.17};
av::Timestamp fromDuration{std::chrono::duration_cast<std::chrono::microseconds>(ms)};
INFO("fromDuration(ms): " << fromDuration.seconds()
<< "s, val: " << fromDuration);
REQUIRE(std::abs(fromDuration.seconds() - ms.count() * Ratio::num / Ratio::den) <= 0.00001);
auto toDoubleDuration = fromDuration.toDuration<std::chrono::duration<double, Ratio>>();
INFO("toDoubleDuration: " << toDoubleDuration);
REQUIRE(std::abs(toDoubleDuration.count() - ms.count()) <= 0.00001);
}
// Double and non-standard ratio
{
using Ratio = std::ratio<1, 48000>;
std::chrono::duration<double, Ratio> dur{1530.17};
av::Timestamp fromDuration{std::chrono::duration_cast<std::chrono::nanoseconds>(dur)};
INFO("fromDuration(dur): " << fromDuration.seconds()
<< "s, val: " << fromDuration);
REQUIRE(std::abs(fromDuration.seconds() - dur.count() * Ratio::num / Ratio::den) <= 0.00001);
auto toDoubleDuration = fromDuration.toDuration<std::chrono::duration<double, Ratio>>();
INFO("toDoubleDuration: " << toDoubleDuration);
REQUIRE(std::abs(toDoubleDuration.count() - dur.count()) <= 0.0001);
}
// Ctor from the floating point duration
{
std::chrono::duration<double> seconds{1.57f};
{
av::Timestamp fromDuration{seconds, std::milli{}};
auto const precision = fromDuration.timebase().getDouble();
INFO("std::chrono::duration<double>:count: " << seconds.count());
INFO("std::chrono::duration<double>: " << seconds);
INFO("av::Timestamp::seconds: " << fromDuration.seconds());
INFO("av::Timestamp: " << fromDuration);
REQUIRE(std::abs(fromDuration.seconds() - seconds.count()) < precision);
auto toDoubleDuration = fromDuration.toDuration<std::chrono::duration<double>>();
INFO("toDoubleDuration: " << toDoubleDuration);
REQUIRE(std::abs(toDoubleDuration.count() - seconds.count()) < precision);
}
{
av::Timestamp fromDuration{seconds, av::Rational{1, 1000}};
auto const precision = fromDuration.timebase().getDouble();
INFO("std::chrono::duration<double>:count: " << seconds.count());
INFO("std::chrono::duration<double>: " << seconds);
INFO("av::Timestamp::seconds: " << fromDuration.seconds());
INFO("av::Timestamp: " << fromDuration);
REQUIRE(std::abs(fromDuration.seconds() - seconds.count()) < precision);
auto toDoubleDuration = fromDuration.toDuration<std::chrono::duration<double>>();
INFO("toDoubleDuration: " << toDoubleDuration);
REQUIRE(std::abs(toDoubleDuration.count() - seconds.count()) < precision);
}
}
}
#ifdef __cpp_lib_print
SECTION("std::format formatter")
{
av::Timestamp ts(48000, av::Rational {1, 48000}); // 1s
auto str = std::format("{}", ts);
CHECK(str == "48000*1/48000");
}
#endif
}