Skip to content

Commit

Permalink
Fix weekly stats pings
Browse files Browse the repository at this point in the history
  • Loading branch information
emerick committed Oct 9, 2019
1 parent 3343e43 commit 84fea1c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 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);
}
41 changes: 11 additions & 30 deletions browser/brave_stats_updater_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* 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"
Expand Down Expand Up @@ -44,41 +46,20 @@ std::string GetPlatformIdentifier() {
#endif
}

int GetIsoWeekNumber(base::Time time) {
base::Time::Exploded now_exploded;
time.LocalExplode(&now_exploded);
now_exploded.hour = 0;
now_exploded.minute = 0;
now_exploded.second = 0;
now_exploded.millisecond = 0;
now_exploded.day_of_month =
now_exploded.day_of_month + 3 - ((now_exploded.day_of_week + 6) % 7);

base::Time now_adjusted;
if (!base::Time::FromLocalExploded(now_exploded, &now_adjusted))
return 0;
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);

base::Time::Exploded jan4_exploded = {0};
jan4_exploded.year = now_exploded.year;
jan4_exploded.month = 1;
jan4_exploded.day_of_week = 0;
jan4_exploded.day_of_month = 4;
jan4_exploded.hour = 0;
jan4_exploded.minute = 0;
jan4_exploded.second = 0;
jan4_exploded.millisecond = 0;

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

return 1 + std::round(
((now_adjusted.ToJsTime() - jan4_time.ToJsTime()) / 86400000 -
3 + (jan4_exploded.day_of_month + 6) % 7) /
7);
return week_number;
}

base::Time GetYMDAsDate(base::StringPiece ymd) {
base::Time GetYMDAsDate(const base::StringPiece& ymd) {
const auto pieces = base::SplitStringPiece(ymd, "-", base::TRIM_WHITESPACE,
base::SPLIT_WANT_NONEMPTY);
DCHECK_EQ(pieces.size(), 3ull);
Expand Down
4 changes: 2 additions & 2 deletions browser/brave_stats_updater_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ std::string GetChannelName();

std::string GetPlatformIdentifier();

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

base::Time GetYMDAsDate(base::StringPiece ymd);
base::Time GetYMDAsDate(const base::StringPiece& ymd);

} // namespace brave

Expand Down

0 comments on commit 84fea1c

Please sign in to comment.