-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathClock.h
79 lines (69 loc) · 2.62 KB
/
Clock.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "TimeTypes.h"
#include <string>
namespace Aws
{
namespace IoTFleetWise
{
/**
* @brief Clock API. Offers the time based on multiple clocks.
*/
class Clock
{
public:
/**
* @brief Computes the timestamp since Unix epoch from the system clock
*
* This should not be used for measuring intervals, elapsed time, duration, etc. Use
* monotonicTimeSinceEpochMs() instead.
*
* @return timestamp in milliseconds
*/
virtual Timestamp systemTimeSinceEpochMs() const = 0;
/**
* @brief Computes the timestamp since epoch from a monotonic clock
*
* Note that epoch in this case is not the Unix timestamp epoch, but rather it can be
* any arbitrary moment (e.g. since the system started)
*
* @return timestamp in milliseconds
*/
virtual Timestamp monotonicTimeSinceEpochMs() const = 0;
/**
* @brief Computes the timestamp since epoch from multiple clocks
*
* Note that epoch in this case is not necessarily the Unix timestamp epoch. For a monotonic clock,
* for example, it can be any arbitrary moment (e.g. since the system started).
*
* @return a TimePoint struct containing the time based on different clocks.
* WARNING: Since there is no way to atomically get the clock from multiple sources, those times could be
* slightly out of sync, especially if the thread is interrupted between different clock calls.
*/
virtual TimePoint timeSinceEpoch() const = 0;
/**
* @brief Convert the current time to ISO 8601 format.
* @return current time in a string format
*/
virtual std::string currentTimeToIsoString() const = 0;
/**
* @brief virtual destructor
*/
virtual ~Clock() = default;
};
/**
* @brief Computes the monotonic timestamp corresponding to the given system timestamp
*
* This is intended to be used when the timestamp is extracted from the data as system time only.
* But when calculating intervals we need to use a monotonic clock to be resilient to system time changes.
* So this function can be used to calculate a monotonic time based on the current time.
*
* Please note that this is not always possible. In situations where the calculated monotonic time would
* be negative, TimePoint {0, 0} is returned.
*
* @return a TimePoint struct containing the given system time and the corresponding monotonic time
*/
TimePoint timePointFromSystemTime( const TimePoint &currTime, Timestamp systemTimeMs );
} // namespace IoTFleetWise
} // namespace Aws