-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestamp.hpp
More file actions
80 lines (66 loc) · 2.42 KB
/
Copy pathtimestamp.hpp
File metadata and controls
80 lines (66 loc) · 2.42 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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2025-Present Datadog, Inc.
#pragma once
#include <chrono>
#include <ratio>
#include "datadog/impl/core/util/assert.hpp"
#include "datadog/impl/core/util/json.hpp"
namespace datadog::impl {
/**
* IsoTimestamp allows JSON-serializable struct types to be explicit about the timestamp
* representation they use for a given member: ISO-8601 is the default format when
* encoding a datadog::Timestamp value as JSON.
*/
using IsoTimestamp = Timestamp;
/**
* Wrapper for a datadog::Timestamp value that is serialized to JSON as an integer count
* of (milliseconds|nanoseconds|etc.) since the Unix epoch, where the target unit is
* defined by `Period`.
*/
template <typename Period>
struct IntegerTimestamp {
Timestamp value;
// Use the same integer representation as datadog::Timestamp, i.e. int64_t
using Rep = Timestamp::rep;
// Alow implicit conversion and assignment from datadog::Timestamp
IntegerTimestamp() {}
// NOLINTNEXTLINE(google-explicit-constructor)
IntegerTimestamp(const Timestamp& in_value) : value(in_value) {}
IntegerTimestamp& operator=(const Timestamp& in_value) {
value = in_value;
return *this;
}
/**
* Returns the currently-held timestamp value as an integer representing ticks since
* the Unix epoch in the unit specified by `Period`.
*/
Rep Count() const {
using Target = std::chrono::duration<Rep, Period>;
Target duration = std::chrono::duration_cast<Target>(value.time_since_epoch());
return duration.count();
}
};
template <typename Period>
size_t GetJsonSize(const IntegerTimestamp<Period>& value) {
const auto count = value.Count();
return GetJsonSize(count);
}
template <typename Period>
size_t WriteJson(char* dst, size_t n, const IntegerTimestamp<Period>& value) {
const auto count = value.Count();
return WriteJson(dst, n, count);
}
/**
* Holds a `datadog::Timestamp` value; serialized to JSON as a number representing
* elapsed nanoseconds since the Unix epoch.
*/
using NanoTimestamp = IntegerTimestamp<std::nano>;
/**
* Holds a `datadog::Timestamp` value; serialized to JSON as a number representing
* elapsed milliseconds since the Unix epoch.
*/
using MilliTimestamp = IntegerTimestamp<std::milli>;
} // namespace datadog::impl