Line data Source code
1 : /*
2 : * Copyright (C) 2004-2026 Savoir-faire Linux Inc.
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 : */
17 : #pragma once
18 :
19 : #include <chrono>
20 : #include <cstdint>
21 :
22 : namespace jami {
23 :
24 : /**
25 : * Millisecond-resolution timestamp shared by the structures synchronized
26 : * between devices (ConvInfo, ConversationRequest, Contact, TrustRequest, ...).
27 : * Serialized with dual keys for backward compatibility: legacy keys carry
28 : * seconds (read by older devices), "*Ms" keys carry milliseconds.
29 : */
30 : using TimePoint = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>;
31 :
32 : inline TimePoint
33 786 : nowMs()
34 : {
35 786 : return std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
36 : }
37 :
38 : constexpr TimePoint
39 544 : timePointFromSeconds(int64_t seconds)
40 : {
41 544 : return TimePoint(std::chrono::seconds(seconds));
42 : }
43 :
44 : constexpr TimePoint
45 960 : timePointFromMilliseconds(int64_t milliseconds)
46 : {
47 960 : return TimePoint(std::chrono::milliseconds(milliseconds));
48 : }
49 :
50 : constexpr int64_t
51 10781 : toSecondsSinceEpoch(const TimePoint& t)
52 : {
53 10781 : return std::chrono::duration_cast<std::chrono::seconds>(t.time_since_epoch()).count();
54 : }
55 :
56 : constexpr int64_t
57 10237 : toMillisecondsSinceEpoch(const TimePoint& t)
58 : {
59 10237 : return t.time_since_epoch().count();
60 : }
61 :
62 : } // namespace jami
|