diff --git a/browser/brave_stats_updater_unittest.cc b/browser/brave_stats_updater_unittest.cc index ff1ea9aa55b..8de85e43862 100644 --- a/browser/brave_stats_updater_unittest.cc +++ b/browser/brave_stats_updater_unittest.cc @@ -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" @@ -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); +} diff --git a/browser/brave_stats_updater_util.cc b/browser/brave_stats_updater_util.cc index e1031db37c0..584a5edca41 100644 --- a/browser/brave_stats_updater_util.cc +++ b/browser/brave_stats_updater_util.cc @@ -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 + #include "brave/browser/brave_stats_updater_util.h" #include "base/strings/string_number_conversions.h" @@ -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); diff --git a/browser/brave_stats_updater_util.h b/browser/brave_stats_updater_util.h index 93e5b320ed1..2a66ee68f32 100644 --- a/browser/brave_stats_updater_util.h +++ b/browser/brave_stats_updater_util.h @@ -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