Commit 38395431 authored by Adolfo Victoria's avatar Adolfo Victoria Committed by Commit Bot

Extend Weekly Time with ToLocalizedString function

Add ToLocalizedString() function to WeeklyTime for later
use in the UI for DeviceAutoUpdateTimeRestrictions.

BUG=chromium:852860
TEST=unit tests

Change-Id: I85db9b19a3f1a1b589c26c064c221fe8e2137123
Reviewed-on: https://chromium-review.googlesource.com/1119392
Commit-Queue: Adolfo Higueros <adokar@google.com>
Reviewed-by: default avatarPavol Marko <pmarko@chromium.org>
Reviewed-by: default avatarSergey Poromov <poromov@chromium.org>
Reviewed-by: default avatarMay Lippert <maybelle@chromium.org>
Cr-Commit-Position: refs/heads/master@{#574207}
parent 4cd4f7d9
......@@ -3,7 +3,11 @@
// found in the LICENSE file.
#include "chrome/browser/chromeos/policy/weekly_time/weekly_time.h"
#include "base/i18n/time_formatting.h"
#include "base/logging.h"
#include "base/strings/string16.h"
#include "base/time/default_clock.h"
#include "base/time/time.h"
namespace em = enterprise_management;
......@@ -17,6 +21,8 @@ constexpr base::TimeDelta kHour = base::TimeDelta::FromHours(1);
constexpr base::TimeDelta kMinute = base::TimeDelta::FromMinutes(1);
constexpr base::TimeDelta kSecond = base::TimeDelta::FromSeconds(1);
const char* kFormatWeekdayHourMinute = "EEEE jj:mm a";
} // namespace
WeeklyTime::WeeklyTime(int day_of_week, int milliseconds)
......@@ -61,18 +67,27 @@ WeeklyTime WeeklyTime::AddMilliseconds(int milliseconds) const {
return WeeklyTime(result_day_of_week, result_milliseconds);
}
base::string16 WeeklyTime::ToLocalizedString() const {
// Clock with the current time.
base::Clock* default_clock = base::DefaultClock::GetInstance();
WeeklyTime now_weekly_time = GetCurrentWeeklyTime(default_clock);
// Offset the current time so that its day of the week and time match
// |day_of_week| and |milliseconds_|.
base::Time offset_time =
default_clock->Now() + now_weekly_time.GetDurationTo(*this);
return base::TimeFormatWithPattern(offset_time, kFormatWeekdayHourMinute);
}
// static
WeeklyTime WeeklyTime::GetCurrentWeeklyTime(base::Clock* clock) {
base::Time::Exploded exploded;
clock->Now().UTCExplode(&exploded);
int day_of_week = exploded.day_of_week;
// Exploded contains 0-based day of week (0 = Sunday, etc.)
if (day_of_week == 0)
day_of_week = 7;
return WeeklyTime(day_of_week,
exploded.hour * kHour.InMilliseconds() +
exploded.minute * kMinute.InMilliseconds() +
exploded.second * kSecond.InMilliseconds());
int day_of_week = exploded.day_of_week == 0 ? 7 : exploded.day_of_week;
int milliseconds = exploded.hour * kHour.InMilliseconds() +
exploded.minute * kMinute.InMilliseconds() +
exploded.second * kSecond.InMilliseconds() +
exploded.millisecond;
return WeeklyTime(day_of_week, milliseconds);
}
// static
......
......@@ -7,6 +7,7 @@
#include <memory>
#include "base/strings/string16.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "base/values.h"
......@@ -16,7 +17,8 @@ namespace policy {
// WeeklyTime class contains day of week and time. Day of week is number from 1
// to 7 (1 = Monday, 2 = Tuesday, etc.) Time is in milliseconds from the
// beginning of the day.
// beginning of the day. WeeklyTime's timezone should be interpreted to be in
// UTC.
class WeeklyTime {
public:
WeeklyTime(int day_of_week, int milliseconds);
......@@ -44,6 +46,15 @@ class WeeklyTime {
// Add milliseconds to WeeklyTime.
WeeklyTime AddMilliseconds(int milliseconds) const;
// Convert WeeklyTime to a string that is in local time and localized to the
// system's language and time display settings. The output is in the format
// "EEEE jj:mm a" E.g. for |day_of_week_| = 4 and |milliseconds_| = 5 hours
// (in ms) then the output should be "Thursday 5:00 AM" in an US locale in UTC
// timezone. Similarly, the output will be "Donnerstag 05:00" in a german
// locale in UTC timezone (there may be slight changes due to different
// standards in different locales).
base::string16 ToLocalizedString() const;
// Return WeeklyTime structure from WeeklyTimeProto. Return nullptr if
// WeeklyTime structure isn't correct.
static std::unique_ptr<WeeklyTime> ExtractFromProto(
......
......@@ -4,13 +4,20 @@
#include "chrome/browser/chromeos/policy/weekly_time/weekly_time.h"
#include <memory>
#include <tuple>
#include <utility>
#include "base/callback_helpers.h"
#include "base/i18n/rtl.h"
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/icu_test_util.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/policy/proto/chrome_device_policy.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/icu/source/i18n/unicode/timezone.h"
namespace em = enterprise_management;
......@@ -238,4 +245,35 @@ INSTANTIATE_TEST_CASE_P(
kThursday,
15 * kMinutesInHour + 24)));
TEST(WeeklyTimeStringTest, ToLocalizedString) {
base::test::ScopedRestoreICUDefaultLocale restore_locale;
// 15:50 UTC, 8:50 PT, 11:50 PT
WeeklyTime test_weekly_time =
WeeklyTime(5, (15 * kMinutesInHour + 50) * kMinute.InMilliseconds());
// Save original timezone
base::ScopedClosureRunner reset_timezone(base::BindOnce(
[](std::unique_ptr<icu::TimeZone> original_timezone) {
icu::TimeZone::adoptDefault(original_timezone.release());
},
base::WrapUnique<icu::TimeZone>(icu::TimeZone::createDefault())));
base::i18n::SetICUDefaultLocale("en_US");
icu::TimeZone::adoptDefault(
icu::TimeZone::createTimeZone("America/Los_Angeles"));
EXPECT_EQ(base::UTF8ToUTF16("Friday 8:50 AM"),
test_weekly_time.ToLocalizedString());
base::i18n::SetICUDefaultLocale("de_DE");
EXPECT_EQ(base::UTF8ToUTF16("Freitag, 08:50"),
test_weekly_time.ToLocalizedString());
base::i18n::SetICUDefaultLocale("en_GB");
icu::TimeZone::adoptDefault(
icu::TimeZone::createTimeZone("America/New_York"));
EXPECT_EQ(base::UTF8ToUTF16("Friday 11:50"),
test_weekly_time.ToLocalizedString());
}
} // namespace policy
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment