Skip to content

Commit 45bc701

Browse files
Copy ctor, move ctor, <=> operator for datetime module.
1 parent a77b35a commit 45bc701

File tree

8 files changed

+103
-108
lines changed

8 files changed

+103
-108
lines changed

sdk/include/teiacare/sdk/datetime/date.hpp

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ class date
9797
{
9898
}
9999

100+
/*!
101+
* \brief Copy Constructor. Copy a tc::sdk::date instance to another one.
102+
*/
103+
constexpr date(const date&) noexcept = default;
104+
105+
/*!
106+
* \brief Move Constructor. Copy a tc::sdk::date instance to another one.
107+
*/
108+
constexpr date(date&&) noexcept = default;
109+
100110
/*!
101111
* \brief Assignment operator. Assign a tc::sdk::date instance to another one.
102112
*/
@@ -171,24 +181,11 @@ class date
171181
}
172182

173183
/*!
174-
* \brief Equality operator.
175-
* \param other the date to compare against.
176-
* \return true if the two date objects are the same.
177-
*/
178-
constexpr inline bool operator==(const date& other) const noexcept
179-
{
180-
return _ymd == other._ymd;
181-
}
182-
183-
/*!
184-
* \brief Inequality operator.
184+
* \brief Spaceship operator.
185185
* \param other the date to compare against.
186-
* \return true if the two date objects are the different.
186+
* \return std::strong_ordering comparing two date objects.
187187
*/
188-
constexpr inline bool operator!=(const date& other) const noexcept
189-
{
190-
return !operator==(other);
191-
}
188+
constexpr auto operator<=>(const date& other) const noexcept = default;
192189

193190
/*!
194191
* \brief Adds a time delta to the current date.
@@ -210,26 +207,6 @@ class date
210207
return tc::sdk::date{std::chrono::sys_days(_ymd) - delta.total_nanoseconds()};
211208
}
212209

213-
/*!
214-
* \brief Checks if the current date is less than another date.
215-
* \param other The date to compare against.
216-
* \return true if the current date is less, false otherwise.
217-
*/
218-
constexpr inline bool operator<(const date& other) const noexcept
219-
{
220-
return _ymd < other._ymd;
221-
}
222-
223-
/*!
224-
* \brief Checks if the current date is greater than another date.
225-
* \param other The date to compare against.
226-
* \return true if the current date is greater, false otherwise.
227-
*/
228-
constexpr inline bool operator>(const date& other) const noexcept
229-
{
230-
return _ymd > other._ymd;
231-
}
232-
233210
/*!
234211
* \brief Calculates the time difference between two dates.
235212
* \param other The date to subtract from the current date.

sdk/include/teiacare/sdk/datetime/datetime.hpp

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ class datetime
115115
{
116116
}
117117

118+
/*!
119+
* \brief Copy Constructor. Copy a tc::sdk::datetime instance to another one.
120+
*/
121+
constexpr datetime(const datetime&) noexcept = default;
122+
123+
/*!
124+
* \brief Move Constructor. Copy a tc::sdk::datetime instance to another one.
125+
*/
126+
constexpr datetime(datetime&&) noexcept = default;
127+
118128
/*!
119129
* \brief Assignment operator. Assign a tc::sdk::datetime instance to another one.
120130
*/
@@ -166,24 +176,11 @@ class datetime
166176
}
167177

168178
/*!
169-
* \brief Equality operator.
170-
* \param other the datetime to compare against.
171-
* \return true if the two datetime objects are the same.
172-
*/
173-
constexpr inline bool operator==(const datetime& other) const noexcept
174-
{
175-
return _tp == other._tp;
176-
}
177-
178-
/*!
179-
* \brief Inequality operator.
179+
* \brief Spaceship operator.
180180
* \param other the datetime to compare against.
181-
* \return true if the two datetime objects are the different.
181+
* \return std::strong_ordering comparing two datetime objects.
182182
*/
183-
constexpr inline bool operator!=(const datetime& other) const noexcept
184-
{
185-
return !operator==(other);
186-
}
183+
constexpr auto operator<=>(const datetime& other) const noexcept = default;
187184

188185
/*!
189186
* \brief Adds a timedelta to the current datetime.
@@ -205,26 +202,6 @@ class datetime
205202
return tc::sdk::datetime{_tp - delta.total_nanoseconds()};
206203
}
207204

208-
/*!
209-
* \brief Checks if the current datetime is less than another datetime.
210-
* \param other The datetime to compare against.
211-
* \return true if the current datetime is less, false otherwise.
212-
*/
213-
constexpr inline bool operator<(const datetime& other) const noexcept
214-
{
215-
return _tp < other._tp;
216-
}
217-
218-
/*!
219-
* \brief Checks if the current datetime is greater than another datetime.
220-
* \param other The datetime to compare against.
221-
* \return true if the current datetime is greater, false otherwise.
222-
*/
223-
constexpr inline bool operator>(const datetime& other) const noexcept
224-
{
225-
return _tp > other._tp;
226-
}
227-
228205
/*!
229206
* \brief Calculates the time difference between two datetime objects.
230207
* \param other The datetime to subtract from the current datetime.

sdk/include/teiacare/sdk/datetime/time.hpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ class time
5959
{
6060
}
6161

62+
/*!
63+
* \brief Copy Constructor. Copy a tc::sdk::time instance to another one.
64+
*/
65+
constexpr time(const time&) noexcept = default;
66+
67+
/*!
68+
* \brief Move Constructor. Copy a tc::sdk::time instance to another one.
69+
*/
70+
constexpr time(time&&) noexcept = default;
71+
6272
/*!
6373
* \brief Assignment operator. Assign a tc::sdk::time instance to another one.
6474
*/
@@ -161,6 +171,16 @@ class time
161171
return !operator==(other);
162172
}
163173

174+
/*!
175+
* \brief Spaceship operator.
176+
* \param other the time to compare against.
177+
* \return std::strong_ordering comparing two time objects.
178+
*/
179+
constexpr auto operator<=>(const time& other) const noexcept
180+
{
181+
return _hh_mm_ss.to_duration() <=> other._hh_mm_ss.to_duration();
182+
}
183+
164184
/*!
165185
* \brief Adds a timedelta to the current time.
166186
* \param delta The tc::sdk::timedelta to add.
@@ -181,26 +201,6 @@ class time
181201
return tc::sdk::time{_hh_mm_ss.to_duration() - delta.total_nanoseconds()};
182202
}
183203

184-
/*!
185-
* \brief Checks if the current time is less than another time.
186-
* \param other The time to compare against.
187-
* \return true if the current time is less, false otherwise.
188-
*/
189-
constexpr inline bool operator<(const time& other) const noexcept
190-
{
191-
return _hh_mm_ss.to_duration() < other._hh_mm_ss.to_duration();
192-
}
193-
194-
/*!
195-
* \brief Checks if the current time is greater than another time.
196-
* \param other The time to compare against.
197-
* \return true if the current time is greater, false otherwise.
198-
*/
199-
constexpr inline bool operator>(const time& other) const noexcept
200-
{
201-
return _hh_mm_ss.to_duration() > other._hh_mm_ss.to_duration();
202-
}
203-
204204
/*!
205205
* \brief Calculates the time difference between two time objects.
206206
* \param other The time to subtract from the current time.

sdk/include/teiacare/sdk/datetime/timedelta.hpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ class timedelta
7272
{
7373
}
7474

75+
/*!
76+
* \brief Copy Constructor. Copy a tc::sdk::timedelta instance to another one.
77+
*/
78+
constexpr timedelta(const timedelta&) noexcept = default;
79+
80+
/*!
81+
* \brief Move Constructor. Copy a tc::sdk::timedelta instance to another one.
82+
*/
83+
constexpr timedelta(timedelta&&) noexcept = default;
84+
7585
/*!
7686
* \brief Assignment operator. Assign a tc::sdk::timedelta instance to another one.
7787
*/
@@ -232,24 +242,11 @@ class timedelta
232242
}
233243

234244
/*!
235-
* \brief Equality operator.
236-
* \param other the timedelta to compare against.
237-
* \return true if the two timedelta objects are the same.
238-
*/
239-
constexpr inline bool operator==(const timedelta& other) const noexcept
240-
{
241-
return _duration == other._duration;
242-
}
243-
244-
/*!
245-
* \brief Inequality operator.
245+
* \brief Spaceship operator.
246246
* \param other the timedelta to compare against.
247-
* \return true if the two timedelta objects are the different.
247+
* \return std::strong_ordering comparing two timedelta objects.
248248
*/
249-
constexpr inline bool operator!=(const timedelta& other) const noexcept
250-
{
251-
return !operator==(other);
252-
}
249+
constexpr auto operator<=>(const timedelta& other) const noexcept = default;
253250

254251
/*!
255252
* \brief Addition operator.

sdk/tests/src/test_datetime_date.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,20 @@ TEST_F(test_datetime_date, operator_comparison)
226226
const auto d1 = tc::sdk::date(2024y, std::chrono::January, 1d); // 2024 Jan 1st
227227
const auto d2 = tc::sdk::date(2023y, std::chrono::December, 31d); // 2023 Dec 31th
228228
const auto d3 = tc::sdk::date(2023y, std::chrono::December, 1d); // 2023 Dec 1st
229+
const auto d4 = tc::sdk::date(2023y, std::chrono::December, 1d); // 2023 Dec 1st
229230

230231
EXPECT_TRUE(d1 > d2);
231232
EXPECT_TRUE(d2 > d3);
233+
EXPECT_TRUE(d1 >= d2);
234+
EXPECT_TRUE(d2 >= d3);
232235

233236
EXPECT_TRUE(d3 < d1);
234237
EXPECT_TRUE(d3 < d2);
238+
EXPECT_TRUE(d3 <= d1);
239+
EXPECT_TRUE(d3 <= d2);
240+
241+
EXPECT_TRUE(d3 <= d4);
242+
EXPECT_TRUE(d3 >= d4);
235243
}
236244

237245
TEST_F(test_datetime_date, ostream)

sdk/tests/src/test_datetime_datetime.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ TEST_F(test_datetime_datetime, operator_minus_timedelta)
180180
EXPECT_EQ(dt2 - delta, dt1);
181181
EXPECT_EQ(dt2, tc::sdk::datetime(2024y, std::chrono::May, 1d, 20h, 33min, 46s, 2ms) - delta);
182182
}
183+
183184
TEST_F(test_datetime_datetime, operator_minus)
184185
{
185186
const auto dt1 = tc::sdk::datetime{tc::sdk::sys_time_point(1'714'588'424'000ms)}; // 01 May 2024 18:33:44.000 UTC
@@ -194,12 +195,20 @@ TEST_F(test_datetime_datetime, operator_comparison)
194195
const auto dt1 = tc::sdk::datetime(2024y, std::chrono::May, 1d, 18h, 33min, 44s, 555ms, 666us); // 01 May 2024 18:33:44.555666 UTC
195196
const auto dt2 = tc::sdk::datetime(2024y, std::chrono::May, 1d, 18h, 33min, 44s, 555ms); // 01 May 2024 18:33:44.555000 UTC
196197
const auto dt3 = tc::sdk::datetime(2024y, std::chrono::May, 1d, 18h, 33min, 44s); // 01 May 2024 18:33:44.000000 UTC
198+
const auto dt4 = tc::sdk::datetime(2024y, std::chrono::May, 1d, 18h, 33min, 44s); // 01 May 2024 18:33:44.000000 UTC
197199

198200
EXPECT_TRUE(dt1 > dt2);
199201
EXPECT_TRUE(dt2 > dt3);
202+
EXPECT_TRUE(dt1 >= dt2);
203+
EXPECT_TRUE(dt2 >= dt3);
200204

201205
EXPECT_TRUE(dt3 < dt1);
202206
EXPECT_TRUE(dt3 < dt2);
207+
EXPECT_TRUE(dt3 <= dt1);
208+
EXPECT_TRUE(dt3 <= dt2);
209+
210+
EXPECT_TRUE(dt3 <= dt4);
211+
EXPECT_TRUE(dt3 >= dt4);
203212
}
204213

205214
TEST_F(test_datetime_datetime, ostream)

sdk/tests/src/test_datetime_time.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,20 @@ TEST_F(test_datetime_time, operator_comparison)
218218
const auto t1 = tc::sdk::time(3'662s); // 01:01:02
219219
const auto t2 = tc::sdk::time(1h, 1min, 1s); // 01:01:01
220220
const auto t3 = tc::sdk::time(std::chrono::days(1)); // 00:00:00
221+
const auto t4 = tc::sdk::time(std::chrono::days(1)); // 00:00:00
221222

222223
EXPECT_TRUE(t1 > t2);
223224
EXPECT_TRUE(t2 > t3);
225+
EXPECT_TRUE(t1 >= t2);
226+
EXPECT_TRUE(t2 >= t3);
224227

225228
EXPECT_TRUE(t3 < t1);
226229
EXPECT_TRUE(t3 < t2);
230+
EXPECT_TRUE(t3 <= t1);
231+
EXPECT_TRUE(t3 <= t2);
232+
233+
EXPECT_TRUE(t3 <= t4);
234+
EXPECT_TRUE(t3 >= t4);
227235
}
228236

229237
TEST_F(test_datetime_time, ostream)

sdk/tests/src/test_datetime_timedelta.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,25 @@ TEST_F(test_datetime_timedelta, operator_div)
156156
EXPECT_EQ(t1 / divider, t3);
157157
}
158158

159+
TEST_F(test_datetime_timedelta, operator_comparison)
160+
{
161+
const auto t1 = tc::sdk::timedelta(1h);
162+
const auto t2 = tc::sdk::timedelta(12min);
163+
const auto t3 = tc::sdk::timedelta(720'000ms);
164+
const auto t4 = tc::sdk::timedelta(720s);
165+
166+
EXPECT_TRUE(t1 > t2);
167+
EXPECT_TRUE(t1 >= t2);
168+
EXPECT_TRUE(t2 >= t3);
169+
170+
EXPECT_TRUE(t3 < t1);
171+
EXPECT_TRUE(t3 <= t1);
172+
EXPECT_TRUE(t3 <= t2);
173+
174+
EXPECT_TRUE(t3 <= t4);
175+
EXPECT_TRUE(t3 >= t4);
176+
}
177+
159178
TEST_F(test_datetime_timedelta, ostream)
160179
{
161180
{

0 commit comments

Comments
 (0)