2525using namespace eprosima ::utils;
2626
2727/* *
28- * Test function timestamp_to_string with a time stamp.
28+ * Test function timestamp_to_string with a time stamp, as well the inverse conversion string_to_timestamp .
2929 *
3030 * CASES:
3131 * - now
32+ * - now with alternative format
3233 * - old time
3334 * - future time
35+ * - some time today
3436 */
35- TEST (time_utils_test, timestamp_to_string )
37+ TEST (time_utils_test, timestamp_to_string_to_timestamp )
3638{
3739 // now
3840 {
@@ -51,7 +53,40 @@ TEST(time_utils_test, timestamp_to_string)
5153 << " -" << number_trailing_zeros_format (local_tm.tm_min , 2 )
5254 << " -" << number_trailing_zeros_format (local_tm.tm_sec , 2 );
5355
56+ // Test timestamp_to_string
5457 ASSERT_EQ (now_time_str, expected_string_os.str ());
58+
59+ // Test string_to_timestamp
60+ Timestamp now_time_from_str = string_to_timestamp (now_time_str);
61+ // NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
62+ ASSERT_EQ (timestamp_to_string (now_time_from_str), expected_string_os.str ());
63+ }
64+
65+ // now with alternative format
66+ {
67+ Timestamp now_time = now ();
68+ std::string format = " %S-%M-%H___%d-%m-%Y" ;
69+ std::string now_time_str = timestamp_to_string (now_time, format);
70+
71+ time_t time = std::chrono::system_clock::to_time_t (now_time);
72+ std::tm local_tm = *gmtime (&time);
73+
74+ std::ostringstream expected_string_os;
75+ expected_string_os
76+ << number_trailing_zeros_format (local_tm.tm_sec , 2 )
77+ << " -" << number_trailing_zeros_format (local_tm.tm_min , 2 )
78+ << " -" << number_trailing_zeros_format (local_tm.tm_hour , 2 )
79+ << " ___" << number_trailing_zeros_format (local_tm.tm_mday , 2 )
80+ << " -" << number_trailing_zeros_format (local_tm.tm_mon + 1 , 2 )
81+ << " -" << number_trailing_zeros_format (local_tm.tm_year + 1900 , 4 );
82+
83+ // Test timestamp_to_string
84+ ASSERT_EQ (now_time_str, expected_string_os.str ());
85+
86+ // Test string_to_timestamp
87+ Timestamp now_time_from_str = string_to_timestamp (now_time_str, format);
88+ // NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
89+ ASSERT_EQ (timestamp_to_string (now_time_from_str, format), expected_string_os.str ());
5590 }
5691
5792 // old time
@@ -68,7 +103,13 @@ TEST(time_utils_test, timestamp_to_string)
68103 << " -" << 39
69104 << " -" << 42 ;
70105
106+ // Test timestamp_to_string
71107 ASSERT_EQ (old_time_str, expected_string_os.str ());
108+
109+ // Test string_to_timestamp
110+ Timestamp old_time_from_str = string_to_timestamp (old_time_str);
111+ // NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
112+ ASSERT_EQ (timestamp_to_string (old_time_from_str), expected_string_os.str ());
72113 }
73114
74115 // future time
@@ -85,19 +126,55 @@ TEST(time_utils_test, timestamp_to_string)
85126 << " -" << " 00"
86127 << " -" << " 00" ;
87128
129+ // Test timestamp_to_string
88130 ASSERT_EQ (future_time_str, expected_string_os.str ());
131+
132+ // Test string_to_timestamp
133+ Timestamp future_time_from_str = string_to_timestamp (future_time_str);
134+ // NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
135+ ASSERT_EQ (timestamp_to_string (future_time_from_str), expected_string_os.str ());
136+ }
137+
138+ // some time today
139+ {
140+ Timestamp some_time_today = time_to_timestamp (13u , 13u , 13u );
141+ std::string some_time_today_str = timestamp_to_string (some_time_today);
142+
143+ // Get current timestamp and use its date to construct expected string
144+ Timestamp now_ts = now ();
145+ std::string now_time_str = timestamp_to_string (now_ts);
146+ time_t now_time = std::chrono::system_clock::to_time_t (now_ts);
147+ std::tm now_tm = *gmtime (&now_time);
148+
149+ std::ostringstream expected_string_os;
150+ expected_string_os
151+ << number_trailing_zeros_format (now_tm.tm_year + 1900 , 4 )
152+ << " -" << number_trailing_zeros_format (now_tm.tm_mon + 1 , 2 )
153+ << " -" << number_trailing_zeros_format (now_tm.tm_mday , 2 )
154+ << " _" << " 13"
155+ << " -" << " 13"
156+ << " -" << " 13" ;
157+
158+ // Test timestamp_to_string
159+ ASSERT_EQ (some_time_today_str, expected_string_os.str ());
160+
161+ // Test string_to_timestamp
162+ Timestamp some_time_today_from_str = string_to_timestamp (some_time_today_str);
163+ // NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
164+ ASSERT_EQ (timestamp_to_string (some_time_today_from_str), expected_string_os.str ());
89165 }
90166}
91167
92168/* *
93- * Test function timestamp_to_string with a time stamp with local time.
169+ * Test function timestamp_to_string with a time stamp with local time, as well the inverse conversion string_to_timestamp .
94170 *
95171 * NOTE: only case is now because the local time zone depends on the part of the year and the test becomes a mess.
96172 */
97- TEST (time_utils_test, timestamp_to_string_local )
173+ TEST (time_utils_test, timestamp_to_string_to_timestamp_local )
98174{
99175 Timestamp now_time = now ();
100- std::string now_time_str = timestamp_to_string (now_time, " %Y-%m-%d_%H-%M-%S" , true );
176+ std::string format = " %Y-%m-%d_%H-%M-%S" ;
177+ std::string now_time_str = timestamp_to_string (now_time, format, true );
101178
102179 time_t time = std::chrono::system_clock::to_time_t (now_time);
103180 std::tm local_tm = *localtime (&time);
@@ -112,7 +189,13 @@ TEST(time_utils_test, timestamp_to_string_local)
112189 << " -" << number_trailing_zeros_format (local_tm.tm_min , 2 )
113190 << " -" << number_trailing_zeros_format (local_tm.tm_sec , 2 );
114191
192+ // Test timestamp_to_string
115193 ASSERT_EQ (now_time_str, expected_string_os.str ());
194+
195+ // Test string_to_timestamp
196+ Timestamp now_time_from_str = string_to_timestamp (now_time_str, format, true );
197+ // NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
198+ ASSERT_EQ (timestamp_to_string (now_time_from_str, format, true ), expected_string_os.str ());
116199}
117200
118201/* *
0 commit comments