@@ -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 */
4041TEST (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