/Users/deen/code/yugabyte-db/src/yb/util/timestamp.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #ifndef YB_UTIL_TIMESTAMP_H_ |
15 | | #define YB_UTIL_TIMESTAMP_H_ |
16 | | |
17 | | #include <inttypes.h> |
18 | | #include <string> |
19 | | #include "yb/util/status_fwd.h" |
20 | | |
21 | | namespace yb { |
22 | | |
23 | | class Timestamp { |
24 | | public: |
25 | | typedef int64_t val_type; |
26 | 16.9M | explicit Timestamp(int64_t value) : value_(value) {} |
27 | 0 | Timestamp() : value_(0) {} |
28 | | int64_t ToInt64() const; |
29 | | CHECKED_STATUS FromInt64(int64_t value); |
30 | | |
31 | | std::string ToString() const; |
32 | | |
33 | | std::string ToFormattedString() const; |
34 | | |
35 | | // Return date in human readable format (in local time zone). |
36 | | // For example, 2021-Jan-10 22:29:35.776000. |
37 | | std::string ToHumanReadableTime() const; |
38 | | |
39 | 17.1k | val_type value() const { return value_; } |
40 | 0 | void set_value(int64_t value) {value_ = value;} |
41 | | |
42 | 875k | bool operator ==(const Timestamp &other) const { |
43 | 875k | return value_ == other.value_; |
44 | 875k | } |
45 | 0 | bool operator !=(const Timestamp &other) const { |
46 | 0 | return value_ != other.value_; |
47 | 0 | } |
48 | | |
49 | | int CompareTo(const Timestamp &other) const; |
50 | | |
51 | 640 | bool operator <(const Timestamp& other) const { |
52 | 640 | return CompareTo(other) < 0; |
53 | 640 | } |
54 | | |
55 | 0 | bool operator >(const Timestamp& other) const { |
56 | 0 | return CompareTo(other) > 0; |
57 | 0 | } |
58 | | |
59 | 0 | bool operator <=(const Timestamp& other) const { |
60 | 0 | return CompareTo(other) <= 0; |
61 | 0 | } |
62 | | |
63 | 0 | bool operator >=(const Timestamp& other) const { |
64 | 0 | return CompareTo(other) >= 0; |
65 | 0 | } |
66 | | |
67 | | private: |
68 | | val_type value_; |
69 | | }; |
70 | | |
71 | 640 | inline int Timestamp::CompareTo(const Timestamp &other) const { |
72 | 640 | if (value_ < other.value_) { |
73 | 297 | return -1; |
74 | 343 | } else if (value_ > other.value_) { |
75 | 149 | return 1; |
76 | 149 | } |
77 | 194 | return 0; |
78 | 194 | } |
79 | | |
80 | 0 | inline std::ostream &operator <<(std::ostream &o, const Timestamp ×tamp) { |
81 | 0 | return o << timestamp.ToString(); |
82 | 0 | } |
83 | | |
84 | | } // namespace yb |
85 | | |
86 | | #endif // YB_UTIL_TIMESTAMP_H_ |