blob: 5c8bad1d5eff49ee999df8357b4d71ccd7c2f248 [file] [log] [blame]
shinyak@google.com44706852011-08-03 14:28:10 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
mmentovai@google.com9d0fbde2008-08-13 01:04:02 +09005#include <time.h>
6
initial.commit3f4a7322008-07-27 06:49:38 +09007#include "base/third_party/nspr/prtime.h"
8#include "base/time.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
dsh@google.com0f8dd262008-10-28 05:43:33 +090011using base::Time;
12
initial.commit3f4a7322008-07-27 06:49:38 +090013namespace {
14
initial.commit3f4a7322008-07-27 06:49:38 +090015// time_t representation of 15th Oct 2007 12:45:00 PDT
mmentovai@google.com9d0fbde2008-08-13 01:04:02 +090016PRTime comparison_time_pdt = 1192477500 * Time::kMicrosecondsPerSecond;
initial.commit3f4a7322008-07-27 06:49:38 +090017
18// Specialized test fixture allowing time strings without timezones to be
19// tested by comparing them to a known time in the local zone.
20class PRTimeTest : public testing::Test {
21 protected:
22 virtual void SetUp() {
23 // Use mktime to get a time_t, and turn it into a PRTime by converting
24 // seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This
25 // must be a time guaranteed to be outside of a DST fallback hour in
26 // any timezone.
27 struct tm local_comparison_tm = {
28 0, // second
29 45, // minute
30 12, // hour
31 15, // day of month
32 10 - 1, // month
33 2007 - 1900, // year
34 0, // day of week (ignored, output only)
35 0, // day of year (ignored, output only)
36 -1 // DST in effect, -1 tells mktime to figure it out
37 };
38 comparison_time_local_ = mktime(&local_comparison_tm) *
mmentovai@google.com9d0fbde2008-08-13 01:04:02 +090039 Time::kMicrosecondsPerSecond;
initial.commit3f4a7322008-07-27 06:49:38 +090040 ASSERT_GT(comparison_time_local_, 0);
41 }
42
43 PRTime comparison_time_local_;
44};
45
initial.commit3f4a7322008-07-27 06:49:38 +090046// Tests the PR_ParseTimeString nspr helper function for
47// a variety of time strings.
48TEST_F(PRTimeTest, ParseTimeTest1) {
pinkerton@google.com54907a82008-08-12 08:07:19 +090049 time_t current_time = 0;
initial.commit3f4a7322008-07-27 06:49:38 +090050 time(&current_time);
51
pinkerton@google.com54907a82008-08-12 08:07:19 +090052 const int BUFFER_SIZE = 64;
mmentovai@google.com9d0fbde2008-08-13 01:04:02 +090053 struct tm local_time = {0};
pinkerton@google.com54907a82008-08-12 08:07:19 +090054 char time_buf[BUFFER_SIZE] = {0};
55#if defined(OS_WIN)
mmentovai@google.com9d0fbde2008-08-13 01:04:02 +090056 localtime_s(&local_time, &current_time);
initial.commit3f4a7322008-07-27 06:49:38 +090057 asctime_s(time_buf, arraysize(time_buf), &local_time);
pinkerton@google.com54907a82008-08-12 08:07:19 +090058#elif defined(OS_POSIX)
mmentovai@google.com9d0fbde2008-08-13 01:04:02 +090059 localtime_r(&current_time, &local_time);
pinkerton@google.com54907a82008-08-12 08:07:19 +090060 asctime_r(&local_time, time_buf);
61#endif
mmentovai@google.com9d0fbde2008-08-13 01:04:02 +090062
pinkerton@google.com54907a82008-08-12 08:07:19 +090063 PRTime current_time64 = static_cast<PRTime>(current_time) * PR_USEC_PER_SEC;
initial.commit3f4a7322008-07-27 06:49:38 +090064
65 PRTime parsed_time = 0;
66 PRStatus result = PR_ParseTimeString(time_buf, PR_FALSE, &parsed_time);
67 EXPECT_EQ(PR_SUCCESS, result);
pinkerton@google.com54907a82008-08-12 08:07:19 +090068 EXPECT_EQ(current_time64, parsed_time);
initial.commit3f4a7322008-07-27 06:49:38 +090069}
70
71TEST_F(PRTimeTest, ParseTimeTest2) {
72 PRTime parsed_time = 0;
73 PRStatus result = PR_ParseTimeString("Mon, 15 Oct 2007 19:45:00 GMT",
74 PR_FALSE, &parsed_time);
75 EXPECT_EQ(PR_SUCCESS, result);
76 EXPECT_EQ(parsed_time, comparison_time_pdt);
77}
78
79TEST_F(PRTimeTest, ParseTimeTest3) {
80 PRTime parsed_time = 0;
81 PRStatus result = PR_ParseTimeString("15 Oct 07 12:45:00", PR_FALSE,
82 &parsed_time);
83 EXPECT_EQ(PR_SUCCESS, result);
84 EXPECT_EQ(parsed_time, comparison_time_local_);
85}
86
87TEST_F(PRTimeTest, ParseTimeTest4) {
88 PRTime parsed_time = 0;
89 PRStatus result = PR_ParseTimeString("15 Oct 07 19:45 GMT", PR_FALSE,
90 &parsed_time);
91 EXPECT_EQ(PR_SUCCESS, result);
92 EXPECT_EQ(parsed_time, comparison_time_pdt);
93}
94
95TEST_F(PRTimeTest, ParseTimeTest5) {
96 PRTime parsed_time = 0;
97 PRStatus result = PR_ParseTimeString("Mon Oct 15 12:45 PDT 2007",
98 PR_FALSE, &parsed_time);
99 EXPECT_EQ(PR_SUCCESS, result);
100 EXPECT_EQ(parsed_time, comparison_time_pdt);
101}
102
103TEST_F(PRTimeTest, ParseTimeTest6) {
104 PRTime parsed_time = 0;
105 PRStatus result = PR_ParseTimeString("Monday, Oct 15, 2007 12:45 PM",
106 PR_FALSE, &parsed_time);
107 EXPECT_EQ(PR_SUCCESS, result);
108 EXPECT_EQ(parsed_time, comparison_time_local_);
109}
110
111TEST_F(PRTimeTest, ParseTimeTest7) {
112 PRTime parsed_time = 0;
113 PRStatus result = PR_ParseTimeString("10/15/07 12:45:00 PM", PR_FALSE,
114 &parsed_time);
115 EXPECT_EQ(PR_SUCCESS, result);
116 EXPECT_EQ(parsed_time, comparison_time_local_);
117}
118
119TEST_F(PRTimeTest, ParseTimeTest8) {
120 PRTime parsed_time = 0;
121 PRStatus result = PR_ParseTimeString("15-OCT-2007 12:45pm", PR_FALSE,
122 &parsed_time);
123 EXPECT_EQ(PR_SUCCESS, result);
124 EXPECT_EQ(parsed_time, comparison_time_local_);
125}
126
127TEST_F(PRTimeTest, ParseTimeTest9) {
128 PRTime parsed_time = 0;
129 PRStatus result = PR_ParseTimeString("16 Oct 2007 4:45-JST (Tuesday)",
130 PR_FALSE, &parsed_time);
131 EXPECT_EQ(PR_SUCCESS, result);
132 EXPECT_EQ(parsed_time, comparison_time_pdt);
133}
134
maruel@chromium.org3be14ad2011-04-27 04:07:08 +0900135// This test should not crash when compiled with Visual C++ 2005 (see
136// http://crbug.com/4387).
137TEST_F(PRTimeTest, ParseTimeTestOutOfRange) {
138 PRTime parsed_time = 0;
139 // Note the lack of timezone in the time string. The year has to be 3001.
140 // The date has to be after 23:59:59, December 31, 3000, US Pacific Time, so
141 // we use January 2, 3001 to make sure it's after the magic maximum in any
142 // timezone.
143 PRStatus result = PR_ParseTimeString("Sun Jan 2 00:00:00 3001",
144 PR_FALSE, &parsed_time);
145 EXPECT_EQ(PR_SUCCESS, result);
146}
147
148TEST_F(PRTimeTest, ParseTimeTestNotNormalized1) {
149 PRTime parsed_time = 0;
150 PRStatus result = PR_ParseTimeString("Mon Oct 15 12:44:60 PDT 2007",
151 PR_FALSE, &parsed_time);
152 EXPECT_EQ(PR_SUCCESS, result);
153 EXPECT_EQ(comparison_time_pdt, parsed_time);
154}
155
156TEST_F(PRTimeTest, ParseTimeTestNotNormalized2) {
157 PRTime parsed_time = 0;
158 PRStatus result = PR_ParseTimeString("Sun Oct 14 36:45 PDT 2007",
159 PR_FALSE, &parsed_time);
160 EXPECT_EQ(PR_SUCCESS, result);
161 EXPECT_EQ(comparison_time_pdt, parsed_time);
162}
wtc@chromium.org40249922009-03-03 10:24:01 +0900163
mark@chromium.org17684802008-09-10 09:16:28 +0900164} // namespace