-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtime_utils_test.cpp
More file actions
514 lines (441 loc) · 17 KB
/
Copy pathtime_utils_test.cpp
File metadata and controls
514 lines (441 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <thread>
#include <iomanip>
#include <cpp_utils/testing/gtest_aux.hpp>
#include <gtest/gtest.h>
#include <cpp_utils/macros/macros.hpp>
#include <cpp_utils/time/time_utils.hpp>
#include <cpp_utils/utils.hpp>
using namespace eprosima::utils;
/**
* Test function timestamp_to_string with a time stamp, as well the inverse conversion string_to_timestamp.
*
* CASES:
* - now
* - now with alternative format
* - old time
* - date before 1970
* - the beginning of time
* - future time
* - date after 2038
* - the end of time
* - some time today
*/
TEST(time_utils_test, timestamp_to_string_to_timestamp)
{
// now
{
Timestamp now_time = now();
std::string now_time_str = timestamp_to_string(now_time);
time_t time = std::chrono::system_clock::to_time_t(now_time);
std::tm local_tm = *gmtime(&time);
std::ostringstream expected_string_os;
expected_string_os
<< number_trailing_zeros_format(local_tm.tm_year + 1900, 4)
<< "-" << number_trailing_zeros_format(local_tm.tm_mon + 1, 2)
<< "-" << number_trailing_zeros_format(local_tm.tm_mday, 2)
<< "_" << number_trailing_zeros_format(local_tm.tm_hour, 2)
<< "-" << number_trailing_zeros_format(local_tm.tm_min, 2)
<< "-" << number_trailing_zeros_format(local_tm.tm_sec, 2);
// Test timestamp_to_string
ASSERT_EQ(now_time_str, expected_string_os.str());
// Test string_to_timestamp
Timestamp now_time_from_str = string_to_timestamp(now_time_str);
// NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
ASSERT_EQ(timestamp_to_string(now_time_from_str), expected_string_os.str());
}
// now with alternative format
{
Timestamp now_time = now();
std::string format = "%S-%M-%H___%d-%m-%Y";
std::string now_time_str = timestamp_to_string(now_time, format);
time_t time = std::chrono::system_clock::to_time_t(now_time);
std::tm local_tm = *gmtime(&time);
std::ostringstream expected_string_os;
expected_string_os
<< number_trailing_zeros_format(local_tm.tm_sec, 2)
<< "-" << number_trailing_zeros_format(local_tm.tm_min, 2)
<< "-" << number_trailing_zeros_format(local_tm.tm_hour, 2)
<< "___" << number_trailing_zeros_format(local_tm.tm_mday, 2)
<< "-" << number_trailing_zeros_format(local_tm.tm_mon + 1, 2)
<< "-" << number_trailing_zeros_format(local_tm.tm_year + 1900, 4);
// Test timestamp_to_string
ASSERT_EQ(now_time_str, expected_string_os.str());
// Test string_to_timestamp
Timestamp now_time_from_str = string_to_timestamp(now_time_str, format);
// NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
ASSERT_EQ(timestamp_to_string(now_time_from_str, format), expected_string_os.str());
}
// old time
{
Timestamp old_time = date_to_timestamp(1970u, 7u, 20u, 6u, 39u, 42u);
std::string old_time_str = timestamp_to_string(old_time);
std::ostringstream expected_string_os;
expected_string_os
<< 1970
<< "-" << "07"
<< "-" << 20
<< "_" << "06"
<< "-" << 39
<< "-" << 42;
// Test timestamp_to_string
ASSERT_EQ(old_time_str, expected_string_os.str());
// Test string_to_timestamp
Timestamp old_time_from_str = string_to_timestamp(old_time_str);
// NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
ASSERT_EQ(timestamp_to_string(old_time_from_str), expected_string_os.str());
}
// date before 1970
{
Timestamp old_time = date_to_timestamp(1959u, 7u, 20u, 6u, 39u, 42u);
std::string old_time_str = timestamp_to_string(old_time);
std::ostringstream expected_string_os;
#if _EPROSIMA_WINDOWS_PLATFORM
expected_string_os
<< 1970
<< "-" << "01"
<< "-" << "01"
<< "_" << "00"
<< "-" << "00"
<< "-" << "00";
#else
expected_string_os
<< 1959
<< "-" << "07"
<< "-" << 20
<< "_" << "06"
<< "-" << 39
<< "-" << 42;
#endif // _EPROSIMA_WINDOWS_PLATFORM
// Test timestamp_to_string
ASSERT_EQ(old_time_str, expected_string_os.str());
// Test string_to_timestamp
Timestamp old_time_from_str = string_to_timestamp(old_time_str);
// NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
ASSERT_EQ(timestamp_to_string(old_time_from_str), expected_string_os.str());
}
// the begininng of time
{
Timestamp beginning_time = the_beginning_of_time();
std::string beginning_time_str = timestamp_to_string(beginning_time);
std::ostringstream expected_string_os;
#if _EPROSIMA_WINDOWS_PLATFORM
expected_string_os
<< 1970
<< "-" << "01"
<< "-" << "01"
<< "_" << "00"
<< "-" << "00"
<< "-" << "00";
#else //1677-09-21_00-12-44
expected_string_os
<< 1677
<< "-" << "09"
<< "-" << "21"
<< "_" << "00"
<< "-" << "12"
<< "-" << "44";
#endif // _EPROSIMA_WINDOWS_PLATFORM
// Test timestamp_to_string
ASSERT_EQ(beginning_time_str, expected_string_os.str());
// Test string_to_timestamp
Timestamp beginning_time_from_str = string_to_timestamp(beginning_time_str);
// NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
ASSERT_EQ(timestamp_to_string(beginning_time_from_str), expected_string_os.str());
}
// future time
{
Timestamp future_time = date_to_timestamp(2033u, 5u, 22u);
std::string future_time_str = timestamp_to_string(future_time);
std::ostringstream expected_string_os;
expected_string_os
<< 2033
<< "-" << "05"
<< "-" << 22
<< "_" << "00"
<< "-" << "00"
<< "-" << "00";
// Test timestamp_to_string
ASSERT_EQ(future_time_str, expected_string_os.str());
// Test string_to_timestamp
Timestamp future_time_from_str = string_to_timestamp(future_time_str);
// NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
ASSERT_EQ(timestamp_to_string(future_time_from_str), expected_string_os.str());
}
// date after 2038
{
Timestamp future_time = date_to_timestamp(2049u, 5u, 22u);
std::string future_time_str = timestamp_to_string(future_time);
std::ostringstream expected_string_os;
#if _EPROSIMA_WINDOWS_PLATFORM && PLATFORM_32BIT
expected_string_os
<< 2038
<< "-" << "01"
<< "-" << 19
<< "_" << "03"
<< "-" << "14"
<< "-" << "07";
#else
expected_string_os
<< 2049
<< "-" << "05"
<< "-" << 22
<< "_" << "00"
<< "-" << "00"
<< "-" << "00";
#endif // _EPROSIMA_WINDOWS_PLATFORM
// Test timestamp_to_string
ASSERT_EQ(future_time_str, expected_string_os.str());
// Test string_to_timestamp
Timestamp future_time_from_str = string_to_timestamp(future_time_str);
// NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
ASSERT_EQ(timestamp_to_string(future_time_from_str), expected_string_os.str());
}
// the end of time
{
Timestamp end_time = the_end_of_time();
std::string end_time_str = timestamp_to_string(end_time);
std::ostringstream expected_string_os;
#if _EPROSIMA_WINDOWS_PLATFORM && _PLATFORM_32BIT
expected_string_os
<< 2038
<< "-" << "01"
<< "-" << 19
<< "_" << "03"
<< "-" << "14"
<< "-" << "07";
#elif _EPROSIMA_WINDOWS_PLATFORM && _PLATFORM_64BIT
expected_string_os
<< 3000
<< "-" << "12"
<< "-" << 31
<< "_" << "23"
<< "-" << "59"
<< "-" << "59";
#else // 2262-04-11 23:47:16
expected_string_os
<< 2262
<< "-" << "04"
<< "-" << 11
<< "_" << "23"
<< "-" << "47"
<< "-" << "16";
#endif // _EPROSIMA_WINDOWS_PLATFORM
// Test timestamp_to_string
ASSERT_EQ(end_time_str, expected_string_os.str());
// Test string_to_timestamp
Timestamp end_time_from_str = string_to_timestamp(end_time_str);
// NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
ASSERT_EQ(timestamp_to_string(end_time_from_str), expected_string_os.str());
}
// some time today
{
Timestamp some_time_today = time_to_timestamp(13u, 13u, 13u);
std::string some_time_today_str = timestamp_to_string(some_time_today);
// Get current timestamp and use its date to construct expected string
Timestamp now_ts = now();
std::string now_time_str = timestamp_to_string(now_ts);
time_t now_time = std::chrono::system_clock::to_time_t(now_ts);
std::tm now_tm = *gmtime(&now_time);
std::ostringstream expected_string_os;
expected_string_os
<< number_trailing_zeros_format(now_tm.tm_year + 1900, 4)
<< "-" << number_trailing_zeros_format(now_tm.tm_mon + 1, 2)
<< "-" << number_trailing_zeros_format(now_tm.tm_mday, 2)
<< "_" << "13"
<< "-" << "13"
<< "-" << "13";
// Test timestamp_to_string
ASSERT_EQ(some_time_today_str, expected_string_os.str());
// Test string_to_timestamp
Timestamp some_time_today_from_str = string_to_timestamp(some_time_today_str);
// NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
ASSERT_EQ(timestamp_to_string(some_time_today_from_str), expected_string_os.str());
}
}
/**
* Test function timestamp_to_string with a time stamp with local time, as well the inverse conversion string_to_timestamp.
*
* NOTE: only case is now because the local time zone depends on the part of the year and the test becomes a mess.
*/
TEST(time_utils_test, timestamp_to_string_to_timestamp_local)
{
Timestamp now_time = now();
std::string format = "%Y-%m-%d_%H-%M-%S";
std::string now_time_str = timestamp_to_string(now_time, format, true);
time_t time = std::chrono::system_clock::to_time_t(now_time);
std::tm local_tm = *localtime(&time);
std::ostringstream expected_string_os;
expected_string_os
<< number_trailing_zeros_format(local_tm.tm_year + 1900, 4)
<< "-" << number_trailing_zeros_format(local_tm.tm_mon + 1, 2)
<< "-" << number_trailing_zeros_format(local_tm.tm_mday, 2)
<< "_" << number_trailing_zeros_format(local_tm.tm_hour, 2)
<< "-" << number_trailing_zeros_format(local_tm.tm_min, 2)
<< "-" << number_trailing_zeros_format(local_tm.tm_sec, 2);
// Test timestamp_to_string
ASSERT_EQ(now_time_str, expected_string_os.str());
// Test string_to_timestamp
Timestamp now_time_from_str = string_to_timestamp(now_time_str, format, true);
// NOTE: cannot directly compare timestamps because some precision is lost during ts->str conversion
ASSERT_EQ(timestamp_to_string(now_time_from_str, format, true), expected_string_os.str());
}
/**
* Test function timestamp_to_string with a time stamp changing format string.
*
* CASES:
* - time zone
* - time zone offset from UTC
* - year
* - 2 digits year
* - month
* - month abbreviation
* - month name
* - day of the month
* - week day
* - day of the year
* - hours
* - minutes
* - seconds
* - hours:minutes:seconds
* - year & seconds (with strange format)
* - seconds & TZone & week day (with strange format)
* - TZone & TZone deviation (with local date)
*
* NOTE: There are more available values to format. These are the most common.
*/
TEST(time_utils_test, timestamp_to_string_format)
{
// The 20th of July 1969 at 6h 39min 42s the man put a foot in the moon.
// However, we use here 1970 because windows time format does not allow years before 1970
auto date = date_to_timestamp(1970u, 7u, 20u, 6u, 39u, 42u);
// time zone
{
// String for Time Zone is different in Windows or Linux
std::string expected_str = "UTC+00";
std::string date_str = timestamp_to_string(date, "%Z");
ASSERT_EQ(date_str, expected_str);
}
// time zone offset from UTC
{
// String for Time Zone offset is different in Windows or Linux
// Thus the first char (+/-) is removed because it is arbitrary (or looks like it) in Windows.
std::string expected_str = "0000";
std::string date_str = timestamp_to_string(date, "%z");
date_str = date_str.substr(1);
ASSERT_EQ(date_str, expected_str);
}
// year
{
std::string expected_str = "1970";
std::string date_str = timestamp_to_string(date, "%Y");
ASSERT_EQ(date_str, expected_str);
}
// 2 digits year
{
std::string expected_str = "70";
std::string date_str = timestamp_to_string(date, "%y");
ASSERT_EQ(date_str, expected_str);
}
// month
{
std::string expected_str = "07";
std::string date_str = timestamp_to_string(date, "%m");
ASSERT_EQ(date_str, expected_str);
}
// month abbreviation
{
std::string expected_str = "Jul";
std::string date_str = timestamp_to_string(date, "%b");
ASSERT_EQ(date_str, expected_str);
}
// month name
{
std::string expected_str = "July";
std::string date_str = timestamp_to_string(date, "%B");
ASSERT_EQ(date_str, expected_str);
}
// day of the month
{
std::string expected_str = "20";
std::string date_str = timestamp_to_string(date, "%d");
ASSERT_EQ(date_str, expected_str);
}
// week day
{
std::string expected_str = "1";
std::string date_str = timestamp_to_string(date, "%w");
ASSERT_EQ(date_str, expected_str);
}
// day of the year
{
std::string expected_str = "201";
std::string date_str = timestamp_to_string(date, "%j");
ASSERT_EQ(date_str, expected_str);
}
// hours
{
std::string expected_str = "06";
std::string date_str = timestamp_to_string(date, "%H");
ASSERT_EQ(date_str, expected_str);
}
// minutes
{
std::string expected_str = "39";
std::string date_str = timestamp_to_string(date, "%M");
ASSERT_EQ(date_str, expected_str);
}
// seconds
{
std::string expected_str = "42";
std::string date_str = timestamp_to_string(date, "%S");
ASSERT_EQ(date_str, expected_str);
}
// hours:minutes:seconds
{
std::string expected_str = "06:39:42";
std::string date_str = timestamp_to_string(date, "%T");
ASSERT_EQ(date_str, expected_str);
}
// year & seconds (with strange format)
{
std::string expected_str = "_1970_-_42_";
std::string date_str = timestamp_to_string(date, "_%Y_-_%S_");
ASSERT_EQ(date_str, expected_str);
}
// seconds & TZone & week day (with strange format)
{
std::string expected_str = "42::1";
std::string date_str = timestamp_to_string(date, "%S::%w");
ASSERT_EQ(date_str, expected_str);
}
// TZone & TZone deviation (with local date)
{
auto date_now = now();
std::string this_time_zone_str = timestamp_to_string(date_now, "%Z", true);
std::string this_time_zone_dev_str = timestamp_to_string(date_now, "%z", true);
std::ostringstream expected_ostr;
expected_ostr << " " << this_time_zone_str << "( " << this_time_zone_dev_str << " ) ";
std::string date_str = timestamp_to_string(date_now, " %Z( %z ) ", true);
ASSERT_EQ(date_str, expected_ostr.str());
}
}
int main(
int argc,
char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}