@@ -31,7 +31,11 @@ using namespace eprosima::utils;
3131 * - now
3232 * - now with alternative format
3333 * - old time
34+ * - date before 1970
35+ * - the beginning of time
3436 * - future time
37+ * - date after 2038
38+ * - the end of time
3539 * - some time today
3640 */
3741TEST (time_utils_test, timestamp_to_string_to_timestamp)
@@ -112,14 +116,81 @@ TEST(time_utils_test, timestamp_to_string_to_timestamp)
112116 ASSERT_EQ (timestamp_to_string (old_time_from_str), expected_string_os.str ());
113117 }
114118
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+
152+ // the begininng of time
153+ {
154+ Timestamp beginning_time = the_beginning_of_time ();
155+ std::string beginning_time_str = timestamp_to_string (beginning_time);
156+
157+ std::ostringstream expected_string_os;
158+ #if _EPROSIMA_WINDOWS_PLATFORM
159+ expected_string_os
160+ << 1970
161+ << " -" << " 01"
162+ << " -" << " 01"
163+ << " _" << " 00"
164+ << " -" << " 00"
165+ << " -" << " 00" ;
166+ #else // 1677-09-21_00-12-44
167+ expected_string_os
168+ << 1677
169+ << " -" << " 09"
170+ << " -" << " 21"
171+ << " _" << " 00"
172+ << " -" << " 12"
173+ << " -" << " 44" ;
174+ #endif // _EPROSIMA_WINDOWS_PLATFORM
175+
176+
177+ // Test timestamp_to_string
178+ ASSERT_EQ (beginning_time_str, expected_string_os.str ());
179+
180+ // Test string_to_timestamp
181+ Timestamp beginning_time_from_str = string_to_timestamp (beginning_time_str);
182+ // NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
183+ ASSERT_EQ (timestamp_to_string (beginning_time_from_str), expected_string_os.str ());
184+ }
185+
115186 // future time
116187 {
117- Timestamp future_time = date_to_timestamp (2233u , 5u , 22u );
188+ Timestamp future_time = date_to_timestamp (2033u , 5u , 22u );
118189 std::string future_time_str = timestamp_to_string (future_time);
119190
120191 std::ostringstream expected_string_os;
121192 expected_string_os
122- << 2233
193+ << 2033
123194 << " -" << " 05"
124195 << " -" << 22
125196 << " _" << " 00"
@@ -135,6 +206,81 @@ TEST(time_utils_test, timestamp_to_string_to_timestamp)
135206 ASSERT_EQ (timestamp_to_string (future_time_from_str), expected_string_os.str ());
136207 }
137208
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 && PLATFORM_32BIT
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+
242+ // the end of time
243+ {
244+ Timestamp end_time = the_end_of_time ();
245+ std::string end_time_str = timestamp_to_string (end_time);
246+
247+ std::ostringstream expected_string_os;
248+ #if _EPROSIMA_WINDOWS_PLATFORM && _PLATFORM_32BIT
249+ expected_string_os
250+ << 2038
251+ << " -" << " 01"
252+ << " -" << 19
253+ << " _" << " 03"
254+ << " -" << " 14"
255+ << " -" << " 07" ;
256+ #elif _EPROSIMA_WINDOWS_PLATFORM && _PLATFORM_64BIT
257+ expected_string_os
258+ << 3000
259+ << " -" << " 12"
260+ << " -" << 31
261+ << " _" << " 23"
262+ << " -" << " 59"
263+ << " -" << " 59" ;
264+ #else // 2262-04-11 23:47:16
265+ expected_string_os
266+ << 2262
267+ << " -" << " 04"
268+ << " -" << 11
269+ << " _" << " 23"
270+ << " -" << " 47"
271+ << " -" << " 16" ;
272+ #endif // _EPROSIMA_WINDOWS_PLATFORM
273+
274+
275+ // Test timestamp_to_string
276+ ASSERT_EQ (end_time_str, expected_string_os.str ());
277+
278+ // Test string_to_timestamp
279+ Timestamp end_time_from_str = string_to_timestamp (end_time_str);
280+ // NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
281+ ASSERT_EQ (timestamp_to_string (end_time_from_str), expected_string_os.str ());
282+ }
283+
138284 // some time today
139285 {
140286 Timestamp some_time_today = time_to_timestamp (13u , 13u , 13u );
0 commit comments