@@ -71,6 +71,7 @@ TEST(CommandLineFlagsTest, CanBeAccessedInCodeOnceGTestHIsIncluded) {
71
71
72
72
#include < limits.h> // For INT_MAX.
73
73
#include < stdlib.h>
74
+ #include < string.h>
74
75
#include < time.h>
75
76
76
77
#include < map>
@@ -141,6 +142,7 @@ using testing::TestPartResult;
141
142
using testing::TestPartResultArray;
142
143
using testing::TestProperty;
143
144
using testing::TestResult;
145
+ using testing::TimeInMillis;
144
146
using testing::UnitTest;
145
147
using testing::kMaxStackTraceDepth ;
146
148
using testing::internal::AddReference;
@@ -156,13 +158,15 @@ using testing::internal::CountIf;
156
158
using testing::internal::EqFailure;
157
159
using testing::internal::FloatingPoint;
158
160
using testing::internal::ForEach;
161
+ using testing::internal::FormatEpochTimeInMillisAsIso8601;
159
162
using testing::internal::FormatTimeInMillisAsSeconds;
160
163
using testing::internal::GTestFlagSaver;
161
164
using testing::internal::GetCurrentOsStackTraceExceptTop;
162
165
using testing::internal::GetElementOr;
163
166
using testing::internal::GetNextRandomSeed;
164
167
using testing::internal::GetRandomSeedFromFlag;
165
168
using testing::internal::GetTestTypeId;
169
+ using testing::internal::GetTimeInMillis;
166
170
using testing::internal::GetTypeId;
167
171
using testing::internal::GetUnitTestImpl;
168
172
using testing::internal::ImplicitlyConvertible;
@@ -308,6 +312,103 @@ TEST(FormatTimeInMillisAsSecondsTest, FormatsNegativeNumber) {
308
312
EXPECT_EQ (" -3" , FormatTimeInMillisAsSeconds (-3000 ));
309
313
}
310
314
315
+ // Tests FormatEpochTimeInMillisAsIso8601(). The correctness of conversion
316
+ // for particular dates below was verified in Python using
317
+ // datetime.datetime.fromutctimestamp(<timetamp>/1000).
318
+
319
+ // FormatEpochTimeInMillisAsIso8601 depends on the current timezone, so we
320
+ // have to set up a particular timezone to obtain predictable results.
321
+ class FormatEpochTimeInMillisAsIso8601Test : public Test {
322
+ public:
323
+ // On Cygwin, GCC doesn't allow unqualified integer literals to exceed
324
+ // 32 bits, even when 64-bit integer types are available. We have to
325
+ // force the constants to have a 64-bit type here.
326
+ static const TimeInMillis kMillisPerSec = 1000 ;
327
+
328
+ private:
329
+ virtual void SetUp () {
330
+ saved_tz_ = NULL ;
331
+ #if _MSC_VER
332
+ # pragma warning(push) // Saves the current warning state.
333
+ # pragma warning(disable:4996) // Temporarily disables warning 4996
334
+ // (function or variable may be unsafe
335
+ // for getenv, function is deprecated for
336
+ // strdup).
337
+ if (getenv (" TZ" ))
338
+ saved_tz_ = strdup (getenv (" TZ" ));
339
+ # pragma warning(pop) // Restores the warning state again.
340
+ #else
341
+ if (getenv (" TZ" ))
342
+ saved_tz_ = strdup (getenv (" TZ" ));
343
+ #endif
344
+
345
+ // Set up the time zone for FormatEpochTimeInMillisAsIso8601 to use. We
346
+ // cannot use the local time zone because the function's output depends
347
+ // on the time zone.
348
+ SetTimeZone (" UTC+00" );
349
+ }
350
+
351
+ virtual void TearDown () {
352
+ SetTimeZone (saved_tz_);
353
+ free (const_cast <char *>(saved_tz_));
354
+ saved_tz_ = NULL ;
355
+ }
356
+
357
+ static void SetTimeZone (const char * time_zone) {
358
+ // tzset() distinguishes between the TZ variable being present and empty
359
+ // and not being present, so we have to consider the case of time_zone
360
+ // being NULL.
361
+ #if _MSC_VER
362
+ // ...Unless it's MSVC, whose standard library's _putenv doesn't
363
+ // distinguish between an empty and a missing variable.
364
+ const std::string env_var =
365
+ std::string (" TZ=" ) + (time_zone ? time_zone : " " );
366
+ _putenv (env_var.c_str ());
367
+ # pragma warning(push) // Saves the current warning state.
368
+ # pragma warning(disable:4996) // Temporarily disables warning 4996
369
+ // (function is deprecated).
370
+ tzset ();
371
+ # pragma warning(pop) // Restores the warning state again.
372
+ #else
373
+ if (time_zone) {
374
+ setenv ((" TZ" ), time_zone, 1 );
375
+ } else {
376
+ unsetenv (" TZ" );
377
+ }
378
+ tzset ();
379
+ #endif
380
+ }
381
+
382
+ const char * saved_tz_;
383
+ };
384
+
385
+ const TimeInMillis FormatEpochTimeInMillisAsIso8601Test::kMillisPerSec ;
386
+
387
+ TEST_F (FormatEpochTimeInMillisAsIso8601Test, PrintsTwoDigitSegments) {
388
+ EXPECT_EQ (" 2011-10-31T18:52:42" ,
389
+ FormatEpochTimeInMillisAsIso8601 (1320087162 * kMillisPerSec ));
390
+ }
391
+
392
+ TEST_F (FormatEpochTimeInMillisAsIso8601Test, MillisecondsDoNotAffectResult) {
393
+ EXPECT_EQ (
394
+ " 2011-10-31T18:52:42" ,
395
+ FormatEpochTimeInMillisAsIso8601 (1320087162 * kMillisPerSec + 234 ));
396
+ }
397
+
398
+ TEST_F (FormatEpochTimeInMillisAsIso8601Test, PrintsLeadingZeroes) {
399
+ EXPECT_EQ (" 2011-09-03T05:07:02" ,
400
+ FormatEpochTimeInMillisAsIso8601 (1315026422 * kMillisPerSec ));
401
+ }
402
+
403
+ TEST_F (FormatEpochTimeInMillisAsIso8601Test, Prints24HourTime) {
404
+ EXPECT_EQ (" 2011-09-28T17:08:22" ,
405
+ FormatEpochTimeInMillisAsIso8601 (1317229702 * kMillisPerSec ));
406
+ }
407
+
408
+ TEST_F (FormatEpochTimeInMillisAsIso8601Test, PrintsEpochStart) {
409
+ EXPECT_EQ (" 1970-01-01T00:00:00" , FormatEpochTimeInMillisAsIso8601 (0 ));
410
+ }
411
+
311
412
#if GTEST_CAN_COMPARE_NULL
312
413
313
414
# ifdef __BORLANDC__
@@ -2130,6 +2231,11 @@ TEST(UnitTestTest, CanGetOriginalWorkingDir) {
2130
2231
EXPECT_STRNE (UnitTest::GetInstance ()->original_working_dir (), " " );
2131
2232
}
2132
2233
2234
+ TEST (UnitTestTest, ReturnsPlausibleTimestamp) {
2235
+ EXPECT_LT (0 , UnitTest::GetInstance ()->start_timestamp ());
2236
+ EXPECT_LE (UnitTest::GetInstance ()->start_timestamp (), GetTimeInMillis ());
2237
+ }
2238
+
2133
2239
// This group of tests is for predicate assertions (ASSERT_PRED*, etc)
2134
2240
// of various arities. They do not attempt to be exhaustive. Rather,
2135
2241
// view them as smoke tests that can be easily reviewed and verified.
0 commit comments