1 | // |
---|---|
2 | // Clock.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: DateTime |
6 | // Module: Clock |
7 | // |
8 | // Copyright (c) 2013, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Clock.h" |
16 | #include "Poco/Exception.h" |
17 | #include "Poco/Timestamp.h" |
18 | #include <chrono> |
19 | #include <algorithm> |
20 | #undef min |
21 | #undef max |
22 | #include <limits> |
23 | |
24 | |
25 | #ifndef POCO_HAVE_CLOCK_GETTIME |
26 | #if (defined(_POSIX_TIMERS) && defined(CLOCK_REALTIME)) || defined(POCO_VXWORKS) || defined(__QNX__) |
27 | #ifndef __APPLE__ // See GitHub issue #1453 - not available before Mac OS 10.12/iOS 10 |
28 | #define POCO_HAVE_CLOCK_GETTIME |
29 | #endif |
30 | #endif |
31 | #endif |
32 | |
33 | |
34 | namespace Poco { |
35 | |
36 | |
37 | const Clock::ClockVal Clock::CLOCKVAL_MIN = std::numeric_limits<Clock::ClockVal>::min(); |
38 | const Clock::ClockVal Clock::CLOCKVAL_MAX = std::numeric_limits<Clock::ClockVal>::max(); |
39 | |
40 | |
41 | Clock::Clock() |
42 | { |
43 | update(); |
44 | } |
45 | |
46 | |
47 | Clock::Clock(ClockVal tv) |
48 | { |
49 | _clock = tv; |
50 | } |
51 | |
52 | |
53 | Clock::Clock(const Clock& other) |
54 | { |
55 | _clock = other._clock; |
56 | } |
57 | |
58 | |
59 | Clock::~Clock() |
60 | { |
61 | } |
62 | |
63 | |
64 | Clock& Clock::operator = (const Clock& other) |
65 | { |
66 | _clock = other._clock; |
67 | return *this; |
68 | } |
69 | |
70 | |
71 | Clock& Clock::operator = (ClockVal tv) |
72 | { |
73 | _clock = tv; |
74 | return *this; |
75 | } |
76 | |
77 | |
78 | void Clock::swap(Clock& timestamp) |
79 | { |
80 | std::swap(_clock, timestamp._clock); |
81 | } |
82 | |
83 | |
84 | void Clock::update() |
85 | { |
86 | _clock = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
87 | } |
88 | |
89 | |
90 | Clock::ClockDiff Clock::accuracy() |
91 | { |
92 | ClockVal acc = static_cast<ClockVal>(std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(std::chrono::steady_clock::duration(1)).count()); |
93 | return acc > 0 ? acc : 1; |
94 | } |
95 | |
96 | |
97 | bool Clock::monotonic() |
98 | { |
99 | return std::chrono::steady_clock::is_steady; |
100 | } |
101 | |
102 | |
103 | } // namespace Poco |
104 |