Sen API
Sen Libraries
Loading...
Searching...
No Matches
timestamp.h
Go to the documentation of this file.
1// === timestamp.h =====================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_BASE_TIMESTAMP_H
9#define SEN_CORE_BASE_TIMESTAMP_H
10
11// sen
14
15// std
16#include <string>
17
18namespace sen
19{
20
23
26{
27public:
29
30public: // defaults
31 constexpr TimeStamp() noexcept = default;
32 constexpr explicit TimeStamp(Duration timeSinceEpoch) noexcept: timeSinceEpoch_(timeSinceEpoch) {}
33 ~TimeStamp() noexcept = default;
34
35public: // special members
36 constexpr SEN_COPY_CONSTRUCT(TimeStamp) = default;
37 constexpr SEN_MOVE_CONSTRUCT(TimeStamp) = default;
38 SEN_COPY_ASSIGN(TimeStamp) = default;
39 SEN_MOVE_ASSIGN(TimeStamp) = default;
40
41public: // factories
42 static Result<TimeStamp, std::string> make(const std::string_view iso8601Time);
43
44public: // accessors
46 [[nodiscard]] constexpr Duration sinceEpoch() const noexcept { return timeSinceEpoch_; }
47
48public: // operators
50 constexpr bool operator==(const TimeStamp& other) const noexcept { return timeSinceEpoch_ == other.timeSinceEpoch_; }
51
53 constexpr bool operator!=(const TimeStamp& other) const noexcept { return timeSinceEpoch_ != other.timeSinceEpoch_; }
54
56 constexpr bool operator<(const TimeStamp& other) const noexcept { return timeSinceEpoch_ < other.timeSinceEpoch_; }
57
59 constexpr bool operator<=(const TimeStamp& other) const noexcept { return timeSinceEpoch_ <= other.timeSinceEpoch_; }
60
62 constexpr bool operator>(const TimeStamp& other) const noexcept { return timeSinceEpoch_ > other.timeSinceEpoch_; }
63
65 constexpr bool operator>=(const TimeStamp& other) const noexcept { return timeSinceEpoch_ >= other.timeSinceEpoch_; }
66
68 TimeStamp& operator+=(const Duration& other) noexcept
69 {
70 timeSinceEpoch_ += other;
71 return *this;
72 }
73
75 constexpr TimeStamp operator+(const Duration& other) const noexcept { return TimeStamp(timeSinceEpoch_ + other); }
76
78 TimeStamp& operator-=(const Duration& other) noexcept
79 {
80 timeSinceEpoch_ -= other;
81 return *this;
82 }
83
85 constexpr TimeStamp operator-(const Duration& other) const noexcept { return TimeStamp(timeSinceEpoch_ - other); }
86
88 constexpr Duration operator-(const TimeStamp& other) const noexcept { return sinceEpoch() - other.sinceEpoch(); }
89
90public:
92 [[nodiscard]] std::string toUtcString() const;
93
95 [[nodiscard]] std::string toLocalString() const;
96
97private:
98 Duration timeSinceEpoch_ {};
99};
100
101template <>
102struct ShouldBePassedByValue<TimeStamp>: std::true_type
103{
104};
105
106static_assert(sizeof(TimeStamp) == 8, "TimeStamp should be 8 bytes");
107static_assert(std::is_trivially_copyable_v<TimeStamp>);
108
110
111} // namespace sen
112
113#endif // SEN_CORE_BASE_TIMESTAMP_H
A time duration.
Definition duration.h:25
Base::ValueType ValueType
Definition duration.h:31
Result<T, E> is a template type that can be used to return and propagate errors. The intent is to rep...
Definition result.h:135
A point in time.
Definition timestamp.h:26
std::string toUtcString() const
UTC string representation of this time point.
TimeStamp & operator+=(const Duration &other) noexcept
Adds other to *this and returns *this.
Definition timestamp.h:68
constexpr Duration sinceEpoch() const noexcept
Time passed since 1 January 1970 UTC.
Definition timestamp.h:46
constexpr TimeStamp operator+(const Duration &other) const noexcept
Returns a TimeStamp of *this + other.
Definition timestamp.h:75
constexpr bool operator>=(const TimeStamp &other) const noexcept
True if *this is greater or equal than other.
Definition timestamp.h:65
constexpr bool operator==(const TimeStamp &other) const noexcept
Compares two Timestamps to determine if they are equal.
Definition timestamp.h:50
static Result< TimeStamp, std::string > make(const std::string_view iso8601Time)
constexpr TimeStamp() noexcept=default
~TimeStamp() noexcept=default
constexpr bool operator!=(const TimeStamp &other) const noexcept
Compares two Timestamps to determine if they are not equal.
Definition timestamp.h:53
TimeStamp & operator-=(const Duration &other) noexcept
Removes other from *this and returns *this.
Definition timestamp.h:78
constexpr Duration operator-(const TimeStamp &other) const noexcept
Returns a Duration of *this - other.
Definition timestamp.h:88
constexpr bool operator<=(const TimeStamp &other) const noexcept
True if *this is less or equal than other.
Definition timestamp.h:59
Duration::ValueType ValueType
Definition timestamp.h:28
constexpr TimeStamp operator-(const Duration &other) const noexcept
Returns a TimeStamp of *this - other.
Definition timestamp.h:85
constexpr bool operator>(const TimeStamp &other) const noexcept
True if *this is greater than other.
Definition timestamp.h:62
constexpr bool operator<(const TimeStamp &other) const noexcept
True if *this is less than other.
Definition timestamp.h:56
std::string toLocalString() const
Local string representation of this time point.
Definition assert.h:17
STL namespace.
Utility to indicate that your class wants to be passed by value in some of the library calls.
Definition class_helpers.h:34