Skip to content

Commit 0449f8f

Browse files
Applied suggestions
Signed-off-by: Eugenio Collado <eugeniocollado@eprosima.com>
1 parent ffd1418 commit 0449f8f

3 files changed

Lines changed: 81 additions & 12 deletions

File tree

cpp_utils/include/cpp_utils/time/time_utils.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ namespace utils {
3030
using Duration_ms = uint32_t;
3131

3232
/**
33-
* Type used to represent time points
33+
* Type used to fix the clock to the system clock
3434
*/
3535
using Timeclock = std::chrono::system_clock;
36+
37+
/**
38+
* Type used to represent time points
39+
*/
3640
using Timestamp = std::chrono::time_point<Timeclock>;
3741

3842
/**

cpp_utils/src/cpp/time/time_utils.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,12 @@ Timestamp now() noexcept
4141

4242
Timestamp the_end_of_time() noexcept
4343
{
44-
#if _EPROSIMA_WINDOWS_PLATFORM // In Windows std::gmtime does not support negative values nor values greater than 2^32
45-
return Timeclock::from_time_t(std::numeric_limits<long>::max());
46-
#else
4744
return Timestamp::max();
48-
#endif // if _EPROSIMA_WINDOWS_PLATFORM
4945
}
5046

5147
Timestamp the_beginning_of_time() noexcept
5248
{
53-
#if _EPROSIMA_WINDOWS_PLATFORM // In Windows std::gmtime does not support negative values nor values greater than 2^32
54-
return Timeclock::from_time_t(0);
55-
#else
5649
return Timestamp::min();
57-
#endif // if _EPROSIMA_WINDOWS_PLATFORM
5850
}
5951

6052
Timestamp date_to_timestamp(
@@ -106,8 +98,14 @@ std::string timestamp_to_string(
10698
time_t duration_seconds = std::chrono::duration_cast<std::chrono::seconds>(timestamp.time_since_epoch()).count();
10799

108100
#if _EPROSIMA_WINDOWS_PLATFORM // In Windows std::gmtime does not support negative values nor values greater than 2^32
109-
time_t max_value = std::numeric_limits<long>::max();
110-
duration_seconds = std::max((time_t) 0, std::min(max_value, duration_seconds));
101+
time_t max_value = std::numeric_limits<int32_t>::max();
102+
if (0 > duration_seconds || duration_seconds > max_value)
103+
{
104+
EPROSIMA_LOG_WARNING(TIME_UTILS,
105+
"Timestamp value: " << duration_seconds << " is out of range for Windows, clamping to 0 and " <<
106+
max_value);
107+
duration_seconds = std::max((time_t) 0, std::min(max_value, duration_seconds));
108+
}
111109
#endif // if _EPROSIMA_WINDOWS_PLATFORM
112110

113111
std::tm* tm = nullptr;

cpp_utils/test/unittest/time/time_utils_test.cpp

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ using namespace eprosima::utils;
3131
* - now
3232
* - now with alternative format
3333
* - old time
34+
* - date before 1970
3435
* - the beginning of time
3536
* - future time
37+
* - date after 2038
3638
* - the end of time
3739
* - some time today
38-
* - wrong date and time
3940
*/
4041
TEST(time_utils_test, timestamp_to_string_to_timestamp)
4142
{
@@ -115,6 +116,39 @@ TEST(time_utils_test, timestamp_to_string_to_timestamp)
115116
ASSERT_EQ(timestamp_to_string(old_time_from_str), expected_string_os.str());
116117
}
117118

119+
// date before 1970
120+
{
121+
Timestamp old_time = date_to_timestamp(1959u, 7u, 20u, 6u, 39u, 42u);
122+
std::string old_time_str = timestamp_to_string(old_time);
123+
124+
std::ostringstream expected_string_os;
125+
#if _EPROSIMA_WINDOWS_PLATFORM
126+
expected_string_os
127+
<< 1970
128+
<< "-" << "01"
129+
<< "-" << "01"
130+
<< "_" << "00"
131+
<< "-" << "00"
132+
<< "-" << "00";
133+
#else
134+
expected_string_os
135+
<< 1959
136+
<< "-" << "07"
137+
<< "-" << 20
138+
<< "_" << "06"
139+
<< "-" << 39
140+
<< "-" << 42;
141+
#endif // _EPROSIMA_WINDOWS_PLATFORM
142+
143+
// Test timestamp_to_string
144+
ASSERT_EQ(old_time_str, expected_string_os.str());
145+
146+
// Test string_to_timestamp
147+
Timestamp old_time_from_str = string_to_timestamp(old_time_str);
148+
// NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
149+
ASSERT_EQ(timestamp_to_string(old_time_from_str), expected_string_os.str());
150+
}
151+
118152
// the begininng of time
119153
{
120154
Timestamp beginning_time = the_beginning_of_time();
@@ -172,6 +206,39 @@ TEST(time_utils_test, timestamp_to_string_to_timestamp)
172206
ASSERT_EQ(timestamp_to_string(future_time_from_str), expected_string_os.str());
173207
}
174208

209+
// date after 2038
210+
{
211+
Timestamp future_time = date_to_timestamp(2049u, 5u, 22u);
212+
std::string future_time_str = timestamp_to_string(future_time);
213+
214+
std::ostringstream expected_string_os;
215+
#if _EPROSIMA_WINDOWS_PLATFORM
216+
expected_string_os
217+
<< 2038
218+
<< "-" << "01"
219+
<< "-" << 19
220+
<< "_" << "03"
221+
<< "-" << "14"
222+
<< "-" << "07";
223+
#else
224+
expected_string_os
225+
<< 2049
226+
<< "-" << "05"
227+
<< "-" << 22
228+
<< "_" << "00"
229+
<< "-" << "00"
230+
<< "-" << "00";
231+
#endif // _EPROSIMA_WINDOWS_PLATFORM
232+
233+
// Test timestamp_to_string
234+
ASSERT_EQ(future_time_str, expected_string_os.str());
235+
236+
// Test string_to_timestamp
237+
Timestamp future_time_from_str = string_to_timestamp(future_time_str);
238+
// NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
239+
ASSERT_EQ(timestamp_to_string(future_time_from_str), expected_string_os.str());
240+
}
241+
175242
// the end of time
176243
{
177244
Timestamp end_time = the_end_of_time();

0 commit comments

Comments
 (0)