From 3b65408cab42a357e592a30f4a33965f3812a622 Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Tue, 29 Oct 2024 08:15:26 -0500 Subject: [PATCH] [ads] Add SmartNTT ISO 8601 formatted date and time string condition matcher --- ...perator_condition_matcher_util_internal.cc | 11 ++++-- ...operator_condition_matcher_util_internal.h | 3 +- ...ondition_matcher_util_internal_unittest.cc | 35 +++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/components/brave_ads/core/internal/serving/targeting/condition_matcher/matchers/internal/epoch_operator_condition_matcher_util_internal.cc b/components/brave_ads/core/internal/serving/targeting/condition_matcher/matchers/internal/epoch_operator_condition_matcher_util_internal.cc index 70f4be86754f..65f4f978029b 100644 --- a/components/brave_ads/core/internal/serving/targeting/condition_matcher/matchers/internal/epoch_operator_condition_matcher_util_internal.cc +++ b/components/brave_ads/core/internal/serving/targeting/condition_matcher/matchers/internal/epoch_operator_condition_matcher_util_internal.cc @@ -74,11 +74,16 @@ base::TimeDelta TimeDeltaSinceEpoch(const int64_t timestamp) { std::optional ParseTimeDelta(const std::string_view value) { double timestamp; - if (!base::StringToDouble(value, ×tamp)) { - return std::nullopt; + if (base::StringToDouble(value, ×tamp)) { + return TimeDeltaSinceEpoch(static_cast(timestamp)); + } + + base::Time time; + if (base::Time::FromUTCString(value.data(), &time)) { + return TimeDeltaSinceEpoch(time.ToTimeT()); } - return TimeDeltaSinceEpoch(static_cast(timestamp)); + return std::nullopt; } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/serving/targeting/condition_matcher/matchers/internal/epoch_operator_condition_matcher_util_internal.h b/components/brave_ads/core/internal/serving/targeting/condition_matcher/matchers/internal/epoch_operator_condition_matcher_util_internal.h index ef2591251872..8128d00c2f44 100644 --- a/components/brave_ads/core/internal/serving/targeting/condition_matcher/matchers/internal/epoch_operator_condition_matcher_util_internal.h +++ b/components/brave_ads/core/internal/serving/targeting/condition_matcher/matchers/internal/epoch_operator_condition_matcher_util_internal.h @@ -27,7 +27,8 @@ bool IsUnixEpochTimestamp(int64_t timestamp); // Converts a Windows timestamp to a Unix timestamp. int64_t WindowsToUnixEpoch(int64_t timestamp); -// Returns the time delta since a Unix or Windows timestamp. +// Returns the time delta since a Unix or Windows timestamp or an ISO 8601 +// formatted date and time. base::TimeDelta TimeDeltaSinceEpoch(int64_t timestamp); // Parses a time delta from a string. diff --git a/components/brave_ads/core/internal/serving/targeting/condition_matcher/matchers/internal/epoch_operator_condition_matcher_util_internal_unittest.cc b/components/brave_ads/core/internal/serving/targeting/condition_matcher/matchers/internal/epoch_operator_condition_matcher_util_internal_unittest.cc index b0512d95f3fa..b2bf6d04b2cc 100644 --- a/components/brave_ads/core/internal/serving/targeting/condition_matcher/matchers/internal/epoch_operator_condition_matcher_util_internal_unittest.cc +++ b/components/brave_ads/core/internal/serving/targeting/condition_matcher/matchers/internal/epoch_operator_condition_matcher_util_internal_unittest.cc @@ -127,6 +127,41 @@ TEST_F(BraveAdsOperatorConditionMatcherUtilInternalTest, ParseTimeDelta("1727740800" /*1st October 2024 00:00:00 UTC*/)); } +TEST_F(BraveAdsOperatorConditionMatcherUtilInternalTest, + ParseTimeStringTimeDelta) { + // Arrange + AdvanceClockTo(test::TimeFromUTCString("3 October 2024")); + + // Act & Assert + EXPECT_EQ( + base::Days(2), + ParseTimeDelta("2024-10-01T00:00:00Z" /*1st October 2024 00:00:00 UTC*/)); + EXPECT_EQ( + base::Days(2), + ParseTimeDelta( + "2024-10-01T00:00:00.000000Z" /*1st October 2024 00:00:00 UTC*/)); + EXPECT_EQ( + base::Days(2), + ParseTimeDelta("2024-10-01T00:00:00" /*1st October 2024 00:00:00 UTC*/)); + EXPECT_EQ( + base::Days(2), + ParseTimeDelta( + "2024-10-01T00:00:00.000000" /*1st October 2024 00:00:00 UTC*/)); + EXPECT_EQ(base::Days(2), + ParseTimeDelta( + "2024-09-30T19:00:00-05:00" /*1st October 2024 00:00:00 UTC*/)); + EXPECT_EQ( + base::Days(2), + ParseTimeDelta("1 Oct 2024 00:00:00" /*1st October 2024 00:00:00 UTC*/)); + EXPECT_EQ( + base::Days(2), + ParseTimeDelta( + "1 Oct 2024 00:00:00.000000" /*1st October 2024 00:00:00 UTC*/)); + EXPECT_EQ(base::Days(2), + ParseTimeDelta( + "30 Sept 2024 19:00:00 EST" /*1st October 2024 00:00:00 UTC*/)); +} + TEST_F(BraveAdsOperatorConditionMatcherUtilInternalTest, DoNotParseTimeDelta) { // Act & Assert EXPECT_FALSE(ParseTimeDelta("broken time"));