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 "chrome/browser/google_apis/time_util.h"
7 #include "base/i18n/time_formatting.h"
9 #include "base/utf_string_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 #if defined(OS_CHROMEOS)
13 #include "chrome/browser/chromeos/system/timezone_settings.h"
16 namespace google_apis
{
20 std::string
FormatTime(const base::Time
& time
) {
21 return UTF16ToUTF8(TimeFormatShortDateAndTime(time
));
26 #if defined(OS_CHROMEOS)
27 // ChromeOS can test utilities using ICU library.
28 TEST(TimeUtilTest
, GetTimeFromStringLocalTimezoneForChromeOs
) {
29 // Creates time object GMT.
30 base::Time::Exploded exploded
= {2012, 7, 0, 14, 1, 3, 21, 151};
31 base::Time target_time
= base::Time::FromUTCExploded(exploded
);
33 // Creates time object as the local time.
35 ASSERT_TRUE(GetTimeFromString("2012-07-14T01:03:21.151", &test_time
));
37 // Gets the offset between the local time and GMT.
38 const icu::TimeZone
& tz
=
39 chromeos::system::TimezoneSettings::GetInstance()->GetTimezone();
40 UErrorCode status
= U_ZERO_ERROR
;
41 int millisecond_in_day
= ((1 * 60 + 3) * 60 + 21) * 1000 + 151;
42 int offset
= tz
.getOffset(1, 2012, 7, 14, 1, millisecond_in_day
, status
);
43 ASSERT_TRUE(U_SUCCESS(status
));
45 EXPECT_EQ((target_time
- test_time
).InMilliseconds(), offset
);
49 TEST(TimeUtilTest
, GetTimeFromStringLocalTimezone
) {
50 // Creates local time objects from exploded structure.
51 base::Time::Exploded exploded
= {2013, 1, 0, 15, 17, 11, 35, 374};
52 base::Time local_time
= base::Time::FromLocalExploded(exploded
);
54 // Creates local time object, parsing time string.
56 ASSERT_TRUE(GetTimeFromString("2013-01-15T17:11:35.374", &test_time
));
58 // Compare the time objects.
59 EXPECT_EQ(local_time
, test_time
);
62 TEST(TimeUtilTest
, GetTimeFromStringTimezone
) {
63 base::Time target_time
;
65 // Creates the target time.
66 EXPECT_TRUE(GetTimeFromString("2012-07-14T01:03:21.151Z", &target_time
));
68 // Tests positive offset (hour only).
69 EXPECT_TRUE(GetTimeFromString("2012-07-14T02:03:21.151+01", &test_time
));
70 EXPECT_EQ(FormatTime(target_time
), FormatTime(test_time
));
72 // Tests positive offset (hour and minutes).
73 EXPECT_TRUE(GetTimeFromString("2012-07-14T07:33:21.151+06:30", &test_time
));
74 EXPECT_EQ(FormatTime(target_time
), FormatTime(test_time
));
76 // Tests negative offset.
77 EXPECT_TRUE(GetTimeFromString("2012-07-13T18:33:21.151-06:30", &test_time
));
78 EXPECT_EQ(FormatTime(target_time
), FormatTime(test_time
));
81 TEST(TimeUtilTest
, GetTimeFromString
) {
84 base::Time::Exploded target_time1
= {2005, 1, 0, 7, 8, 2, 0, 0};
85 EXPECT_TRUE(GetTimeFromString("2005-01-07T08:02:00Z", &test_time
));
86 EXPECT_EQ(FormatTime(base::Time::FromUTCExploded(target_time1
)),
87 FormatTime(test_time
));
89 base::Time::Exploded target_time2
= {2005, 8, 0, 9, 17, 57, 0, 0};
90 EXPECT_TRUE(GetTimeFromString("2005-08-09T09:57:00-08:00", &test_time
));
91 EXPECT_EQ(FormatTime(base::Time::FromUTCExploded(target_time2
)),
92 FormatTime(test_time
));
94 base::Time::Exploded target_time3
= {2005, 1, 0, 7, 8, 2, 0, 123};
95 EXPECT_TRUE(GetTimeFromString("2005-01-07T08:02:00.123Z", &test_time
));
96 EXPECT_EQ(FormatTime(base::Time::FromUTCExploded(target_time3
)),
97 FormatTime(test_time
));
100 TEST(TimeUtilTest
, FormatTimeAsString
) {
101 base::Time::Exploded exploded_time
= {2012, 7, 0, 19, 15, 59, 13, 123};
102 base::Time time
= base::Time::FromUTCExploded(exploded_time
);
103 EXPECT_EQ("2012-07-19T15:59:13.123Z", FormatTimeAsString(time
));
107 } // namespace google_apis