time_util_unittest.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include "google_apis/common/time_util.h"
  5. #include "base/i18n/time_formatting.h"
  6. #include "base/strings/utf_string_conversions.h"
  7. #include "base/time/time.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace google_apis {
  10. namespace util {
  11. namespace {
  12. std::string FormatTime(const base::Time& time) {
  13. return base::UTF16ToUTF8(base::TimeFormatShortDateAndTime(time));
  14. }
  15. } // namespace
  16. TEST(TimeUtilTest, GetTimeFromStringLocalTimezone) {
  17. // Creates local time objects from exploded structure.
  18. base::Time::Exploded exploded = {2013, 1, 0, 15, 17, 11, 35, 374};
  19. base::Time local_time;
  20. EXPECT_TRUE(base::Time::FromLocalExploded(exploded, &local_time));
  21. // Creates local time object, parsing time string. Note that if there is
  22. // not timezone suffix, GetTimeFromString() will handle this as local time
  23. // with FromLocalExploded().
  24. base::Time test_time;
  25. ASSERT_TRUE(GetTimeFromString("2013-01-15T17:11:35.374", &test_time));
  26. // Compare the time objects.
  27. EXPECT_EQ(local_time, test_time);
  28. }
  29. TEST(TimeUtilTest, GetTimeFromStringNonTrivialTimezones) {
  30. base::Time target_time;
  31. base::Time test_time;
  32. // Creates the target time.
  33. EXPECT_TRUE(GetTimeFromString("2012-07-14T01:03:21.151Z", &target_time));
  34. // Tests positive offset (hour only).
  35. EXPECT_TRUE(GetTimeFromString("2012-07-14T02:03:21.151+01", &test_time));
  36. EXPECT_EQ(FormatTime(target_time), FormatTime(test_time));
  37. // Tests positive offset (hour and minutes).
  38. EXPECT_TRUE(GetTimeFromString("2012-07-14T07:33:21.151+06:30", &test_time));
  39. EXPECT_EQ(FormatTime(target_time), FormatTime(test_time));
  40. // Tests negative offset.
  41. EXPECT_TRUE(GetTimeFromString("2012-07-13T18:33:21.151-06:30", &test_time));
  42. EXPECT_EQ(FormatTime(target_time), FormatTime(test_time));
  43. }
  44. TEST(TimeUtilTest, GetTimeFromStringBasic) {
  45. base::Time test_time;
  46. base::Time out_time;
  47. // Test that the special timezone "Z" (UTC) is handled.
  48. base::Time::Exploded target_time1 = {2005, 1, 0, 7, 8, 2, 0, 0};
  49. EXPECT_TRUE(GetTimeFromString("2005-01-07T08:02:00Z", &test_time));
  50. EXPECT_TRUE(base::Time::FromUTCExploded(target_time1, &out_time));
  51. EXPECT_EQ(FormatTime(out_time), FormatTime(test_time));
  52. // Test that a simple timezone "-08:00" is handled
  53. // 17:57 - 8 hours = 09:57
  54. base::Time::Exploded target_time2 = {2005, 8, 0, 9, 17, 57, 0, 0};
  55. EXPECT_TRUE(GetTimeFromString("2005-08-09T09:57:00-08:00", &test_time));
  56. EXPECT_TRUE(base::Time::FromUTCExploded(target_time2, &out_time));
  57. EXPECT_EQ(FormatTime(out_time), FormatTime(test_time));
  58. // Test that milliseconds (.123) are handled.
  59. base::Time::Exploded target_time3 = {2005, 1, 0, 7, 8, 2, 0, 123};
  60. EXPECT_TRUE(GetTimeFromString("2005-01-07T08:02:00.123Z", &test_time));
  61. EXPECT_TRUE(base::Time::FromUTCExploded(target_time3, &out_time));
  62. EXPECT_EQ(FormatTime(out_time), FormatTime(test_time));
  63. }
  64. TEST(TimeUtilTest, GetDateOnlyFromStringBasic) {
  65. base::Time test_time;
  66. base::Time out_time;
  67. base::Time::Exploded target_time1 = {2009, 10, 0, 23};
  68. EXPECT_TRUE(GetDateOnlyFromString("2009-10-23", &test_time));
  69. EXPECT_TRUE(base::Time::FromUTCExploded(target_time1, &out_time));
  70. EXPECT_EQ(FormatTime(out_time), FormatTime(test_time));
  71. }
  72. TEST(TimeUtilTest, FormatTimeAsString) {
  73. base::Time::Exploded exploded_time = {2012, 7, 0, 19, 15, 59, 13, 123};
  74. base::Time time;
  75. EXPECT_TRUE(base::Time::FromUTCExploded(exploded_time, &time));
  76. EXPECT_EQ("2012-07-19T15:59:13.123Z", FormatTimeAsString(time));
  77. EXPECT_EQ("null", FormatTimeAsString(base::Time()));
  78. }
  79. TEST(TimeUtilTest, FormatTimeAsStringLocalTime) {
  80. base::Time::Exploded exploded_time = {2012, 7, 0, 19, 15, 59, 13, 123};
  81. base::Time time;
  82. EXPECT_TRUE(base::Time::FromLocalExploded(exploded_time, &time));
  83. EXPECT_EQ("2012-07-19T15:59:13.123", FormatTimeAsStringLocaltime(time));
  84. EXPECT_EQ("null", FormatTimeAsStringLocaltime(base::Time()));
  85. }
  86. } // namespace util
  87. } // namespace google_apis