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