Skip to content

Commit

Permalink
Merge pull request #4176 from /issues/7218
Browse files Browse the repository at this point in the history
Fixes next payment date appearing as 4th instead of 5th for a user in Australia on Android
  • Loading branch information
tmancey authored Dec 10, 2019
2 parents b6b1160 + 6a8eb67 commit a04176a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -947,14 +947,14 @@ uint64_t ConfirmationsImpl::GetAdNotificationsReceivedThisMonthForTransactions(

auto now = base::Time::Now();
base::Time::Exploded now_exploded;
now.LocalExplode(&now_exploded);
now.UTCExplode(&now_exploded);

for (const auto& transaction : transactions) {
auto transaction_timestamp =
Time::FromDoubleT(transaction.timestamp_in_seconds);

base::Time::Exploded transaction_timestamp_exploded;
transaction_timestamp.LocalExplode(&transaction_timestamp_exploded);
transaction_timestamp.UTCExplode(&transaction_timestamp_exploded);

if (transaction_timestamp_exploded.year == now_exploded.year &&
transaction_timestamp_exploded.month == now_exploded.month &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ base::Time Payments::CalculateNextPaymentDate(
const base::Time& time,
const uint64_t next_token_redemption_date_in_seconds) const {
base::Time::Exploded now_exploded;
time.LocalExplode(&now_exploded);
time.UTCExplode(&now_exploded);

int month = now_exploded.month;

Expand All @@ -153,7 +153,7 @@ base::Time Payments::CalculateNextPaymentDate(
Time::FromDoubleT(next_token_redemption_date_in_seconds);

base::Time::Exploded next_token_redemption_date_exploded;
next_token_redemption_date.LocalExplode(
next_token_redemption_date.UTCExplode(
&next_token_redemption_date_exploded);

if (next_token_redemption_date_exploded.month == month) {
Expand All @@ -177,10 +177,18 @@ base::Time Payments::CalculateNextPaymentDate(
year++;
}

auto date = base::StringPrintf("%d-%d-%d", year, month, kNextPaymentDay);
base::Time::Exploded next_payment_date_exploded = now_exploded;
next_payment_date_exploded.year = year;
next_payment_date_exploded.month = month;
next_payment_date_exploded.day_of_month = kNextPaymentDay;
next_payment_date_exploded.hour = 23;
next_payment_date_exploded.minute = 59;
next_payment_date_exploded.second = 59;
next_payment_date_exploded.millisecond = 999;

base::Time next_payment_date;
bool success = base::Time::FromString(date.c_str(), &next_payment_date);
bool success = base::Time::FromUTCExploded(next_payment_date_exploded,
&next_payment_date);
DCHECK(success);

return next_payment_date;
Expand Down Expand Up @@ -321,15 +329,15 @@ PaymentInfo Payments::GetPaymentForTransactionMonth(

std::string Payments::GetTransactionMonth(const base::Time& time) const {
base::Time::Exploded time_exploded;
time.LocalExplode(&time_exploded);
time.UTCExplode(&time_exploded);

return GetFormattedTransactionMonth(time_exploded.year, time_exploded.month);
}

std::string Payments::GetPreviousTransactionMonth(
const base::Time& time) const {
base::Time::Exploded time_exploded;
time.LocalExplode(&time_exploded);
time.UTCExplode(&time_exploded);

time_exploded.month--;
if (time_exploded.month < 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,25 @@ class ConfirmationsPaymentsTest : public ::testing::Test {
}

// Objects declared here can be used by all tests in the test case
base::Time GetNextPaymentDate(
base::Time GetNextPaymentUTCDate(
const std::string& date,
const std::string& next_token_redemption_date) {
auto current_date = DateFromString(date);
auto current_date = UTCDateFromString(date);

auto token_redemption_date = DateFromString(next_token_redemption_date);
auto token_redemption_date = UTCDateFromString(next_token_redemption_date);
uint64_t token_redemption_date_in_seconds =
token_redemption_date.ToDoubleT();

return payments_->CalculateNextPaymentDate(current_date,
token_redemption_date_in_seconds);
}

base::Time DateFromString(const std::string& date) {
base::Time UTCDateFromString(
const std::string& date) {
const std::string utc_date = date + " 00:00:00 +00:00";

base::Time time;
if (!base::Time::FromString(date.c_str(), &time)) {
if (!base::Time::FromString(utc_date.c_str(), &time)) {
return base::Time();
}

Expand Down Expand Up @@ -126,7 +129,7 @@ TEST_F(ConfirmationsPaymentsTest, InvalidJson_DefaultTransactionCount) {
std::string json = "[{\"balance\":\"0.5\",\"month\":\"2019-06\",\"transactionCount\":\"INVALID\"}]"; // NOLINT
payments_->SetFromJson(json);

auto date = DateFromString("6 July 2019");
auto date = UTCDateFromString("6 July 2019");

// Act
auto transaction_count = payments_->GetTransactionCountForMonth(date);
Expand All @@ -140,7 +143,7 @@ TEST_F(ConfirmationsPaymentsTest, InvalidJsonWrongType_DefaultTransactionCount)
std::string json = "[{\"balance\":\"0.5\",\"month\":\"2019-06\",\"transactionCount\":5}]"; // NOLINT
payments_->SetFromJson(json);

auto date = DateFromString("6 July 2019");
auto date = UTCDateFromString("6 July 2019");

// Act
auto transaction_count = payments_->GetTransactionCountForMonth(date);
Expand Down Expand Up @@ -190,7 +193,7 @@ TEST_F(ConfirmationsPaymentsTest, TransactionCount_ForThisMonth) {
std::string json = "[{\"balance\":\"0.5\",\"month\":\"2019-06\",\"transactionCount\":\"10\"}]"; // NOLINT
payments_->SetFromJson(json);

auto date = DateFromString("6 June 2019");
auto date = UTCDateFromString("6 June 2019");

// Act
auto transaction_count = payments_->GetTransactionCountForMonth(date);
Expand All @@ -204,7 +207,7 @@ TEST_F(ConfirmationsPaymentsTest, TransactionCount_ForThisMonthWithMultiplePayme
std::string json = "[{\"balance\":\"0.5\",\"month\":\"2019-06\",\"transactionCount\":\"10\"},{\"balance\":\"0.25\",\"month\":\"2019-05\",\"transactionCount\":\"5\"}]"; // NOLINT
payments_->SetFromJson(json);

auto date = DateFromString("6 June 2019");
auto date = UTCDateFromString("6 June 2019");

// Act
auto transaction_count = payments_->GetTransactionCountForMonth(date);
Expand All @@ -218,7 +221,7 @@ TEST_F(ConfirmationsPaymentsTest, TransactionCount_ForThisMonthWithMultiplePayme
std::string json = "[{\"balance\":\"0.25\",\"month\":\"2019-05\",\"transactionCount\":\"5\"},{\"balance\":\"0.5\",\"month\":\"2019-06\",\"transactionCount\":\"10\"}]"; // NOLINT
payments_->SetFromJson(json);

auto date = DateFromString("6 June 2019");
auto date = UTCDateFromString("6 June 2019");

// Act
auto transaction_count = payments_->GetTransactionCountForMonth(date);
Expand All @@ -236,10 +239,11 @@ TEST_F(ConfirmationsPaymentsTest, CalculateNextPaymentDate_BeforeNextPaymentDate
std::string next_token_redemption_date = "21 July 2019";

// Act
auto next_payment_date = GetNextPaymentDate(date, next_token_redemption_date);
auto next_payment_date =
GetNextPaymentUTCDate(date, next_token_redemption_date);

// Assert
auto expected_next_payment_date = DateFromString("5 July 2019");
auto expected_next_payment_date = UTCDateFromString("5 July 2019");
EXPECT_EQ(expected_next_payment_date, next_payment_date);
}

Expand All @@ -252,10 +256,11 @@ TEST_F(ConfirmationsPaymentsTest, CalculateNextPaymentDate_BeforeNextPaymentDate
std::string next_token_redemption_date = "21 July 2019";

// Act
auto next_payment_date = GetNextPaymentDate(date, next_token_redemption_date);
auto next_payment_date =
GetNextPaymentUTCDate(date, next_token_redemption_date);

// Assert
auto expected_next_payment_date = DateFromString("5 July 2019");
auto expected_next_payment_date = UTCDateFromString("5 July 2019");
EXPECT_EQ(expected_next_payment_date, next_payment_date);
}

Expand All @@ -268,10 +273,11 @@ TEST_F(ConfirmationsPaymentsTest, CalculateNextPaymentDate_BeforeNextPaymentDate
std::string next_token_redemption_date = "21 July 2019";

// Act
auto next_payment_date = GetNextPaymentDate(date, next_token_redemption_date);
auto next_payment_date =
GetNextPaymentUTCDate(date, next_token_redemption_date);

// Assert
auto expected_next_payment_date = DateFromString("5 August 2019");
auto expected_next_payment_date = UTCDateFromString("5 August 2019");
EXPECT_EQ(expected_next_payment_date, next_payment_date);
}

Expand All @@ -284,10 +290,11 @@ TEST_F(ConfirmationsPaymentsTest, CalculateNextPaymentDate_BeforeNextPaymentDate
std::string next_token_redemption_date = "21 July 2019";

// Act
auto next_payment_date = GetNextPaymentDate(date, next_token_redemption_date);
auto next_payment_date =
GetNextPaymentUTCDate(date, next_token_redemption_date);

// Assert
auto expected_next_payment_date = DateFromString("5 August 2019");
auto expected_next_payment_date = UTCDateFromString("5 August 2019");
EXPECT_EQ(expected_next_payment_date, next_payment_date);
}

Expand All @@ -300,10 +307,11 @@ TEST_F(ConfirmationsPaymentsTest, CalculateNextPaymentDate_BeforeNextPaymentDate
std::string next_token_redemption_date = "21 July 2019";

// Act
auto next_payment_date = GetNextPaymentDate(date, next_token_redemption_date);
auto next_payment_date =
GetNextPaymentUTCDate(date, next_token_redemption_date);

// Assert
auto expected_next_payment_date = DateFromString("5 August 2019");
auto expected_next_payment_date = UTCDateFromString("5 August 2019");
EXPECT_EQ(expected_next_payment_date, next_payment_date);
}

Expand All @@ -316,10 +324,11 @@ TEST_F(ConfirmationsPaymentsTest, CalculateNextPaymentDate_AfterNextPaymentDate_
std::string next_token_redemption_date = "28 July 2019";

// Act
auto next_payment_date = GetNextPaymentDate(date, next_token_redemption_date);
auto next_payment_date =
GetNextPaymentUTCDate(date, next_token_redemption_date);

// Assert
auto expected_next_payment_date = DateFromString("5 August 2019");
auto expected_next_payment_date = UTCDateFromString("5 August 2019");
EXPECT_EQ(expected_next_payment_date, next_payment_date);
}

Expand All @@ -332,10 +341,11 @@ TEST_F(ConfirmationsPaymentsTest, CalculateNextPaymentDate_AfterNextPaymentDate_
std::string next_token_redemption_date = "28 July 2019";

// Act
auto next_payment_date = GetNextPaymentDate(date, next_token_redemption_date);
auto next_payment_date =
GetNextPaymentUTCDate(date, next_token_redemption_date);

// Assert
auto expected_next_payment_date = DateFromString("5 August 2019");
auto expected_next_payment_date = UTCDateFromString("5 August 2019");
EXPECT_EQ(expected_next_payment_date, next_payment_date);
}

Expand All @@ -348,10 +358,11 @@ TEST_F(ConfirmationsPaymentsTest, CalculateNextPaymentDate_AfterNextPaymentDate_
std::string next_token_redemption_date = "15 July 2019";

// Act
auto next_payment_date = GetNextPaymentDate(date, next_token_redemption_date);
auto next_payment_date =
GetNextPaymentUTCDate(date, next_token_redemption_date);

// Assert
auto expected_next_payment_date = DateFromString("5 August 2019");
auto expected_next_payment_date = UTCDateFromString("5 August 2019");
EXPECT_EQ(expected_next_payment_date, next_payment_date);
}

Expand All @@ -364,10 +375,11 @@ TEST_F(ConfirmationsPaymentsTest, CalculateNextPaymentDate_AfterNextPaymentDate_
std::string next_token_redemption_date = "15 July 2019";

// Act
auto next_payment_date = GetNextPaymentDate(date, next_token_redemption_date);
auto next_payment_date =
GetNextPaymentUTCDate(date, next_token_redemption_date);

// Assert
auto expected_next_payment_date = DateFromString("5 August 2019");
auto expected_next_payment_date = UTCDateFromString("5 August 2019");
EXPECT_EQ(expected_next_payment_date, next_payment_date);
}

Expand All @@ -380,10 +392,11 @@ TEST_F(ConfirmationsPaymentsTest, CalculateNextPaymentDate_AfterNextPaymentDate_
std::string next_token_redemption_date = "15 July 2019";

// Act
auto next_payment_date = GetNextPaymentDate(date, next_token_redemption_date);
auto next_payment_date =
GetNextPaymentUTCDate(date, next_token_redemption_date);

// Assert
auto expected_next_payment_date = DateFromString("5 August 2019");
auto expected_next_payment_date = UTCDateFromString("5 August 2019");
EXPECT_EQ(expected_next_payment_date, next_payment_date);
}

Expand All @@ -396,10 +409,11 @@ TEST_F(ConfirmationsPaymentsTest, CalculateNextPaymentDate_AfterNextPaymentDate_
std::string next_token_redemption_date = "15 August 2019";

// Act
auto next_payment_date = GetNextPaymentDate(date, next_token_redemption_date);
auto next_payment_date =
GetNextPaymentUTCDate(date, next_token_redemption_date);

// Assert
auto expected_next_payment_date = DateFromString("5 September 2019");
auto expected_next_payment_date = UTCDateFromString("5 September 2019");
EXPECT_EQ(expected_next_payment_date, next_payment_date);
}

Expand All @@ -412,10 +426,11 @@ TEST_F(ConfirmationsPaymentsTest, CalculateNextPaymentDate_AfterNextPaymentDate_
std::string next_token_redemption_date = "15 August 2019";

// Act
auto next_payment_date = GetNextPaymentDate(date, next_token_redemption_date);
auto next_payment_date =
GetNextPaymentUTCDate(date, next_token_redemption_date);

// Assert
auto expected_next_payment_date = DateFromString("5 September 2019");
auto expected_next_payment_date = UTCDateFromString("5 September 2019");
EXPECT_EQ(expected_next_payment_date, next_payment_date);
}

Expand Down

0 comments on commit a04176a

Please sign in to comment.