Realm C++ SDK Version v2.2.0

timestamp.hpp

1
2//
3// Copyright 2024 Realm Inc.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
18
19#ifndef CPPREALM_BRIDGE_TIMESTAMP_HPP
20#define CPPREALM_BRIDGE_TIMESTAMP_HPP
21
22#include <chrono>
23#include <cpprealm/internal/bridge/utils.hpp>
24
25namespace realm {
26 class Timestamp;
27}
28
29namespace realm::internal::bridge {
30 struct timestamp {
31 timestamp() = default;
32 timestamp(const timestamp& other) = default;
33 timestamp& operator=(const timestamp& other) = default;
34 timestamp(timestamp&& other) = default;
35 timestamp& operator=(timestamp&& other) = default;
36 ~timestamp() = default;
37 timestamp(const Timestamp&); //NOLINT(google-explicit-constructor)
38 operator Timestamp() const; //NOLINT(google-explicit-constructor)
39 operator std::chrono::time_point<std::chrono::system_clock>() const; //NOLINT(google-explicit-constructor)
40 timestamp(int64_t seconds, int32_t nanoseconds);
41 timestamp(const std::chrono::time_point<std::chrono::system_clock>& tp); //NOLINT(google-explicit-constructor)
42 [[nodiscard]] int64_t get_seconds() const noexcept;
43 [[nodiscard]] int32_t get_nanoseconds() const noexcept;
44 [[nodiscard]] std::chrono::time_point<std::chrono::system_clock, std::chrono::system_clock::duration>
45 get_time_point() const {
46 int64_t native_nano = get_seconds() * nanoseconds_per_second + get_nanoseconds();
47 auto duration = std::chrono::duration_cast<std::chrono::system_clock::duration>(std::chrono::duration<int64_t, std::nano>{native_nano});
48 return std::chrono::time_point<std::chrono::system_clock,
49 std::chrono::system_clock::duration>(duration);
50 }
51 private:
52 static constexpr int32_t nanoseconds_per_second = 1000000000;
53 int64_t m_seconds = 0;
54 int32_t m_nanoseconds = 0;
55 friend bool operator ==(const timestamp&, const timestamp&);
56 friend bool operator !=(const timestamp&, const timestamp&);
57 friend bool operator >(const timestamp&, const timestamp&);
58 friend bool operator <(const timestamp&, const timestamp&);
59 friend bool operator >=(const timestamp&, const timestamp&);
60 friend bool operator <=(const timestamp&, const timestamp&);
61 };
62
63 bool operator ==(const timestamp&, const timestamp&);
64 bool operator !=(const timestamp&, const timestamp&);
65 bool operator >(const timestamp&, const timestamp&);
66 bool operator <(const timestamp&, const timestamp&);
67 bool operator >=(const timestamp&, const timestamp&);
68 bool operator <=(const timestamp&, const timestamp&);
69}
70
71#endif //CPPREALM_BRIDGE_TIMESTAMP_HPP
Definition: timestamp.hpp:30