Update V8 to version 4.6.55.
[chromium-blink-merge.git] / google_apis / drive / time_util_unittest.cc
blob6d3abf6717c54647c86a4393793d35d4decf28a8
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.
5 #include "google_apis/drive/time_util.h"
7 #include "base/i18n/time_formatting.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/time/time.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace google_apis {
13 namespace util {
14 namespace {
16 std::string FormatTime(const base::Time& time) {
17 return base::UTF16ToUTF8(base::TimeFormatShortDateAndTime(time));
20 } // namespace
22 TEST(TimeUtilTest, GetTimeFromStringLocalTimezone) {
23 // Creates local time objects from exploded structure.
24 base::Time::Exploded exploded = {2013, 1, 0, 15, 17, 11, 35, 374};
25 base::Time local_time = base::Time::FromLocalExploded(exploded);
27 // Creates local time object, parsing time string. Note that if there is
28 // not timezone suffix, GetTimeFromString() will handle this as local time
29 // with FromLocalExploded().
30 base::Time test_time;
31 ASSERT_TRUE(GetTimeFromString("2013-01-15T17:11:35.374", &test_time));
33 // Compare the time objects.
34 EXPECT_EQ(local_time, test_time);
37 TEST(TimeUtilTest, GetTimeFromStringNonTrivialTimezones) {
38 base::Time target_time;
39 base::Time test_time;
40 // Creates the target time.
41 EXPECT_TRUE(GetTimeFromString("2012-07-14T01:03:21.151Z", &target_time));
43 // Tests positive offset (hour only).
44 EXPECT_TRUE(GetTimeFromString("2012-07-14T02:03:21.151+01", &test_time));
45 EXPECT_EQ(FormatTime(target_time), FormatTime(test_time));
47 // Tests positive offset (hour and minutes).
48 EXPECT_TRUE(GetTimeFromString("2012-07-14T07:33:21.151+06:30", &test_time));
49 EXPECT_EQ(FormatTime(target_time), FormatTime(test_time));
51 // Tests negative offset.
52 EXPECT_TRUE(GetTimeFromString("2012-07-13T18:33:21.151-06:30", &test_time));
53 EXPECT_EQ(FormatTime(target_time), FormatTime(test_time));
56 TEST(TimeUtilTest, GetTimeFromStringBasic) {
57 base::Time test_time;
59 // Test that the special timezone "Z" (UTC) is handled.
60 base::Time::Exploded target_time1 = {2005, 1, 0, 7, 8, 2, 0, 0};
61 EXPECT_TRUE(GetTimeFromString("2005-01-07T08:02:00Z", &test_time));
62 EXPECT_EQ(FormatTime(base::Time::FromUTCExploded(target_time1)),
63 FormatTime(test_time));
65 // Test that a simple timezone "-08:00" is handled
66 // 17:57 - 8 hours = 09:57
67 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));
69 EXPECT_EQ(FormatTime(base::Time::FromUTCExploded(target_time2)),
70 FormatTime(test_time));
72 // Test that milliseconds (.123) are handled.
73 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));
75 EXPECT_EQ(FormatTime(base::Time::FromUTCExploded(target_time3)),
76 FormatTime(test_time));
79 TEST(TimeUtilTest, FormatTimeAsString) {
80 base::Time::Exploded exploded_time = {2012, 7, 0, 19, 15, 59, 13, 123};
81 base::Time time = base::Time::FromUTCExploded(exploded_time);
82 EXPECT_EQ("2012-07-19T15:59:13.123Z", FormatTimeAsString(time));
84 EXPECT_EQ("null", FormatTimeAsString(base::Time()));
87 TEST(TimeUtilTest, FormatTimeAsStringLocalTime) {
88 base::Time::Exploded exploded_time = {2012, 7, 0, 19, 15, 59, 13, 123};
89 base::Time time = base::Time::FromLocalExploded(exploded_time);
90 EXPECT_EQ("2012-07-19T15:59:13.123", FormatTimeAsStringLocaltime(time));
92 EXPECT_EQ("null", FormatTimeAsStringLocaltime(base::Time()));
95 } // namespace util
96 } // namespace google_apis