Skip to content

Commit

Permalink
[ads] Add SmartNTT ISO 8601 formatted date and time string condition …
Browse files Browse the repository at this point in the history
…matcher
  • Loading branch information
tmancey committed Oct 29, 2024
1 parent e7d84a5 commit 3b65408
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ base::TimeDelta TimeDeltaSinceEpoch(const int64_t timestamp) {

std::optional<base::TimeDelta> ParseTimeDelta(const std::string_view value) {
double timestamp;
if (!base::StringToDouble(value, &timestamp)) {
return std::nullopt;
if (base::StringToDouble(value, &timestamp)) {
return TimeDeltaSinceEpoch(static_cast<int64_t>(timestamp));
}

base::Time time;
if (base::Time::FromUTCString(value.data(), &time)) {
return TimeDeltaSinceEpoch(time.ToTimeT());
}

return TimeDeltaSinceEpoch(static_cast<int64_t>(timestamp));
return std::nullopt;
}

} // namespace brave_ads
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down

0 comments on commit 3b65408

Please sign in to comment.