This source file includes following definitions.
- SetUp
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST
- HighResClockTest
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include "base/time/time.h"
#include <time.h>
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "base/threading/platform_thread.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::Time;
using base::TimeDelta;
using base::TimeTicks;
class TimeTest : public testing::Test {
protected:
virtual void SetUp() OVERRIDE {
struct tm local_comparison_tm = {
0,
45,
12,
15,
10 - 1,
2007 - 1900,
0,
0,
-1
};
time_t converted_time = mktime(&local_comparison_tm);
ASSERT_GT(converted_time, 0);
comparison_time_local_ = Time::FromTimeT(converted_time);
comparison_time_pdt_ = Time::FromTimeT(1192477500);
}
Time comparison_time_local_;
Time comparison_time_pdt_;
};
TEST_F(TimeTest, TimeT) {
time_t now_t_1 = time(NULL);
struct tm tms;
#if defined(OS_WIN)
localtime_s(&tms, &now_t_1);
#elif defined(OS_POSIX)
localtime_r(&now_t_1, &tms);
#endif
Time our_time_1 = Time::FromTimeT(now_t_1);
Time::Exploded exploded;
our_time_1.LocalExplode(&exploded);
EXPECT_EQ(tms.tm_year + 1900, exploded.year);
EXPECT_EQ(tms.tm_mon + 1, exploded.month);
EXPECT_EQ(tms.tm_mday, exploded.day_of_month);
EXPECT_EQ(tms.tm_hour, exploded.hour);
EXPECT_EQ(tms.tm_min, exploded.minute);
EXPECT_EQ(tms.tm_sec, exploded.second);
Time our_time_2 = Time::FromLocalExploded(exploded);
EXPECT_TRUE(our_time_1 == our_time_2);
time_t now_t_2 = our_time_2.ToTimeT();
EXPECT_EQ(now_t_1, now_t_2);
EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT());
EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT());
EXPECT_EQ(0, Time().ToTimeT());
EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue());
}
TEST_F(TimeTest, JsTime) {
Time epoch = Time::FromJsTime(0.0);
EXPECT_EQ(epoch, Time::UnixEpoch());
Time t = Time::FromJsTime(700000.3);
EXPECT_EQ(700.0003, t.ToDoubleT());
t = Time::FromDoubleT(800.73);
EXPECT_EQ(800730.0, t.ToJsTime());
}
#if defined(OS_POSIX)
TEST_F(TimeTest, FromTimeVal) {
Time now = Time::Now();
Time also_now = Time::FromTimeVal(now.ToTimeVal());
EXPECT_EQ(now, also_now);
}
#endif
TEST_F(TimeTest, FromExplodedWithMilliseconds) {
Time now = Time::NowFromSystemTime();
Time::Exploded exploded1 = {0};
now.UTCExplode(&exploded1);
exploded1.millisecond = 500;
Time time = Time::FromUTCExploded(exploded1);
Time::Exploded exploded2 = {0};
time.UTCExplode(&exploded2);
EXPECT_EQ(exploded1.millisecond, exploded2.millisecond);
}
TEST_F(TimeTest, ZeroIsSymmetric) {
Time zero_time(Time::FromTimeT(0));
EXPECT_EQ(0, zero_time.ToTimeT());
EXPECT_EQ(0.0, zero_time.ToDoubleT());
}
TEST_F(TimeTest, LocalExplode) {
Time a = Time::Now();
Time::Exploded exploded;
a.LocalExplode(&exploded);
Time b = Time::FromLocalExploded(exploded);
EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
}
TEST_F(TimeTest, UTCExplode) {
Time a = Time::Now();
Time::Exploded exploded;
a.UTCExplode(&exploded);
Time b = Time::FromUTCExploded(exploded);
EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
}
TEST_F(TimeTest, LocalMidnight) {
Time::Exploded exploded;
Time::Now().LocalMidnight().LocalExplode(&exploded);
EXPECT_EQ(0, exploded.hour);
EXPECT_EQ(0, exploded.minute);
EXPECT_EQ(0, exploded.second);
EXPECT_EQ(0, exploded.millisecond);
}
TEST_F(TimeTest, ParseTimeTest1) {
time_t current_time = 0;
time(¤t_time);
const int BUFFER_SIZE = 64;
struct tm local_time = {0};
char time_buf[BUFFER_SIZE] = {0};
#if defined(OS_WIN)
localtime_s(&local_time, ¤t_time);
asctime_s(time_buf, arraysize(time_buf), &local_time);
#elif defined(OS_POSIX)
localtime_r(¤t_time, &local_time);
asctime_r(&local_time, time_buf);
#endif
Time parsed_time;
EXPECT_TRUE(Time::FromString(time_buf, &parsed_time));
EXPECT_EQ(current_time, parsed_time.ToTimeT());
}
TEST_F(TimeTest, DayOfWeekSunday) {
Time time;
EXPECT_TRUE(Time::FromString("Sun, 06 May 2012 12:00:00 GMT", &time));
Time::Exploded exploded;
time.UTCExplode(&exploded);
EXPECT_EQ(0, exploded.day_of_week);
}
TEST_F(TimeTest, DayOfWeekWednesday) {
Time time;
EXPECT_TRUE(Time::FromString("Wed, 09 May 2012 12:00:00 GMT", &time));
Time::Exploded exploded;
time.UTCExplode(&exploded);
EXPECT_EQ(3, exploded.day_of_week);
}
TEST_F(TimeTest, DayOfWeekSaturday) {
Time time;
EXPECT_TRUE(Time::FromString("Sat, 12 May 2012 12:00:00 GMT", &time));
Time::Exploded exploded;
time.UTCExplode(&exploded);
EXPECT_EQ(6, exploded.day_of_week);
}
TEST_F(TimeTest, ParseTimeTest2) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("Mon, 15 Oct 2007 19:45:00 GMT", &parsed_time));
EXPECT_EQ(comparison_time_pdt_, parsed_time);
}
TEST_F(TimeTest, ParseTimeTest3) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("15 Oct 07 12:45:00", &parsed_time));
EXPECT_EQ(comparison_time_local_, parsed_time);
}
TEST_F(TimeTest, ParseTimeTest4) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("15 Oct 07 19:45 GMT", &parsed_time));
EXPECT_EQ(comparison_time_pdt_, parsed_time);
}
TEST_F(TimeTest, ParseTimeTest5) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("Mon Oct 15 12:45 PDT 2007", &parsed_time));
EXPECT_EQ(comparison_time_pdt_, parsed_time);
}
TEST_F(TimeTest, ParseTimeTest6) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("Monday, Oct 15, 2007 12:45 PM", &parsed_time));
EXPECT_EQ(comparison_time_local_, parsed_time);
}
TEST_F(TimeTest, ParseTimeTest7) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("10/15/07 12:45:00 PM", &parsed_time));
EXPECT_EQ(comparison_time_local_, parsed_time);
}
TEST_F(TimeTest, ParseTimeTest8) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("15-OCT-2007 12:45pm", &parsed_time));
EXPECT_EQ(comparison_time_local_, parsed_time);
}
TEST_F(TimeTest, ParseTimeTest9) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("16 Oct 2007 4:45-JST (Tuesday)", &parsed_time));
EXPECT_EQ(comparison_time_pdt_, parsed_time);
}
TEST_F(TimeTest, ParseTimeTest10) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("15/10/07 12:45", &parsed_time));
EXPECT_EQ(parsed_time, comparison_time_local_);
}
TEST_F(TimeTest, ParseTimeTestEpoch0) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:00 +0100 1970",
&parsed_time));
EXPECT_EQ(0, parsed_time.ToTimeT());
EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:00 GMT 1970",
&parsed_time));
EXPECT_EQ(0, parsed_time.ToTimeT());
}
TEST_F(TimeTest, ParseTimeTestEpoch1) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:01 +0100 1970",
&parsed_time));
EXPECT_EQ(1, parsed_time.ToTimeT());
EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:01 GMT 1970",
&parsed_time));
EXPECT_EQ(1, parsed_time.ToTimeT());
}
TEST_F(TimeTest, ParseTimeTestEpoch2) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:02 +0100 1970",
&parsed_time));
EXPECT_EQ(2, parsed_time.ToTimeT());
EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:02 GMT 1970",
&parsed_time));
EXPECT_EQ(2, parsed_time.ToTimeT());
}
TEST_F(TimeTest, ParseTimeTestEpochNeg1) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:59 +0100 1970",
&parsed_time));
EXPECT_EQ(-1, parsed_time.ToTimeT());
EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 1969",
&parsed_time));
EXPECT_EQ(-1, parsed_time.ToTimeT());
}
TEST_F(TimeTest, ParseTimeTestEpochNotNeg1) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 2100",
&parsed_time));
EXPECT_NE(-1, parsed_time.ToTimeT());
}
TEST_F(TimeTest, ParseTimeTestEpochNeg2) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:58 +0100 1970",
&parsed_time));
EXPECT_EQ(-2, parsed_time.ToTimeT());
EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:58 GMT 1969",
&parsed_time));
EXPECT_EQ(-2, parsed_time.ToTimeT());
}
TEST_F(TimeTest, ParseTimeTestEpoch1960) {
Time parsed_time;
EXPECT_TRUE(Time::FromString("Wed Jun 29 19:40:01 +0100 1960",
&parsed_time));
EXPECT_EQ(-299999999, parsed_time.ToTimeT());
EXPECT_TRUE(Time::FromString("Wed Jun 29 18:40:01 GMT 1960",
&parsed_time));
EXPECT_EQ(-299999999, parsed_time.ToTimeT());
EXPECT_TRUE(Time::FromString("Wed Jun 29 17:40:01 GMT 1960",
&parsed_time));
EXPECT_EQ(-300003599, parsed_time.ToTimeT());
}
TEST_F(TimeTest, ParseTimeTestEmpty) {
Time parsed_time;
EXPECT_FALSE(Time::FromString("", &parsed_time));
}
TEST_F(TimeTest, ParseTimeTestInvalidString) {
Time parsed_time;
EXPECT_FALSE(Time::FromString("Monday morning 2000", &parsed_time));
}
TEST_F(TimeTest, ExplodeBeforeUnixEpoch) {
static const int kUnixEpochYear = 1970;
Time t;
Time::Exploded exploded;
t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
EXPECT_EQ(12, exploded.month);
EXPECT_EQ(31, exploded.day_of_month);
EXPECT_EQ(23, exploded.hour);
EXPECT_EQ(59, exploded.minute);
EXPECT_EQ(59, exploded.second);
EXPECT_EQ(999, exploded.millisecond);
t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1000);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
EXPECT_EQ(12, exploded.month);
EXPECT_EQ(31, exploded.day_of_month);
EXPECT_EQ(23, exploded.hour);
EXPECT_EQ(59, exploded.minute);
EXPECT_EQ(59, exploded.second);
EXPECT_EQ(999, exploded.millisecond);
t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1001);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
EXPECT_EQ(12, exploded.month);
EXPECT_EQ(31, exploded.day_of_month);
EXPECT_EQ(23, exploded.hour);
EXPECT_EQ(59, exploded.minute);
EXPECT_EQ(59, exploded.second);
EXPECT_EQ(998, exploded.millisecond);
t = Time::UnixEpoch() - TimeDelta::FromMilliseconds(1000);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
EXPECT_EQ(12, exploded.month);
EXPECT_EQ(31, exploded.day_of_month);
EXPECT_EQ(23, exploded.hour);
EXPECT_EQ(59, exploded.minute);
EXPECT_EQ(59, exploded.second);
EXPECT_EQ(0, exploded.millisecond);
t = Time::UnixEpoch() - TimeDelta::FromMilliseconds(1001);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
EXPECT_EQ(12, exploded.month);
EXPECT_EQ(31, exploded.day_of_month);
EXPECT_EQ(23, exploded.hour);
EXPECT_EQ(59, exploded.minute);
EXPECT_EQ(58, exploded.second);
EXPECT_EQ(999, exploded.millisecond);
t = Time::UnixEpoch();
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
EXPECT_EQ(kUnixEpochYear, exploded.year);
EXPECT_EQ(1, exploded.month);
EXPECT_EQ(1, exploded.day_of_month);
EXPECT_EQ(0, exploded.hour);
EXPECT_EQ(0, exploded.minute);
EXPECT_EQ(0, exploded.second);
EXPECT_EQ(0, exploded.millisecond);
t = Time::UnixEpoch() + TimeDelta::FromMicroseconds(1);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
EXPECT_EQ(kUnixEpochYear, exploded.year);
EXPECT_EQ(1, exploded.month);
EXPECT_EQ(1, exploded.day_of_month);
EXPECT_EQ(0, exploded.hour);
EXPECT_EQ(0, exploded.minute);
EXPECT_EQ(0, exploded.second);
EXPECT_EQ(0, exploded.millisecond);
t = Time::UnixEpoch() + TimeDelta::FromMicroseconds(1000);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
EXPECT_EQ(kUnixEpochYear, exploded.year);
EXPECT_EQ(1, exploded.month);
EXPECT_EQ(1, exploded.day_of_month);
EXPECT_EQ(0, exploded.hour);
EXPECT_EQ(0, exploded.minute);
EXPECT_EQ(0, exploded.second);
EXPECT_EQ(1, exploded.millisecond);
t = Time::UnixEpoch() + TimeDelta::FromMilliseconds(1000);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
EXPECT_EQ(kUnixEpochYear, exploded.year);
EXPECT_EQ(1, exploded.month);
EXPECT_EQ(1, exploded.day_of_month);
EXPECT_EQ(0, exploded.hour);
EXPECT_EQ(0, exploded.minute);
EXPECT_EQ(1, exploded.second);
EXPECT_EQ(0, exploded.millisecond);
t = Time::UnixEpoch() + TimeDelta::FromMilliseconds(1001);
t.UTCExplode(&exploded);
EXPECT_TRUE(exploded.HasValidValues());
EXPECT_EQ(kUnixEpochYear, exploded.year);
EXPECT_EQ(1, exploded.month);
EXPECT_EQ(1, exploded.day_of_month);
EXPECT_EQ(0, exploded.hour);
EXPECT_EQ(0, exploded.minute);
EXPECT_EQ(1, exploded.second);
EXPECT_EQ(1, exploded.millisecond);
}
TEST_F(TimeTest, TimeDeltaMax) {
TimeDelta max = TimeDelta::Max();
EXPECT_TRUE(max.is_max());
EXPECT_EQ(max, TimeDelta::Max());
EXPECT_GT(max, TimeDelta::FromDays(100 * 365));
EXPECT_GT(max, TimeDelta());
}
TEST_F(TimeTest, TimeDeltaMaxConversions) {
TimeDelta t = TimeDelta::Max();
EXPECT_EQ(std::numeric_limits<int64>::max(), t.ToInternalValue());
EXPECT_EQ(std::numeric_limits<int>::max(), t.InDays());
EXPECT_EQ(std::numeric_limits<int>::max(), t.InHours());
EXPECT_EQ(std::numeric_limits<int>::max(), t.InMinutes());
EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InSecondsF());
EXPECT_EQ(std::numeric_limits<int64>::max(), t.InSeconds());
EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InMillisecondsF());
EXPECT_EQ(std::numeric_limits<int64>::max(), t.InMilliseconds());
EXPECT_EQ(std::numeric_limits<int64>::max(), t.InMillisecondsRoundedUp());
t = TimeDelta::FromDays(std::numeric_limits<int>::max());
EXPECT_TRUE(t.is_max());
t = TimeDelta::FromHours(std::numeric_limits<int>::max());
EXPECT_TRUE(t.is_max());
t = TimeDelta::FromMinutes(std::numeric_limits<int>::max());
EXPECT_TRUE(t.is_max());
t = TimeDelta::FromSeconds(std::numeric_limits<int64>::max());
EXPECT_TRUE(t.is_max());
t = TimeDelta::FromMilliseconds(std::numeric_limits<int64>::max());
EXPECT_TRUE(t.is_max());
t = TimeDelta::FromMicroseconds(std::numeric_limits<int64>::max());
EXPECT_TRUE(t.is_max());
}
TEST_F(TimeTest, Max) {
Time max = Time::Max();
EXPECT_TRUE(max.is_max());
EXPECT_EQ(max, Time::Max());
EXPECT_GT(max, Time::Now());
EXPECT_GT(max, Time());
}
TEST_F(TimeTest, MaxConversions) {
Time t = Time::Max();
EXPECT_EQ(std::numeric_limits<int64>::max(), t.ToInternalValue());
t = Time::FromDoubleT(std::numeric_limits<double>::infinity());
EXPECT_TRUE(t.is_max());
EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToDoubleT());
t = Time::FromJsTime(std::numeric_limits<double>::infinity());
EXPECT_TRUE(t.is_max());
EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToJsTime());
t = Time::FromTimeT(std::numeric_limits<time_t>::max());
EXPECT_TRUE(t.is_max());
EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT());
#if defined(OS_POSIX)
struct timeval tval;
tval.tv_sec = std::numeric_limits<time_t>::max();
tval.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1;
t = Time::FromTimeVal(tval);
EXPECT_TRUE(t.is_max());
tval = t.ToTimeVal();
EXPECT_EQ(std::numeric_limits<time_t>::max(), tval.tv_sec);
EXPECT_EQ(static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1,
tval.tv_usec);
#endif
#if defined(OS_MACOSX)
t = Time::FromCFAbsoluteTime(std::numeric_limits<CFAbsoluteTime>::infinity());
EXPECT_TRUE(t.is_max());
EXPECT_EQ(std::numeric_limits<CFAbsoluteTime>::infinity(),
t.ToCFAbsoluteTime());
#endif
#if defined(OS_WIN)
FILETIME ftime;
ftime.dwHighDateTime = std::numeric_limits<DWORD>::max();
ftime.dwLowDateTime = std::numeric_limits<DWORD>::max();
t = Time::FromFileTime(ftime);
EXPECT_TRUE(t.is_max());
ftime = t.ToFileTime();
EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwHighDateTime);
EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwLowDateTime);
#endif
}
#if defined(OS_MACOSX)
TEST_F(TimeTest, TimeTOverflow) {
Time t = Time::FromInternalValue(std::numeric_limits<int64>::max() - 1);
EXPECT_FALSE(t.is_max());
EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT());
}
#endif
#if defined(OS_ANDROID)
TEST_F(TimeTest, FromLocalExplodedCrashOnAndroid) {
Time::Exploded midnight = {2013,
10,
0,
13,
0,
0,
0,
};
static char buffer[] = "TZ=America/Santiago";
putenv(buffer);
tzset();
Time t = Time::FromLocalExploded(midnight);
EXPECT_EQ(1381633200, t.ToTimeT());
}
#endif
TEST(TimeTicks, Deltas) {
for (int index = 0; index < 50; index++) {
TimeTicks ticks_start = TimeTicks::Now();
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
TimeTicks ticks_stop = TimeTicks::Now();
TimeDelta delta = ticks_stop - ticks_start;
EXPECT_GE(delta.InMilliseconds(), 9);
EXPECT_GE(delta.InMicroseconds(), 9000);
EXPECT_EQ(delta.InSeconds(), 0);
}
}
static void HighResClockTest(TimeTicks (*GetTicks)()) {
#if defined(OS_WIN)
if (!TimeTicks::IsHighResClockWorking())
return;
#endif
const int kTargetGranularityUs = 15000;
bool success = false;
int retries = 100;
TimeDelta delta;
while (!success && retries--) {
TimeTicks ticks_start = GetTicks();
do {
delta = GetTicks() - ticks_start;
} while (delta.InMilliseconds() == 0);
if (delta.InMicroseconds() <= kTargetGranularityUs)
success = true;
}
EXPECT_TRUE(success);
}
TEST(TimeTicks, HighResNow) {
HighResClockTest(&TimeTicks::HighResNow);
}
#if defined(OS_ANDROID)
#define MAYBE_ThreadNow DISABLED_ThreadNow
#else
#define MAYBE_ThreadNow ThreadNow
#endif
TEST(TimeTicks, MAYBE_ThreadNow) {
if (TimeTicks::IsThreadNowSupported()) {
TimeTicks begin = TimeTicks::Now();
TimeTicks begin_thread = TimeTicks::ThreadNow();
EXPECT_GT(begin_thread, TimeTicks());
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
TimeTicks end_thread = TimeTicks::ThreadNow();
TimeTicks end = TimeTicks::Now();
TimeDelta delta = end - begin;
TimeDelta delta_thread = end_thread - begin_thread;
EXPECT_GT(delta_thread.InMicroseconds(), 0);
TimeDelta difference = delta - delta_thread;
EXPECT_GE(difference.InMicroseconds(), 9000);
}
}
TEST(TimeTicks, NowFromSystemTraceTime) {
HighResClockTest(&TimeTicks::NowFromSystemTraceTime);
}
TEST(TimeDelta, FromAndIn) {
EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48));
EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180));
EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120));
EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000));
EXPECT_TRUE(TimeDelta::FromMilliseconds(2) ==
TimeDelta::FromMicroseconds(2000));
EXPECT_EQ(13, TimeDelta::FromDays(13).InDays());
EXPECT_EQ(13, TimeDelta::FromHours(13).InHours());
EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes());
EXPECT_EQ(13, TimeDelta::FromSeconds(13).InSeconds());
EXPECT_EQ(13.0, TimeDelta::FromSeconds(13).InSecondsF());
EXPECT_EQ(13, TimeDelta::FromMilliseconds(13).InMilliseconds());
EXPECT_EQ(13.0, TimeDelta::FromMilliseconds(13).InMillisecondsF());
EXPECT_EQ(13, TimeDelta::FromMicroseconds(13).InMicroseconds());
}
#if defined(OS_POSIX)
TEST(TimeDelta, TimeSpecConversion) {
struct timespec result = TimeDelta::FromSeconds(0).ToTimeSpec();
EXPECT_EQ(result.tv_sec, 0);
EXPECT_EQ(result.tv_nsec, 0);
result = TimeDelta::FromSeconds(1).ToTimeSpec();
EXPECT_EQ(result.tv_sec, 1);
EXPECT_EQ(result.tv_nsec, 0);
result = TimeDelta::FromMicroseconds(1).ToTimeSpec();
EXPECT_EQ(result.tv_sec, 0);
EXPECT_EQ(result.tv_nsec, 1000);
result = TimeDelta::FromMicroseconds(
Time::kMicrosecondsPerSecond + 1).ToTimeSpec();
EXPECT_EQ(result.tv_sec, 1);
EXPECT_EQ(result.tv_nsec, 1000);
}
#endif
TEST(TimeDelta, WindowsEpoch) {
Time::Exploded exploded;
exploded.year = 1970;
exploded.month = 1;
exploded.day_of_week = 0;
exploded.day_of_month = 1;
exploded.hour = 0;
exploded.minute = 0;
exploded.second = 0;
exploded.millisecond = 0;
Time t = Time::FromUTCExploded(exploded);
EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue());
}