Safeheron-SGX-Native-Development-Framework v1.2.0
Loading...
Searching...
No Matches
ssgx_utils_t_time.h
Go to the documentation of this file.
1#ifndef SAFEHERON_SGX_TRUSTED_TIME_H
2#define SAFEHERON_SGX_TRUSTED_TIME_H
3
4#include <cstdint>
5#include <ctime>
6#include <string>
7
8namespace ssgx {
9namespace utils_t {
10
15class TimeSpan {
16 public:
20 TimeSpan() noexcept;
26 explicit TimeSpan(time_t span) noexcept;
27
36 TimeSpan(int64_t num_days, int num_hours, int num_mins, int num_secs) noexcept;
37
43 [[nodiscard]] time_t GetTimeSpan() const noexcept;
44
50 [[nodiscard]] int64_t GetDays() const noexcept;
51
57 [[nodiscard]] int64_t GetTotalHours() const noexcept;
58
64 [[nodiscard]] int64_t GetTotalMinutes() const noexcept;
65
71 [[nodiscard]] int64_t GetTotalSeconds() const noexcept;
72
78 [[nodiscard]] int64_t GetHours() const noexcept;
79
85 [[nodiscard]] int64_t GetMinutes() const noexcept;
86
92 [[nodiscard]] int64_t GetSeconds() const noexcept;
93
94 // Overloaded operators for arithmetic and comparison
95 TimeSpan operator+(TimeSpan span) const noexcept;
96 TimeSpan operator-(TimeSpan span) const noexcept;
97 TimeSpan& operator+=(TimeSpan span) noexcept;
98 TimeSpan& operator-=(TimeSpan span) noexcept;
99 bool operator==(TimeSpan span) const noexcept;
100 bool operator!=(TimeSpan span) const noexcept;
101 bool operator<(TimeSpan span) const noexcept;
102 bool operator>(TimeSpan span) const noexcept;
103 bool operator<=(TimeSpan span) const noexcept;
104 bool operator>=(TimeSpan span) const noexcept;
105
106 private:
107 time_t time_span_;
108};
109
114class DateTime {
115 public:
123 static DateTime Now();
124
131 static DateTime FromFormatTime(const std::string& formattedTime);
132
136 DateTime() noexcept;
137
143 explicit DateTime(time_t timestamp) noexcept;
144
155 DateTime(int num_year, int num_month, int num_day, int num_hour, int num_min, int num_sec);
156
157 // Assignment and arithmetic operators
158 DateTime& operator=(time_t timestamp) noexcept;
159 DateTime& operator+=(TimeSpan span) noexcept;
160 DateTime& operator-=(TimeSpan span) noexcept;
161 TimeSpan operator-(DateTime time) const noexcept;
162 DateTime operator-(TimeSpan span) const noexcept;
163 DateTime operator+(TimeSpan span) const noexcept;
164
165 // Comparison
166 bool operator==(DateTime time) const noexcept;
167 bool operator!=(DateTime time) const noexcept;
168 bool operator<(DateTime time) const noexcept;
169 bool operator>(DateTime time) const noexcept;
170 bool operator<=(DateTime time) const noexcept;
171 bool operator>=(DateTime time) const noexcept;
172
173 // Accessors for time components
174 [[nodiscard]] time_t GetTimestamp() const noexcept;
175 [[nodiscard]] int GetYear() const noexcept;
176 [[nodiscard]] int GetMonth() const noexcept;
177 [[nodiscard]] int GetDay() const noexcept;
178 [[nodiscard]] int GetHour() const noexcept;
179 [[nodiscard]] int GetMinute() const noexcept;
180 [[nodiscard]] int GetSecond() const noexcept;
181 [[nodiscard]] int GetDayOfWeek() const noexcept;
182
189 [[nodiscard]] std::string ToFormatTime() const;
190
191 private:
192 time_t timestamp_; // Internal representation of the timestamp
193};
194
200 public:
208 static int64_t NowInNanoseconds();
209
217 static int64_t NowInMilliseconds();
218};
219
220} // namespace utils_t
221} // namespace ssgx
222
223#endif // SAFEHERON_SGX_TRUSTED_TIME_H
Represents a specific point in time. Supports UTC/GMT time only.
Definition ssgx_utils_t_time.h:114
static DateTime FromFormatTime(const std::string &formattedTime)
Constructs a DateTime object from a formatted string. Supports the format "YYYY-MM-DD HH:MM:SS".
DateTime() noexcept
Default constructor, initializes the time to epoch (1970-01-01 00:00:00).
static DateTime Now()
Returns the current system time as a DateTime object.
Provides high-resolution time utilities for nanoseconds and milliseconds.
Definition ssgx_utils_t_time.h:199
static int64_t NowInMilliseconds()
Returns the current time in milliseconds since the UNIX epoch.
static int64_t NowInNanoseconds()
Returns the current time in nanoseconds since the UNIX epoch.
Represents a span of time, providing utility functions for calculating days, hours,...
Definition ssgx_utils_t_time.h:15
int64_t GetTotalMinutes() const noexcept
Returns the total number of minutes in the time span.
int64_t GetTotalHours() const noexcept
Returns the total number of hours in the time span.
int64_t GetTotalSeconds() const noexcept
Returns the total number of seconds in the time span.
int64_t GetSeconds() const noexcept
Returns the remaining seconds after extracting minutes.
TimeSpan() noexcept
Default constructor, initializes the time span to 0.
int64_t GetMinutes() const noexcept
Returns the remaining minutes after extracting hours.
int64_t GetHours() const noexcept
Returns the remaining hours after extracting days.
time_t GetTimeSpan() const noexcept
Returns the total time span in seconds.
int64_t GetDays() const noexcept
Returns the number of days in the time span.
Definition ssgx_attestation_t.h:6