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 "google_apis/drive/time_util.h" 6 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" 11 12namespace google_apis { 13namespace util { 14namespace { 15 16std::string FormatTime(const base::Time& time) { 17 return base::UTF16ToUTF8(base::TimeFormatShortDateAndTime(time)); 18} 19 20} // namespace 21 22TEST(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); 26 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)); 32 33 // Compare the time objects. 34 EXPECT_EQ(local_time, test_time); 35} 36 37TEST(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)); 42 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)); 46 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)); 50 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)); 54} 55 56TEST(TimeUtilTest, GetTimeFromStringBasic) { 57 base::Time test_time; 58 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)); 64 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)); 71 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)); 77} 78 79TEST(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)); 83 84 EXPECT_EQ("null", FormatTimeAsString(base::Time())); 85} 86 87TEST(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)); 91 92 EXPECT_EQ("null", FormatTimeAsStringLocaltime(base::Time())); 93} 94 95} // namespace util 96} // namespace google_apis 97