summaryrefslogtreecommitdiff
path: root/absl/time
diff options
context:
space:
mode:
authorGravatar Gennadiy Rozental <rogeeff@google.com>2022-10-18 02:34:29 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2022-10-18 02:35:17 -0700
commit4bc7568c70822dae40eea3f27a3051a618021651 (patch)
treeb7e9ef7372a4ff1daad77af2eb84e2b230dfc506 /absl/time
parent94433ef0615616b1ccb8b2193bde777c10406ac0 (diff)
Eliminate use of internal interfaces and make the test portable and expose it to OSS.
PiperOrigin-RevId: 481865072 Change-Id: I46364ec07df6aaff25911f8bd9e40dfee6c60626
Diffstat (limited to 'absl/time')
-rw-r--r--absl/time/BUILD.bazel26
-rw-r--r--absl/time/CMakeLists.txt13
-rw-r--r--absl/time/civil_time.cc26
-rw-r--r--absl/time/civil_time.h25
-rw-r--r--absl/time/flag_test.cc147
5 files changed, 237 insertions, 0 deletions
diff --git a/absl/time/BUILD.bazel b/absl/time/BUILD.bazel
index aa07df3d..b5f03980 100644
--- a/absl/time/BUILD.bazel
+++ b/absl/time/BUILD.bazel
@@ -45,12 +45,14 @@ cc_library(
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
"//absl/base",
+ "//absl/base:config",
"//absl/base:core_headers",
"//absl/base:raw_logging_internal",
"//absl/numeric:int128",
"//absl/strings",
"//absl/time/internal/cctz:civil_time",
"//absl/time/internal/cctz:time_zone",
+ "//absl/types:optional",
],
)
@@ -98,6 +100,30 @@ cc_test(
)
cc_test(
+ name = "flag_test",
+ srcs = [
+ "flag_test.cc",
+ ],
+ copts = ABSL_TEST_COPTS,
+ linkopts = ABSL_DEFAULT_LINKOPTS,
+ tags = [
+ "no_test_android_arm",
+ "no_test_android_arm64",
+ "no_test_android_x86",
+ "no_test_ios_x86_64",
+ "no_test_loonix",
+ "no_test_msvc_x64",
+ "no_test_wasm",
+ ],
+ deps = [
+ ":time",
+ "//absl/flags:flag",
+ "//absl/flags:reflection",
+ "@com_google_googletest//:gtest_main",
+ ],
+)
+
+cc_test(
name = "time_benchmark",
srcs = [
"civil_time_benchmark.cc",
diff --git a/absl/time/CMakeLists.txt b/absl/time/CMakeLists.txt
index debab3ba..1580a303 100644
--- a/absl/time/CMakeLists.txt
+++ b/absl/time/CMakeLists.txt
@@ -127,3 +127,16 @@ absl_cc_test(
absl::time_zone
GTest::gmock_main
)
+
+absl_cc_test(
+ NAME
+ flag_test
+ SRCS
+ "flag_test.cc"
+ COPTS
+ ${ABSL_TEST_COPTS}
+ DEPS
+ absl::flags
+ absl::flags_reflection
+ GTest::gmock_main
+)
diff --git a/absl/time/civil_time.cc b/absl/time/civil_time.cc
index 6a231edb..65df39d7 100644
--- a/absl/time/civil_time.cc
+++ b/absl/time/civil_time.cc
@@ -15,6 +15,7 @@
#include "absl/time/civil_time.h"
#include <cstdlib>
+#include <ostream>
#include <string>
#include "absl/strings/str_cat.h"
@@ -167,6 +168,31 @@ std::ostream& operator<<(std::ostream& os, CivilSecond s) {
return os << FormatCivilTime(s);
}
+bool AbslParseFlag(string_view s, CivilSecond* c, std::string*) {
+ return ParseLenientCivilTime(s, c);
+}
+bool AbslParseFlag(string_view s, CivilMinute* c, std::string*) {
+ return ParseLenientCivilTime(s, c);
+}
+bool AbslParseFlag(string_view s, CivilHour* c, std::string*) {
+ return ParseLenientCivilTime(s, c);
+}
+bool AbslParseFlag(string_view s, CivilDay* c, std::string*) {
+ return ParseLenientCivilTime(s, c);
+}
+bool AbslParseFlag(string_view s, CivilMonth* c, std::string*) {
+ return ParseLenientCivilTime(s, c);
+}
+bool AbslParseFlag(string_view s, CivilYear* c, std::string*) {
+ return ParseLenientCivilTime(s, c);
+}
+std::string AbslUnparseFlag(CivilSecond c) { return FormatCivilTime(c); }
+std::string AbslUnparseFlag(CivilMinute c) { return FormatCivilTime(c); }
+std::string AbslUnparseFlag(CivilHour c) { return FormatCivilTime(c); }
+std::string AbslUnparseFlag(CivilDay c) { return FormatCivilTime(c); }
+std::string AbslUnparseFlag(CivilMonth c) { return FormatCivilTime(c); }
+std::string AbslUnparseFlag(CivilYear c) { return FormatCivilTime(c); }
+
} // namespace time_internal
ABSL_NAMESPACE_END
diff --git a/absl/time/civil_time.h b/absl/time/civil_time.h
index bb460044..5855bc73 100644
--- a/absl/time/civil_time.h
+++ b/absl/time/civil_time.h
@@ -70,8 +70,10 @@
#ifndef ABSL_TIME_CIVIL_TIME_H_
#define ABSL_TIME_CIVIL_TIME_H_
+#include <iosfwd>
#include <string>
+#include "absl/base/config.h"
#include "absl/strings/string_view.h"
#include "absl/time/internal/cctz/include/cctz/civil_time.h"
@@ -530,6 +532,29 @@ std::ostream& operator<<(std::ostream& os, CivilHour h);
std::ostream& operator<<(std::ostream& os, CivilMinute m);
std::ostream& operator<<(std::ostream& os, CivilSecond s);
+// AbslParseFlag()
+//
+// Parses the command-line flag string representation `s` into a civil-time
+// value. Flags must be specified in a format that is valid for
+// `absl::ParseLenientCivilTime()`.
+bool AbslParseFlag(absl::string_view s, CivilSecond* c, std::string* error);
+bool AbslParseFlag(absl::string_view s, CivilMinute* c, std::string* error);
+bool AbslParseFlag(absl::string_view s, CivilHour* c, std::string* error);
+bool AbslParseFlag(absl::string_view s, CivilDay* c, std::string* error);
+bool AbslParseFlag(absl::string_view s, CivilMonth* c, std::string* error);
+bool AbslParseFlag(absl::string_view s, CivilYear* c, std::string* error);
+
+// AbslUnparseFlag()
+//
+// Unparses a civil-time value into a command-line string representation using
+// the format specified by `absl::ParseCivilTime()`.
+std::string AbslUnparseFlag(CivilSecond c);
+std::string AbslUnparseFlag(CivilMinute c);
+std::string AbslUnparseFlag(CivilHour c);
+std::string AbslUnparseFlag(CivilDay c);
+std::string AbslUnparseFlag(CivilMonth c);
+std::string AbslUnparseFlag(CivilYear c);
+
} // namespace time_internal
ABSL_NAMESPACE_END
diff --git a/absl/time/flag_test.cc b/absl/time/flag_test.cc
new file mode 100644
index 00000000..8f8532b7
--- /dev/null
+++ b/absl/time/flag_test.cc
@@ -0,0 +1,147 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/flags/flag.h"
+
+#include <string>
+
+#include "gtest/gtest.h"
+#include "absl/flags/reflection.h"
+#include "absl/time/civil_time.h"
+#include "absl/time/time.h"
+
+ABSL_FLAG(absl::CivilSecond, test_flag_civil_second,
+ absl::CivilSecond(2015, 1, 2, 3, 4, 5), "");
+ABSL_FLAG(absl::CivilMinute, test_flag_civil_minute,
+ absl::CivilMinute(2015, 1, 2, 3, 4), "");
+ABSL_FLAG(absl::CivilHour, test_flag_civil_hour, absl::CivilHour(2015, 1, 2, 3),
+ "");
+ABSL_FLAG(absl::CivilDay, test_flag_civil_day, absl::CivilDay(2015, 1, 2), "");
+ABSL_FLAG(absl::CivilMonth, test_flag_civil_month, absl::CivilMonth(2015, 1),
+ "");
+ABSL_FLAG(absl::CivilYear, test_flag_civil_year, absl::CivilYear(2015), "");
+
+ABSL_FLAG(absl::Duration, test_duration_flag, absl::Seconds(5),
+ "For testing support for Duration flags");
+ABSL_FLAG(absl::Time, test_time_flag, absl::InfinitePast(),
+ "For testing support for Time flags");
+
+namespace {
+
+bool SetFlagValue(absl::string_view flag_name, absl::string_view value) {
+ auto* flag = absl::FindCommandLineFlag(flag_name);
+ if (!flag) return false;
+ std::string err;
+ return flag->ParseFrom(value, &err);
+}
+
+bool GetFlagValue(absl::string_view flag_name, std::string& value) {
+ auto* flag = absl::FindCommandLineFlag(flag_name);
+ if (!flag) return false;
+ value = flag->CurrentValue();
+ return true;
+}
+
+TEST(CivilTime, FlagSupport) {
+ // Tests the default setting of the flags.
+ const absl::CivilSecond kDefaultSec(2015, 1, 2, 3, 4, 5);
+ EXPECT_EQ(absl::CivilSecond(kDefaultSec),
+ absl::GetFlag(FLAGS_test_flag_civil_second));
+ EXPECT_EQ(absl::CivilMinute(kDefaultSec),
+ absl::GetFlag(FLAGS_test_flag_civil_minute));
+ EXPECT_EQ(absl::CivilHour(kDefaultSec),
+ absl::GetFlag(FLAGS_test_flag_civil_hour));
+ EXPECT_EQ(absl::CivilDay(kDefaultSec),
+ absl::GetFlag(FLAGS_test_flag_civil_day));
+ EXPECT_EQ(absl::CivilMonth(kDefaultSec),
+ absl::GetFlag(FLAGS_test_flag_civil_month));
+ EXPECT_EQ(absl::CivilYear(kDefaultSec),
+ absl::GetFlag(FLAGS_test_flag_civil_year));
+
+ // Sets flags to a new value.
+ const absl::CivilSecond kNewSec(2016, 6, 7, 8, 9, 10);
+ absl::SetFlag(&FLAGS_test_flag_civil_second, absl::CivilSecond(kNewSec));
+ absl::SetFlag(&FLAGS_test_flag_civil_minute, absl::CivilMinute(kNewSec));
+ absl::SetFlag(&FLAGS_test_flag_civil_hour, absl::CivilHour(kNewSec));
+ absl::SetFlag(&FLAGS_test_flag_civil_day, absl::CivilDay(kNewSec));
+ absl::SetFlag(&FLAGS_test_flag_civil_month, absl::CivilMonth(kNewSec));
+ absl::SetFlag(&FLAGS_test_flag_civil_year, absl::CivilYear(kNewSec));
+
+ EXPECT_EQ(absl::CivilSecond(kNewSec),
+ absl::GetFlag(FLAGS_test_flag_civil_second));
+ EXPECT_EQ(absl::CivilMinute(kNewSec),
+ absl::GetFlag(FLAGS_test_flag_civil_minute));
+ EXPECT_EQ(absl::CivilHour(kNewSec),
+ absl::GetFlag(FLAGS_test_flag_civil_hour));
+ EXPECT_EQ(absl::CivilDay(kNewSec), absl::GetFlag(FLAGS_test_flag_civil_day));
+ EXPECT_EQ(absl::CivilMonth(kNewSec),
+ absl::GetFlag(FLAGS_test_flag_civil_month));
+ EXPECT_EQ(absl::CivilYear(kNewSec),
+ absl::GetFlag(FLAGS_test_flag_civil_year));
+}
+
+TEST(Duration, FlagSupport) {
+ EXPECT_EQ(absl::Seconds(5), absl::GetFlag(FLAGS_test_duration_flag));
+
+ absl::SetFlag(&FLAGS_test_duration_flag, absl::Seconds(10));
+ EXPECT_EQ(absl::Seconds(10), absl::GetFlag(FLAGS_test_duration_flag));
+
+ EXPECT_TRUE(SetFlagValue("test_duration_flag", "20s"));
+ EXPECT_EQ(absl::Seconds(20), absl::GetFlag(FLAGS_test_duration_flag));
+
+ std::string current_flag_value;
+ EXPECT_TRUE(GetFlagValue("test_duration_flag", current_flag_value));
+ EXPECT_EQ("20s", current_flag_value);
+}
+
+TEST(Time, FlagSupport) {
+ EXPECT_EQ(absl::InfinitePast(), absl::GetFlag(FLAGS_test_time_flag));
+
+ const absl::Time t = absl::FromCivil(absl::CivilSecond(2016, 1, 2, 3, 4, 5),
+ absl::UTCTimeZone());
+ absl::SetFlag(&FLAGS_test_time_flag, t);
+ EXPECT_EQ(t, absl::GetFlag(FLAGS_test_time_flag));
+
+ // Successful parse
+ EXPECT_TRUE(SetFlagValue("test_time_flag", "2016-01-02T03:04:06Z"));
+ EXPECT_EQ(t + absl::Seconds(1), absl::GetFlag(FLAGS_test_time_flag));
+ EXPECT_TRUE(SetFlagValue("test_time_flag", "2016-01-02T03:04:07.0Z"));
+ EXPECT_EQ(t + absl::Seconds(2), absl::GetFlag(FLAGS_test_time_flag));
+ EXPECT_TRUE(SetFlagValue("test_time_flag", "2016-01-02T03:04:08.000Z"));
+ EXPECT_EQ(t + absl::Seconds(3), absl::GetFlag(FLAGS_test_time_flag));
+ EXPECT_TRUE(SetFlagValue("test_time_flag", "2016-01-02T03:04:09+00:00"));
+ EXPECT_EQ(t + absl::Seconds(4), absl::GetFlag(FLAGS_test_time_flag));
+ EXPECT_TRUE(SetFlagValue("test_time_flag", "2016-01-02T03:04:05.123+00:00"));
+ EXPECT_EQ(t + absl::Milliseconds(123), absl::GetFlag(FLAGS_test_time_flag));
+ EXPECT_TRUE(SetFlagValue("test_time_flag", "2016-01-02T03:04:05.123+08:00"));
+ EXPECT_EQ(t + absl::Milliseconds(123) - absl::Hours(8),
+ absl::GetFlag(FLAGS_test_time_flag));
+ EXPECT_TRUE(SetFlagValue("test_time_flag", "infinite-future"));
+ EXPECT_EQ(absl::InfiniteFuture(), absl::GetFlag(FLAGS_test_time_flag));
+ EXPECT_TRUE(SetFlagValue("test_time_flag", "infinite-past"));
+ EXPECT_EQ(absl::InfinitePast(), absl::GetFlag(FLAGS_test_time_flag));
+
+ EXPECT_FALSE(SetFlagValue("test_time_flag", "2016-01-02T03:04:06"));
+ EXPECT_FALSE(SetFlagValue("test_time_flag", "2016-01-02"));
+ EXPECT_FALSE(SetFlagValue("test_time_flag", "2016-01-02Z"));
+ EXPECT_FALSE(SetFlagValue("test_time_flag", "2016-01-02+00:00"));
+ EXPECT_FALSE(SetFlagValue("test_time_flag", "2016-99-99T03:04:06Z"));
+
+ EXPECT_TRUE(SetFlagValue("test_time_flag", "2016-01-02T03:04:05Z"));
+ std::string current_flag_value;
+ EXPECT_TRUE(GetFlagValue("test_time_flag", current_flag_value));
+ EXPECT_EQ("2016-01-02T03:04:05+00:00", current_flag_value);
+}
+
+} // namespace