OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "google_apis/drive/time_util.h" | 5 #include "google_apis/drive/time_util.h" |
6 | 6 |
7 #include "base/i18n/time_formatting.h" | 7 #include "base/i18n/time_formatting.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 namespace google_apis { | 12 namespace google_apis { |
13 namespace util { | 13 namespace util { |
14 namespace { | 14 namespace { |
15 | 15 |
16 std::string FormatTime(const base::Time& time) { | 16 std::string FormatTime(const base::Time& time) { |
17 return base::UTF16ToUTF8(base::TimeFormatShortDateAndTime(time)); | 17 return base::UTF16ToUTF8(base::TimeFormatShortDateAndTime(time)); |
18 } | 18 } |
19 | 19 |
20 } // namespace | 20 } // namespace |
21 | 21 |
22 TEST(TimeUtilTest, GetTimeFromStringLocalTimezone) { | 22 TEST(TimeUtilTest, GetTimeFromStringLocalTimezone) { |
23 // Creates local time objects from exploded structure. | 23 // Creates local time objects from exploded structure. |
24 base::Time::Exploded exploded = {2013, 1, 0, 15, 17, 11, 35, 374}; | 24 base::Time::Exploded exploded = {2013, 1, 0, 15, 17, 11, 35, 374}; |
25 base::Time local_time = base::Time::FromLocalExploded(exploded); | 25 base::Time local_time; |
| 26 EXPECT_TRUE(base::Time::FromLocalExploded(exploded, &local_time)); |
26 | 27 |
27 // Creates local time object, parsing time string. Note that if there is | 28 // Creates local time object, parsing time string. Note that if there is |
28 // not timezone suffix, GetTimeFromString() will handle this as local time | 29 // not timezone suffix, GetTimeFromString() will handle this as local time |
29 // with FromLocalExploded(). | 30 // with FromLocalExploded(). |
30 base::Time test_time; | 31 base::Time test_time; |
31 ASSERT_TRUE(GetTimeFromString("2013-01-15T17:11:35.374", &test_time)); | 32 ASSERT_TRUE(GetTimeFromString("2013-01-15T17:11:35.374", &test_time)); |
32 | 33 |
33 // Compare the time objects. | 34 // Compare the time objects. |
34 EXPECT_EQ(local_time, test_time); | 35 EXPECT_EQ(local_time, test_time); |
35 } | 36 } |
(...skipping 12 matching lines...) Expand all Loading... |
48 EXPECT_TRUE(GetTimeFromString("2012-07-14T07:33:21.151+06:30", &test_time)); | 49 EXPECT_TRUE(GetTimeFromString("2012-07-14T07:33:21.151+06:30", &test_time)); |
49 EXPECT_EQ(FormatTime(target_time), FormatTime(test_time)); | 50 EXPECT_EQ(FormatTime(target_time), FormatTime(test_time)); |
50 | 51 |
51 // Tests negative offset. | 52 // Tests negative offset. |
52 EXPECT_TRUE(GetTimeFromString("2012-07-13T18:33:21.151-06:30", &test_time)); | 53 EXPECT_TRUE(GetTimeFromString("2012-07-13T18:33:21.151-06:30", &test_time)); |
53 EXPECT_EQ(FormatTime(target_time), FormatTime(test_time)); | 54 EXPECT_EQ(FormatTime(target_time), FormatTime(test_time)); |
54 } | 55 } |
55 | 56 |
56 TEST(TimeUtilTest, GetTimeFromStringBasic) { | 57 TEST(TimeUtilTest, GetTimeFromStringBasic) { |
57 base::Time test_time; | 58 base::Time test_time; |
| 59 base::Time out_time; |
58 | 60 |
59 // Test that the special timezone "Z" (UTC) is handled. | 61 // Test that the special timezone "Z" (UTC) is handled. |
60 base::Time::Exploded target_time1 = {2005, 1, 0, 7, 8, 2, 0, 0}; | 62 base::Time::Exploded target_time1 = {2005, 1, 0, 7, 8, 2, 0, 0}; |
61 EXPECT_TRUE(GetTimeFromString("2005-01-07T08:02:00Z", &test_time)); | 63 EXPECT_TRUE(GetTimeFromString("2005-01-07T08:02:00Z", &test_time)); |
62 EXPECT_EQ(FormatTime(base::Time::FromUTCExploded(target_time1)), | 64 |
63 FormatTime(test_time)); | 65 EXPECT_TRUE(base::Time::FromUTCExploded(target_time1, &out_time)); |
| 66 EXPECT_EQ(FormatTime(out_time), FormatTime(test_time)); |
64 | 67 |
65 // Test that a simple timezone "-08:00" is handled | 68 // Test that a simple timezone "-08:00" is handled |
66 // 17:57 - 8 hours = 09:57 | 69 // 17:57 - 8 hours = 09:57 |
67 base::Time::Exploded target_time2 = {2005, 8, 0, 9, 17, 57, 0, 0}; | 70 base::Time::Exploded target_time2 = {2005, 8, 0, 9, 17, 57, 0, 0}; |
68 EXPECT_TRUE(GetTimeFromString("2005-08-09T09:57:00-08:00", &test_time)); | 71 EXPECT_TRUE(GetTimeFromString("2005-08-09T09:57:00-08:00", &test_time)); |
69 EXPECT_EQ(FormatTime(base::Time::FromUTCExploded(target_time2)), | 72 EXPECT_TRUE(base::Time::FromUTCExploded(target_time2, &out_time)); |
70 FormatTime(test_time)); | 73 EXPECT_EQ(FormatTime(out_time), FormatTime(test_time)); |
71 | 74 |
72 // Test that milliseconds (.123) are handled. | 75 // Test that milliseconds (.123) are handled. |
73 base::Time::Exploded target_time3 = {2005, 1, 0, 7, 8, 2, 0, 123}; | 76 base::Time::Exploded target_time3 = {2005, 1, 0, 7, 8, 2, 0, 123}; |
74 EXPECT_TRUE(GetTimeFromString("2005-01-07T08:02:00.123Z", &test_time)); | 77 EXPECT_TRUE(GetTimeFromString("2005-01-07T08:02:00.123Z", &test_time)); |
75 EXPECT_EQ(FormatTime(base::Time::FromUTCExploded(target_time3)), | 78 EXPECT_TRUE(base::Time::FromUTCExploded(target_time3, &out_time)); |
76 FormatTime(test_time)); | 79 EXPECT_EQ(FormatTime(out_time), FormatTime(test_time)); |
77 } | 80 } |
78 | 81 |
79 TEST(TimeUtilTest, FormatTimeAsString) { | 82 TEST(TimeUtilTest, FormatTimeAsString) { |
80 base::Time::Exploded exploded_time = {2012, 7, 0, 19, 15, 59, 13, 123}; | 83 base::Time::Exploded exploded_time = {2012, 7, 0, 19, 15, 59, 13, 123}; |
81 base::Time time = base::Time::FromUTCExploded(exploded_time); | 84 base::Time time; |
| 85 EXPECT_TRUE(base::Time::FromUTCExploded(exploded_time, &time)); |
82 EXPECT_EQ("2012-07-19T15:59:13.123Z", FormatTimeAsString(time)); | 86 EXPECT_EQ("2012-07-19T15:59:13.123Z", FormatTimeAsString(time)); |
83 | 87 |
84 EXPECT_EQ("null", FormatTimeAsString(base::Time())); | 88 EXPECT_EQ("null", FormatTimeAsString(base::Time())); |
85 } | 89 } |
86 | 90 |
87 TEST(TimeUtilTest, FormatTimeAsStringLocalTime) { | 91 TEST(TimeUtilTest, FormatTimeAsStringLocalTime) { |
88 base::Time::Exploded exploded_time = {2012, 7, 0, 19, 15, 59, 13, 123}; | 92 base::Time::Exploded exploded_time = {2012, 7, 0, 19, 15, 59, 13, 123}; |
89 base::Time time = base::Time::FromLocalExploded(exploded_time); | 93 base::Time time; |
| 94 EXPECT_TRUE(base::Time::FromLocalExploded(exploded_time, &time)); |
90 EXPECT_EQ("2012-07-19T15:59:13.123", FormatTimeAsStringLocaltime(time)); | 95 EXPECT_EQ("2012-07-19T15:59:13.123", FormatTimeAsStringLocaltime(time)); |
91 | 96 |
92 EXPECT_EQ("null", FormatTimeAsStringLocaltime(base::Time())); | 97 EXPECT_EQ("null", FormatTimeAsStringLocaltime(base::Time())); |
93 } | 98 } |
94 | 99 |
95 } // namespace util | 100 } // namespace util |
96 } // namespace google_apis | 101 } // namespace google_apis |
OLD | NEW |