Debian Codegraph @

cpp/include/proton/timestamp.hpp

raw
1 #ifndef PROTON_TIMESTAMP_HPP
2 #define PROTON_TIMESTAMP_HPP
3
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 */
22
23 #include "./duration.hpp"
24
25 #include <proton/type_compat.h>
26
27 /// @file
28 /// @copybrief proton::timestamp
29
30 namespace proton {
31
32 /// A 64-bit timestamp in milliseconds since the Unix epoch.
33 ///
34 /// The dawn of the Unix epoch was 00:00:00 (UTC), 1 January 1970.
35 class timestamp : private internal::comparable<timestamp> {
36 public:
37 /// A numeric type holding a value in milliseconds.
38 typedef int64_t numeric_type;
39
40 /// The current wall-clock time.
41 PN_CPP_EXTERN static timestamp now();
42
43 /// Construct from a value in milliseconds.
44 explicit timestamp(numeric_type ms = 0) : ms_(ms) {}
45
46 /// Assign a value in milliseconds.
47 timestamp& operator=(numeric_type ms) { ms_ = ms; return *this; }
48
49 /// Get the value in milliseconds.
50 numeric_type milliseconds() const { return ms_; }
51
52 private:
53 numeric_type ms_;
54 };
55
56 /// @name Comparison and arithmetic operators
57 /// @{
58 inline bool operator==(timestamp x, timestamp y) { return x.milliseconds() == y.milliseconds(); }
59 inline bool operator<(timestamp x, timestamp y) { return x.milliseconds() < y.milliseconds(); }
60
61 inline timestamp operator+(timestamp ts, duration d) { return timestamp(ts.milliseconds() + d.milliseconds()); }
62 inline timestamp operator-(timestamp ts, duration d) { return timestamp(ts.milliseconds() - d.milliseconds()); }
63 inline duration operator-(timestamp t0, timestamp t1) { return duration(t0.milliseconds() - t1.milliseconds()); }
64 inline timestamp operator+(duration d, timestamp ts) { return ts + d; }
65 /// @}
66
67 /// Print a timestamp.
68 PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, timestamp);
69
70 } // proton
71
72 #endif // PROTON_TIMESTAMP_HPP