Skip to content

Commit

Permalink
Merge pull request #3648 from brave/incorrect-weekly-pings
Browse files Browse the repository at this point in the history
Fix weekly stats pings
  • Loading branch information
emerick committed Oct 10, 2019
1 parent 8724022 commit 4bb2698
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
30 changes: 30 additions & 0 deletions browser/brave_stats_updater_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "base/time/time.h"
#include "brave/browser/brave_stats_updater_params.h"
#include "brave/browser/brave_stats_updater_util.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_referrals/browser/brave_referrals_service.h"
#include "chrome/browser/browser_process.h"
Expand Down Expand Up @@ -285,3 +286,32 @@ TEST_F(BraveStatsUpdaterTest, HasCorrectWeekOfInstallation) {
"2019-03-25");
}
}

TEST_F(BraveStatsUpdaterTest, GetIsoWeekNumber) {
base::Time::Exploded exploded;
exploded.hour = 0;
exploded.minute = 0;
exploded.second = 0;
exploded.millisecond = 0;
exploded.day_of_week = 1;
exploded.day_of_month = 29;
exploded.month = 7;
exploded.year = 2019;

base::Time time;
ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &time));
EXPECT_EQ(brave::GetIsoWeekNumber(time), 31);

exploded.day_of_month = 30;
exploded.month = 9;

ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &time));
EXPECT_EQ(brave::GetIsoWeekNumber(time), 40);

exploded.day_of_month = 1;
exploded.month = 9;
exploded.day_of_week = 0;

ASSERT_TRUE(base::Time::FromLocalExploded(exploded, &time));
EXPECT_EQ(brave::GetIsoWeekNumber(time), 35);
}
19 changes: 18 additions & 1 deletion browser/brave_stats_updater_util.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <ctime>

#include "brave/browser/brave_stats_updater_util.h"

#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"

namespace brave {
Expand All @@ -15,4 +19,17 @@ std::string GetDateAsYMD(const base::Time& time) {
exploded.day_of_month);
}

int GetIsoWeekNumber(const base::Time& time) {
char buffer[24];
time_t rawtime = time.ToTimeT();
struct tm* timeinfo = std::localtime(&rawtime);
strftime(buffer, 24, "%V", timeinfo);

int week_number = 0;
if (!base::StringToInt(buffer, &week_number))
return 0;

return week_number;
}

} // namespace brave
5 changes: 4 additions & 1 deletion browser/brave_stats_updater_util.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

Expand All @@ -13,6 +14,8 @@ namespace brave {

std::string GetDateAsYMD(const base::Time& time);

int GetIsoWeekNumber(const base::Time& time);

} // namespace brave

#endif // BRAVE_BROWSER_BRAVE_STATS_UPDATER_UTIL_H_

0 comments on commit 4bb2698

Please sign in to comment.