about summary refs log tree commit diff
path: root/absl/time/civil_time_test.cc
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2019-09-20T14·07-0700
committerShaindel Schwartz <shaindel@google.com>2019-09-20T14·40-0400
commitccdd1d57b6386ebc26fb0c7d99b604672437c124 (patch)
treebd2b466cbcf92fe15b5a6cd88a03ed8bdba33af7 /absl/time/civil_time_test.cc
parentddf8e52a2918dd0ccec75d3e2426125fa3926724 (diff)
Export of internal Abseil changes
--
509c39cb5aa70893a180e5625e06cd9f76061ecf by Shaindel Schwartz <shaindel@google.com>:

Release CivilTime parsing API.

PiperOrigin-RevId: 270262448
GitOrigin-RevId: 509c39cb5aa70893a180e5625e06cd9f76061ecf
Change-Id: I343eb3062cdf6a2c53e6fddcaa35eb25d7478b1a
Diffstat (limited to 'absl/time/civil_time_test.cc')
-rw-r--r--absl/time/civil_time_test.cc158
1 files changed, 158 insertions, 0 deletions
diff --git a/absl/time/civil_time_test.cc b/absl/time/civil_time_test.cc
index 03cd1f12d563..0ebd97adbc56 100644
--- a/absl/time/civil_time_test.cc
+++ b/absl/time/civil_time_test.cc
@@ -690,6 +690,69 @@ TEST(CivilTime, Format) {
   EXPECT_EQ("1970", absl::FormatCivilTime(y));
 }
 
+TEST(CivilTime, Parse) {
+  absl::CivilSecond ss;
+  absl::CivilMinute mm;
+  absl::CivilHour hh;
+  absl::CivilDay d;
+  absl::CivilMonth m;
+  absl::CivilYear y;
+
+  // CivilSecond OK; others fail
+  EXPECT_TRUE(absl::ParseCivilTime("2015-01-02T03:04:05", &ss));
+  EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(ss));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02T03:04:05", &mm));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02T03:04:05", &hh));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02T03:04:05", &d));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02T03:04:05", &m));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02T03:04:05", &y));
+
+  // CivilMinute OK; others fail
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02T03:04", &ss));
+  EXPECT_TRUE(absl::ParseCivilTime("2015-01-02T03:04", &mm));
+  EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(mm));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02T03:04", &hh));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02T03:04", &d));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02T03:04", &m));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02T03:04", &y));
+
+  // CivilHour OK; others fail
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02T03", &ss));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02T03", &mm));
+  EXPECT_TRUE(absl::ParseCivilTime("2015-01-02T03", &hh));
+  EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hh));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02T03", &d));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02T03", &m));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02T03", &y));
+
+  // CivilDay OK; others fail
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02", &ss));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02", &mm));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02", &hh));
+  EXPECT_TRUE(absl::ParseCivilTime("2015-01-02", &d));
+  EXPECT_EQ("2015-01-02", absl::FormatCivilTime(d));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02", &m));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01-02", &y));
+
+  // CivilMonth OK; others fail
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01", &ss));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01", &mm));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01", &hh));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01", &d));
+  EXPECT_TRUE(absl::ParseCivilTime("2015-01", &m));
+  EXPECT_EQ("2015-01", absl::FormatCivilTime(m));
+  EXPECT_FALSE(absl::ParseCivilTime("2015-01", &y));
+
+  // CivilYear OK; others fail
+  EXPECT_FALSE(absl::ParseCivilTime("2015", &ss));
+  EXPECT_FALSE(absl::ParseCivilTime("2015", &mm));
+  EXPECT_FALSE(absl::ParseCivilTime("2015", &hh));
+  EXPECT_FALSE(absl::ParseCivilTime("2015", &d));
+  EXPECT_FALSE(absl::ParseCivilTime("2015", &m));
+  EXPECT_TRUE(absl::ParseCivilTime("2015", &y));
+  EXPECT_EQ("2015", absl::FormatCivilTime(y));
+}
+
 TEST(CivilTime, FormatAndParseLenient) {
   absl::CivilSecond ss;
   EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(ss));
@@ -708,6 +771,101 @@ TEST(CivilTime, FormatAndParseLenient) {
 
   absl::CivilYear y;
   EXPECT_EQ("1970", absl::FormatCivilTime(y));
+
+  EXPECT_TRUE(absl::ParseLenientCivilTime("2015-01-02T03:04:05", &ss));
+  EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(ss));
+
+  EXPECT_TRUE(absl::ParseLenientCivilTime("2015-01-02T03:04:05", &mm));
+  EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(mm));
+
+  EXPECT_TRUE(absl::ParseLenientCivilTime("2015-01-02T03:04:05", &hh));
+  EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hh));
+
+  EXPECT_TRUE(absl::ParseLenientCivilTime("2015-01-02T03:04:05", &d));
+  EXPECT_EQ("2015-01-02", absl::FormatCivilTime(d));
+
+  EXPECT_TRUE(absl::ParseLenientCivilTime("2015-01-02T03:04:05", &m));
+  EXPECT_EQ("2015-01", absl::FormatCivilTime(m));
+
+  EXPECT_TRUE(absl::ParseLenientCivilTime("2015-01-02T03:04:05", &y));
+  EXPECT_EQ("2015", absl::FormatCivilTime(y));
+}
+
+TEST(CivilTime, ParseEdgeCases) {
+  absl::CivilSecond ss;
+  EXPECT_TRUE(
+      absl::ParseLenientCivilTime("9223372036854775807-12-31T23:59:59", &ss));
+  EXPECT_EQ("9223372036854775807-12-31T23:59:59", absl::FormatCivilTime(ss));
+  EXPECT_TRUE(
+      absl::ParseLenientCivilTime("-9223372036854775808-01-01T00:00:00", &ss));
+  EXPECT_EQ("-9223372036854775808-01-01T00:00:00", absl::FormatCivilTime(ss));
+
+  absl::CivilMinute mm;
+  EXPECT_TRUE(
+      absl::ParseLenientCivilTime("9223372036854775807-12-31T23:59", &mm));
+  EXPECT_EQ("9223372036854775807-12-31T23:59", absl::FormatCivilTime(mm));
+  EXPECT_TRUE(
+      absl::ParseLenientCivilTime("-9223372036854775808-01-01T00:00", &mm));
+  EXPECT_EQ("-9223372036854775808-01-01T00:00", absl::FormatCivilTime(mm));
+
+  absl::CivilHour hh;
+  EXPECT_TRUE(
+      absl::ParseLenientCivilTime("9223372036854775807-12-31T23", &hh));
+  EXPECT_EQ("9223372036854775807-12-31T23", absl::FormatCivilTime(hh));
+  EXPECT_TRUE(
+      absl::ParseLenientCivilTime("-9223372036854775808-01-01T00", &hh));
+  EXPECT_EQ("-9223372036854775808-01-01T00", absl::FormatCivilTime(hh));
+
+  absl::CivilDay d;
+  EXPECT_TRUE(absl::ParseLenientCivilTime("9223372036854775807-12-31", &d));
+  EXPECT_EQ("9223372036854775807-12-31", absl::FormatCivilTime(d));
+  EXPECT_TRUE(absl::ParseLenientCivilTime("-9223372036854775808-01-01", &d));
+  EXPECT_EQ("-9223372036854775808-01-01", absl::FormatCivilTime(d));
+
+  absl::CivilMonth m;
+  EXPECT_TRUE(absl::ParseLenientCivilTime("9223372036854775807-12", &m));
+  EXPECT_EQ("9223372036854775807-12", absl::FormatCivilTime(m));
+  EXPECT_TRUE(absl::ParseLenientCivilTime("-9223372036854775808-01", &m));
+  EXPECT_EQ("-9223372036854775808-01", absl::FormatCivilTime(m));
+
+  absl::CivilYear y;
+  EXPECT_TRUE(absl::ParseLenientCivilTime("9223372036854775807", &y));
+  EXPECT_EQ("9223372036854775807", absl::FormatCivilTime(y));
+  EXPECT_TRUE(absl::ParseLenientCivilTime("-9223372036854775808", &y));
+  EXPECT_EQ("-9223372036854775808", absl::FormatCivilTime(y));
+
+  // Tests some valid, but interesting, cases
+  EXPECT_TRUE(absl::ParseLenientCivilTime("0", &ss)) << ss;
+  EXPECT_EQ(absl::CivilYear(0), ss);
+  EXPECT_TRUE(absl::ParseLenientCivilTime("0-1", &ss)) << ss;
+  EXPECT_EQ(absl::CivilMonth(0, 1), ss);
+  EXPECT_TRUE(absl::ParseLenientCivilTime(" 2015 ", &ss)) << ss;
+  EXPECT_EQ(absl::CivilYear(2015), ss);
+  EXPECT_TRUE(absl::ParseLenientCivilTime(" 2015-6 ", &ss)) << ss;
+  EXPECT_EQ(absl::CivilMonth(2015, 6), ss);
+  EXPECT_TRUE(absl::ParseLenientCivilTime("2015-6-7", &ss)) << ss;
+  EXPECT_EQ(absl::CivilDay(2015, 6, 7), ss);
+  EXPECT_TRUE(absl::ParseLenientCivilTime(" 2015-6-7 ", &ss)) << ss;
+  EXPECT_EQ(absl::CivilDay(2015, 6, 7), ss);
+  EXPECT_TRUE(absl::ParseLenientCivilTime("2015-06-07T10:11:12 ", &ss)) << ss;
+  EXPECT_EQ(absl::CivilSecond(2015, 6, 7, 10, 11, 12), ss);
+  EXPECT_TRUE(absl::ParseLenientCivilTime(" 2015-06-07T10:11:12 ", &ss)) << ss;
+  EXPECT_EQ(absl::CivilSecond(2015, 6, 7, 10, 11, 12), ss);
+  EXPECT_TRUE(absl::ParseLenientCivilTime("-01-01", &ss)) << ss;
+  EXPECT_EQ(absl::CivilMonth(-1, 1), ss);
+
+  // Tests some invalid cases
+  EXPECT_FALSE(absl::ParseLenientCivilTime("01-01-2015", &ss)) << ss;
+  EXPECT_FALSE(absl::ParseLenientCivilTime("2015-", &ss)) << ss;
+  EXPECT_FALSE(absl::ParseLenientCivilTime("0xff-01", &ss)) << ss;
+  EXPECT_FALSE(absl::ParseLenientCivilTime("2015-02-30T04:05:06", &ss)) << ss;
+  EXPECT_FALSE(absl::ParseLenientCivilTime("2015-02-03T04:05:96", &ss)) << ss;
+  EXPECT_FALSE(absl::ParseLenientCivilTime("X2015-02-03T04:05:06", &ss)) << ss;
+  EXPECT_FALSE(absl::ParseLenientCivilTime("2015-02-03T04:05:003", &ss)) << ss;
+  EXPECT_FALSE(absl::ParseLenientCivilTime("2015 -02-03T04:05:06", &ss)) << ss;
+  EXPECT_FALSE(absl::ParseLenientCivilTime("2015-02-03-04:05:06", &ss)) << ss;
+  EXPECT_FALSE(absl::ParseLenientCivilTime("2015:02:03T04-05-06", &ss)) << ss;
+  EXPECT_FALSE(absl::ParseLenientCivilTime("9223372036854775808", &y)) << y;
 }
 
 TEST(CivilTime, OutputStream) {