File: time_unittest.cc

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (53 lines) | stat: -rw-r--r-- 2,054 bytes parent folder | download | duplicates (10)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "platform/api/time.h"

#include <chrono>
#include <ctime>

#include "gtest/gtest.h"
#include "util/chrono_helpers.h"

namespace openscreen {

#if __cplusplus < 202000L
// Before C++20, the standard does not guarantee that time_t is the number of
// seconds since the UNIX epoch. It also doesn't guarantee that one tick of
// time_t represents one second. However, this is so universally common, it's
// worth assuming the platform provides this.
TEST(TimeTest, TimeTMeetsTheCpp20Standard) {
  // Use the functions the standard *does* provide for calendar time
  // conversions to determine the value of time_t at the UNIX epoch.
  std::tm epoch_tm;
  epoch_tm.tm_sec = 0;
  epoch_tm.tm_min = 0;
  epoch_tm.tm_hour = 0;
  epoch_tm.tm_mday = 1;
  epoch_tm.tm_mon = 0;
  epoch_tm.tm_year = 70;  // 1970
  epoch_tm.tm_isdst = 0;
  // Note: std::mktime() assumes the translation is in the local time zone.
  const std::time_t new_year_1970_in_local_tz = std::mktime(&epoch_tm);
  // Purposely misinterpret |new_year_1970_in_local_tz| as UTC to provide
  // information as to how to offset |epoch_tm| such that std::mktime() will
  // be fooled into returning the value in terms of UTC.
  std::tm* const wrong_tm = std::gmtime(&new_year_1970_in_local_tz);
  epoch_tm.tm_sec += epoch_tm.tm_sec - wrong_tm->tm_sec;
  epoch_tm.tm_min += epoch_tm.tm_min - wrong_tm->tm_min;
  epoch_tm.tm_hour += epoch_tm.tm_hour - wrong_tm->tm_hour;
  epoch_tm.tm_mday += epoch_tm.tm_mday - wrong_tm->tm_mday;
  epoch_tm.tm_mon += epoch_tm.tm_mon - wrong_tm->tm_mon;
  epoch_tm.tm_year += epoch_tm.tm_year - wrong_tm->tm_year;

  const std::time_t epoch = std::mktime(&epoch_tm);
  EXPECT_EQ(seconds(0), seconds(epoch));

  ++epoch_tm.tm_sec;
  const std::time_t epoch_plus_one_second = std::mktime(&epoch_tm);
  EXPECT_EQ(seconds(1), seconds(epoch_plus_one_second));
}
#endif

}  // namespace openscreen