summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorshinyak@google.com <shinyak@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2011-08-03 05:28:10 +0000
committershinyak@google.com <shinyak@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2011-08-03 05:28:10 +0000
commit46470aab29e72565ff358331094402b68d053e30 (patch)
tree4f70762af8aadd3673aa2cb61adc433721900388 /base
parent4e7c71a026ec06c9a4210e84680fc368e1b2f168 (diff)
downloadchromium_src-46470aab29e72565ff358331094402b68d053e30.zip
chromium_src-46470aab29e72565ff358331094402b68d053e30.tar.gz
chromium_src-46470aab29e72565ff358331094402b68d053e30.tar.bz2
Removed wchat_t from Time::FromString.
Also, some of the test case are moved for pr_time_unittests to time_unittests. BUG=77962 TEST=base_unittests:TimeTest.* Review URL: http://codereview.chromium.org/7492063 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@95207 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/file_util_unittest.cc4
-rw-r--r--base/metrics/field_trial.cc3
-rw-r--r--base/pr_time_unittest.cc118
-rw-r--r--base/time.cc12
-rw-r--r--base/time.h2
-rw-r--r--base/time_unittest.cc219
6 files changed, 224 insertions, 134 deletions
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc
index fe3927d..c60a703 100644
--- a/base/file_util_unittest.cc
+++ b/base/file_util_unittest.cc
@@ -1779,13 +1779,13 @@ TEST_F(FileUtilTest, TouchFile) {
base::Time access_time;
// This timestamp is divisible by one day (in local timezone),
// to make it work on FAT too.
- ASSERT_TRUE(base::Time::FromString(L"Wed, 16 Nov 1994, 00:00:00",
+ ASSERT_TRUE(base::Time::FromString("Wed, 16 Nov 1994, 00:00:00",
&access_time));
base::Time modification_time;
// Note that this timestamp is divisible by two (seconds) - FAT stores
// modification times with 2s resolution.
- ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
+ ASSERT_TRUE(base::Time::FromString("Tue, 15 Nov 1994, 12:45:26 GMT",
&modification_time));
ASSERT_TRUE(file_util::TouchFile(foobar, access_time, modification_time));
diff --git a/base/metrics/field_trial.cc b/base/metrics/field_trial.cc
index 2c87f07..99dce41 100644
--- a/base/metrics/field_trial.cc
+++ b/base/metrics/field_trial.cc
@@ -157,8 +157,7 @@ FieldTrial::~FieldTrial() {}
Time FieldTrial::GetBuildTime() {
Time integral_build_time;
const char* kDateTime = __DATE__ " " __TIME__;
- bool result = Time::FromString(ASCIIToWide(kDateTime).c_str(),
- &integral_build_time);
+ bool result = Time::FromString(kDateTime, &integral_build_time);
DCHECK(result);
return integral_build_time;
}
diff --git a/base/pr_time_unittest.cc b/base/pr_time_unittest.cc
index 646eb16..5c8bad1 100644
--- a/base/pr_time_unittest.cc
+++ b/base/pr_time_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 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.
@@ -132,122 +132,6 @@ TEST_F(PRTimeTest, ParseTimeTest9) {
EXPECT_EQ(parsed_time, comparison_time_pdt);
}
-// This tests the Time::FromString wrapper over PR_ParseTimeString
-TEST_F(PRTimeTest, ParseTimeTest10) {
- Time parsed_time;
- bool result = Time::FromString(L"15/10/07 12:45", &parsed_time);
- EXPECT_TRUE(result);
-
- time_t computed_time = parsed_time.ToTimeT();
- time_t time_to_compare = comparison_time_local_ /
- Time::kMicrosecondsPerSecond;
- EXPECT_EQ(computed_time, time_to_compare);
-}
-
-// This tests the Time::FromString wrapper over PR_ParseTimeString
-TEST_F(PRTimeTest, ParseTimeTest11) {
- Time parsed_time;
- bool result = Time::FromString(L"Mon, 15 Oct 2007 19:45:00 GMT",
- &parsed_time);
- EXPECT_TRUE(result);
-
- time_t computed_time = parsed_time.ToTimeT();
- time_t time_to_compare = comparison_time_pdt / Time::kMicrosecondsPerSecond;
- EXPECT_EQ(computed_time, time_to_compare);
-}
-
-// Test some of edge cases around epoch, etc.
-TEST_F(PRTimeTest, ParseTimeTestEpoch0) {
- Time parsed_time;
-
- // time_t == epoch == 0
- EXPECT_TRUE(Time::FromString(L"Thu Jan 01 01:00:00 +0100 1970",
- &parsed_time));
- EXPECT_EQ(0, parsed_time.ToTimeT());
- EXPECT_TRUE(Time::FromString(L"Thu Jan 01 00:00:00 GMT 1970",
- &parsed_time));
- EXPECT_EQ(0, parsed_time.ToTimeT());
-}
-
-TEST_F(PRTimeTest, ParseTimeTestEpoch1) {
- Time parsed_time;
-
- // time_t == 1 second after epoch == 1
- EXPECT_TRUE(Time::FromString(L"Thu Jan 01 01:00:01 +0100 1970",
- &parsed_time));
- EXPECT_EQ(1, parsed_time.ToTimeT());
- EXPECT_TRUE(Time::FromString(L"Thu Jan 01 00:00:01 GMT 1970",
- &parsed_time));
- EXPECT_EQ(1, parsed_time.ToTimeT());
-}
-
-TEST_F(PRTimeTest, ParseTimeTestEpoch2) {
- Time parsed_time;
-
- // time_t == 2 seconds after epoch == 2
- EXPECT_TRUE(Time::FromString(L"Thu Jan 01 01:00:02 +0100 1970",
- &parsed_time));
- EXPECT_EQ(2, parsed_time.ToTimeT());
- EXPECT_TRUE(Time::FromString(L"Thu Jan 01 00:00:02 GMT 1970",
- &parsed_time));
- EXPECT_EQ(2, parsed_time.ToTimeT());
-}
-
-TEST_F(PRTimeTest, ParseTimeTestEpochNeg1) {
- Time parsed_time;
-
- // time_t == 1 second before epoch == -1
- EXPECT_TRUE(Time::FromString(L"Thu Jan 01 00:59:59 +0100 1970",
- &parsed_time));
- EXPECT_EQ(-1, parsed_time.ToTimeT());
- EXPECT_TRUE(Time::FromString(L"Wed Dec 31 23:59:59 GMT 1969",
- &parsed_time));
- EXPECT_EQ(-1, parsed_time.ToTimeT());
-}
-
-// If time_t is 32 bits, a date after year 2038 will overflow time_t and
-// cause timegm() to return -1. The parsed time should not be 1 second
-// before epoch.
-TEST_F(PRTimeTest, ParseTimeTestEpochNotNeg1) {
- Time parsed_time;
-
- EXPECT_TRUE(Time::FromString(L"Wed Dec 31 23:59:59 GMT 2100",
- &parsed_time));
- EXPECT_NE(-1, parsed_time.ToTimeT());
-}
-
-TEST_F(PRTimeTest, ParseTimeTestEpochNeg2) {
- Time parsed_time;
-
- // time_t == 2 seconds before epoch == -2
- EXPECT_TRUE(Time::FromString(L"Thu Jan 01 00:59:58 +0100 1970",
- &parsed_time));
- EXPECT_EQ(-2, parsed_time.ToTimeT());
- EXPECT_TRUE(Time::FromString(L"Wed Dec 31 23:59:58 GMT 1969",
- &parsed_time));
- EXPECT_EQ(-2, parsed_time.ToTimeT());
-}
-
-TEST_F(PRTimeTest, ParseTimeTestEpoch1960) {
- Time parsed_time;
-
- // time_t before Epoch, in 1960
- EXPECT_TRUE(Time::FromString(L"Wed Jun 29 19:40:01 +0100 1960",
- &parsed_time));
- EXPECT_EQ(-299999999, parsed_time.ToTimeT());
- EXPECT_TRUE(Time::FromString(L"Wed Jun 29 18:40:01 GMT 1960",
- &parsed_time));
- EXPECT_EQ(-299999999, parsed_time.ToTimeT());
- EXPECT_TRUE(Time::FromString(L"Wed Jun 29 17:40:01 GMT 1960",
- &parsed_time));
- EXPECT_EQ(-300003599, parsed_time.ToTimeT());
-}
-
-TEST_F(PRTimeTest, ParseTimeTestEmpty) {
- Time parsed_time;
- EXPECT_FALSE(Time::FromString(L"", &parsed_time));
-}
-
// This test should not crash when compiled with Visual C++ 2005 (see
// http://crbug.com/4387).
TEST_F(PRTimeTest, ParseTimeTestOutOfRange) {
diff --git a/base/time.cc b/base/time.cc
index 766f599..859810e 100644
--- a/base/time.cc
+++ b/base/time.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 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.
@@ -98,16 +98,18 @@ Time Time::LocalMidnight() const {
}
// static
-bool Time::FromString(const wchar_t* time_string, Time* parsed_time) {
+bool Time::FromString(const char* time_string, Time* parsed_time) {
DCHECK((time_string != NULL) && (parsed_time != NULL));
- std::string ascii_time_string = SysWideToUTF8(time_string);
- if (ascii_time_string.length() == 0)
+
+ if (time_string[0] == '\0')
return false;
+
PRTime result_time = 0;
- PRStatus result = PR_ParseTimeString(ascii_time_string.c_str(), PR_FALSE,
+ PRStatus result = PR_ParseTimeString(time_string, PR_FALSE,
&result_time);
if (PR_SUCCESS != result)
return false;
+
result_time += kTimeTToMicrosecondsOffset;
*parsed_time = Time(result_time);
return true;
diff --git a/base/time.h b/base/time.h
index 1b05e24..9ece389 100644
--- a/base/time.h
+++ b/base/time.h
@@ -317,7 +317,7 @@ class BASE_API Time {
// in the input string, we assume local time.
// TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to
// a new time converter class.
- static bool FromString(const wchar_t* time_string, Time* parsed_time);
+ static bool FromString(const char* time_string, Time* parsed_time);
// For serializing, use FromInternalValue to reconstitute. Please don't use
// this and do arithmetic on it, as it is more error prone than using the
diff --git a/base/time_unittest.cc b/base/time_unittest.cc
index 6c64b48..6ba03b8 100644
--- a/base/time_unittest.cc
+++ b/base/time_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 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.
@@ -13,8 +13,42 @@ using base::Time;
using base::TimeDelta;
using base::TimeTicks;
+// Specialized test fixture allowing time strings without timezones to be
+// tested by comparing them to a known time in the local zone.
+// See also pr_time_unittests.cc
+class TimeTest : public testing::Test {
+ protected:
+ virtual void SetUp() {
+ // Use mktime to get a time_t, and turn it into a PRTime by converting
+ // seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This
+ // must be a time guaranteed to be outside of a DST fallback hour in
+ // any timezone.
+ struct tm local_comparison_tm = {
+ 0, // second
+ 45, // minute
+ 12, // hour
+ 15, // day of month
+ 10 - 1, // month
+ 2007 - 1900, // year
+ 0, // day of week (ignored, output only)
+ 0, // day of year (ignored, output only)
+ -1 // DST in effect, -1 tells mktime to figure it out
+ };
+
+ time_t converted_time = mktime(&local_comparison_tm);
+ ASSERT_GT(converted_time, 0);
+ comparison_time_local_ = Time::FromTimeT(converted_time);
+
+ // time_t representation of 15th Oct 2007 12:45:00 PDT
+ comparison_time_pdt_ = Time::FromTimeT(1192477500);
+ }
+
+ Time comparison_time_local_;
+ Time comparison_time_pdt_;
+};
+
// Test conversions to/from time_t and exploding/unexploding.
-TEST(Time, TimeT) {
+TEST_F(TimeTest, TimeT) {
// C library time and exploded time.
time_t now_t_1 = time(NULL);
struct tm tms;
@@ -52,7 +86,7 @@ TEST(Time, TimeT) {
EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue());
}
-TEST(Time, FromExplodedWithMilliseconds) {
+TEST_F(TimeTest, FromExplodedWithMilliseconds) {
// Some platform implementations of FromExploded are liable to drop
// milliseconds if we aren't careful.
Time now = Time::NowFromSystemTime();
@@ -65,14 +99,14 @@ TEST(Time, FromExplodedWithMilliseconds) {
EXPECT_EQ(exploded1.millisecond, exploded2.millisecond);
}
-TEST(Time, ZeroIsSymmetric) {
+TEST_F(TimeTest, ZeroIsSymmetric) {
Time zero_time(Time::FromTimeT(0));
EXPECT_EQ(0, zero_time.ToTimeT());
EXPECT_EQ(0.0, zero_time.ToDoubleT());
}
-TEST(Time, LocalExplode) {
+TEST_F(TimeTest, LocalExplode) {
Time a = Time::Now();
Time::Exploded exploded;
a.LocalExplode(&exploded);
@@ -85,7 +119,7 @@ TEST(Time, LocalExplode) {
EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
}
-TEST(Time, UTCExplode) {
+TEST_F(TimeTest, UTCExplode) {
Time a = Time::Now();
Time::Exploded exploded;
a.UTCExplode(&exploded);
@@ -94,7 +128,7 @@ TEST(Time, UTCExplode) {
EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
}
-TEST(Time, LocalMidnight) {
+TEST_F(TimeTest, LocalMidnight) {
Time::Exploded exploded;
Time::Now().LocalMidnight().LocalExplode(&exploded);
EXPECT_EQ(0, exploded.hour);
@@ -103,6 +137,177 @@ TEST(Time, LocalMidnight) {
EXPECT_EQ(0, exploded.millisecond);
}
+TEST_F(TimeTest, ParseTimeTest1) {
+ time_t current_time = 0;
+ time(&current_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, &current_time);
+ asctime_s(time_buf, arraysize(time_buf), &local_time);
+#elif defined(OS_POSIX)
+ localtime_r(&current_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, 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 some of edge cases around epoch, etc.
+TEST_F(TimeTest, ParseTimeTestEpoch0) {
+ Time parsed_time;
+
+ // time_t == epoch == 0
+ 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;
+
+ // time_t == 1 second after epoch == 1
+ 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;
+
+ // time_t == 2 seconds after epoch == 2
+ 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;
+
+ // time_t == 1 second before epoch == -1
+ 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());
+}
+
+// If time_t is 32 bits, a date after year 2038 will overflow time_t and
+// cause timegm() to return -1. The parsed time should not be 1 second
+// before epoch.
+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;
+
+ // time_t == 2 seconds before epoch == -2
+ 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;
+
+ // time_t before Epoch, in 1960
+ 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(TimeTicks, Deltas) {
for (int index = 0; index < 50; index++) {
TimeTicks ticks_start = TimeTicks::Now();