open62541++ 0.13.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
DateTime.h
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <cstdint>
5#include <ratio>
6#include <string>
7#include <string_view>
8
11
12namespace opcua {
13
14/**
15 * UA_DateTime wrapper class.
16 *
17 * An instance in time. A DateTime value is encoded as a 64-bit signed integer which represents the
18 * number of 100 nanosecond intervals since January 1, 1601 (UTC).
19 *
20 * @see https://reference.opcfoundation.org/Core/Part6/v105/docs/5.2.2.5
21 * @ingroup Wrapper
22 */
23class DateTime : public TypeWrapper<UA_DateTime, UA_TYPES_DATETIME> {
24public:
25 using DefaultClock = std::chrono::system_clock;
26 using UaDuration = std::chrono::duration<int64_t, std::ratio<1, 10'000'000>>;
27
28 using TypeWrapper::TypeWrapper; // inherit constructors
29
30 template <typename Clock, typename Duration>
31 DateTime(std::chrono::time_point<Clock, Duration> timePoint) // NOLINT, implicit wanted
32 : DateTime(fromTimePoint(timePoint)) {}
33
34 /// Get current DateTime.
35 static DateTime now() noexcept {
36 return DateTime(UA_DateTime_now()); // NOLINT
37 }
38
39 /// Get DateTime from std::chrono::time_point.
40 template <typename Clock, typename Duration>
41 static DateTime fromTimePoint(std::chrono::time_point<Clock, Duration> timePoint) {
42 return DateTime(
43 int64_t{UA_DATETIME_UNIX_EPOCH} +
44 std::chrono::duration_cast<UaDuration>(timePoint.time_since_epoch()).count()
45 );
46 }
47
48 /// Get DateTime from Unix time.
49 static DateTime fromUnixTime(int64_t unixTime) noexcept {
50 return DateTime(UA_DateTime_fromUnixTime(unixTime)); // NOLINT
51 }
52
53 /// Offset of local time to UTC.
54 static int64_t localTimeUtcOffset() noexcept {
56 }
57
58 /// Convert to std::chrono::time_point.
59 template <typename Clock = DefaultClock, typename Duration = UaDuration>
60 std::chrono::time_point<Clock, Duration> toTimePoint() const {
61 const std::chrono::time_point<Clock, Duration> unixEpoch{};
63 return unixEpoch;
64 }
65 const auto sinceEpoch = UaDuration(get() - UA_DATETIME_UNIX_EPOCH);
66 return unixEpoch + std::chrono::duration_cast<Duration>(sinceEpoch);
67 }
68
69 /// Convert to Unix time (number of seconds since January 1, 1970 UTC).
70 int64_t toUnixTime() const noexcept {
72 return 0;
73 }
74 return UA_DateTime_toUnixTime(get());
75 }
76
77 /// Convert to UA_DateTimeStruct.
78 UA_DateTimeStruct toStruct() const noexcept {
79 return UA_DateTime_toStruct(get());
80 }
81
82 /// Get DateTime value as 100 nanosecond intervals since January 1, 1601 (UTC).
83 int64_t get() const noexcept {
84 return *handle();
85 }
86
87 /// Convert to string with given format (same format codes as strftime).
88 /// @see https://en.cppreference.com/w/cpp/chrono/c/strftime
89 std::string format(std::string_view format, bool localtime = false) const;
90};
91
92} // namespace opcua
UA_DateTime wrapper class.
Definition DateTime.h:23
int64_t get() const noexcept
Get DateTime value as 100 nanosecond intervals since January 1, 1601 (UTC).
Definition DateTime.h:83
UA_DateTimeStruct toStruct() const noexcept
Convert to UA_DateTimeStruct.
Definition DateTime.h:78
static DateTime fromUnixTime(int64_t unixTime) noexcept
Get DateTime from Unix time.
Definition DateTime.h:49
static DateTime now() noexcept
Get current DateTime.
Definition DateTime.h:35
std::chrono::duration< int64_t, std::ratio< 1, 10 '000 '000 > > UaDuration
Definition DateTime.h:26
static DateTime fromTimePoint(std::chrono::time_point< Clock, Duration > timePoint)
Get DateTime from std::chrono::time_point.
Definition DateTime.h:41
int64_t toUnixTime() const noexcept
Convert to Unix time (number of seconds since January 1, 1970 UTC).
Definition DateTime.h:70
static int64_t localTimeUtcOffset() noexcept
Offset of local time to UTC.
Definition DateTime.h:54
DateTime(std::chrono::time_point< Clock, Duration > timePoint)
Definition DateTime.h:31
std::string format(std::string_view format, bool localtime=false) const
Convert to string with given format (same format codes as strftime).
std::chrono::time_point< Clock, Duration > toTimePoint() const
Convert to std::chrono::time_point.
Definition DateTime.h:60
std::chrono::system_clock DefaultClock
Definition DateTime.h:25
Template base class to wrap UA_* type objects.
Definition TypeWrapper.h:26
constexpr TypeWrapper()=default
constexpr T * handle() noexcept
Return pointer to native object.
Definition Wrapper.h:65
#define UA_DATETIME_UNIX_EPOCH
UA_DateTimeStruct UA_DateTime_toStruct(UA_DateTime t)
UA_DateTime UA_DateTime_now(void)
UA_Int64 UA_DateTime_localTimeUtcOffset(void)