Skip to content

Commit

Permalink
Use the time format preference(AM/PM or 24H) in Settings to show even…
Browse files Browse the repository at this point in the history
…ts time

- Add twenty_four_hour_clock_formatter to format the time in 24H.
- Display the time of events in event_list in the same form as specified
  in Settings.

Bug: 1308279
Change-Id: I6c52691ca5856d17a826bcfe2a546a8ccf1182f6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3558322
Reviewed-by: Jiaming Cheng <jiamingc@chromium.org>
Reviewed-by: Alex Newcomer <newcomer@chromium.org>
Commit-Queue: Sylvie Liu <sylvieliu@chromium.org>
Cr-Commit-Position: refs/heads/main@{#988079}
  • Loading branch information
Sylvie Liu authored and Chromium LUCI CQ committed Apr 1, 2022
1 parent 556ac25 commit ed7509f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 8 deletions.
30 changes: 22 additions & 8 deletions ash/system/time/calendar_event_list_item_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

#include "ash/system/time/calendar_event_list_item_view.h"

#include <string>

#include "ash/public/cpp/ash_typography.h"
#include "ash/public/cpp/system_tray_client.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/model/clock_model.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/time/calendar_metrics.h"
#include "ash/system/time/calendar_utils.h"
Expand Down Expand Up @@ -107,10 +110,18 @@ CalendarEventListItemView::CalendarEventListItemView(
event_url_(event.html_link()) {
SetLayoutManager(std::make_unique<views::FillLayout>());

auto start_time = event.start_time().date_time();
auto end_time = event.end_time().date_time();
auto start_time_string = calendar_utils::GetTwelveHourClockTime(start_time);
auto end_time_string = calendar_utils::GetTwelveHourClockTime(end_time);
const base::Time start_time = event.start_time().date_time();
const base::Time end_time = event.end_time().date_time();
bool use_12_hour_clock =
Shell::Get()->system_tray_model()->clock()->hour_clock_type() ==
base::k12HourClock;
std::u16string start_time_string =
use_12_hour_clock
? calendar_utils::GetTwelveHourClockTime(start_time)
: calendar_utils::GetTwentyFourHourClockTime(start_time);
std::u16string end_time_string =
use_12_hour_clock ? calendar_utils::GetTwelveHourClockTime(end_time)
: calendar_utils::GetTwentyFourHourClockTime(end_time);
GetViewAccessibility().OverrideRole(ax::mojom::Role::kButton);
SetAccessibleName(l10n_util::GetStringFUTF16(
IDS_ASH_CALENDAR_EVENT_ENTRY_ACCESSIBLE_DESCRIPTION, start_time_string,
Expand All @@ -125,13 +136,16 @@ CalendarEventListItemView::CalendarEventListItemView(
summary_->SetBorder(views::CreateEmptyBorder(
gfx::Insets::TLBR(0, kEntryHorizontalPadding, 0, 0)));

if (start_time_string.substr(start_time_string.size() - 2) ==
end_time_string.substr(end_time_string.size() - 2)) {
// When the start time and end time are in the same meridiem, remove the
// first one and the space before it, which are the last 3 characters.
// When using AM/PM time format and the start time and end time are in the
// same meridiem, remove the first one and the space before it, which are the
// last 3 characters.
if (use_12_hour_clock &&
start_time_string.substr(start_time_string.size() - 2) ==
end_time_string.substr(end_time_string.size() - 2)) {
start_time_string =
start_time_string.substr(0, start_time_string.size() - 3);
}

auto time_range = start_time_string + u" - " + end_time_string;
time_range_->SetText(time_range);
SetUpLabel(time_range_);
Expand Down
36 changes: 36 additions & 0 deletions ash/system/time/calendar_event_list_view_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/model/clock_model.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/time/calendar_event_list_item_view.h"
#include "ash/system/time/calendar_unittest_utils.h"
Expand Down Expand Up @@ -38,6 +39,10 @@ std::unique_ptr<google_apis::calendar::EventList> CreateMockEventList() {
"id_4", "summary_4", "21 Nov 2021 8:30 GMT", "21 Nov 2021 9:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_5", "summary_5", "21 Nov 2021 10:30 GMT", "21 Nov 2021 11:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_6", "summary_6", "22 Nov 2021 20:30 GMT", "22 Nov 2021 21:30 GMT"));
event_list->InjectItemForTesting(calendar_test_utils::CreateEvent(
"id_7", "summary_7", "22 Nov 2021 23:30 GMT", "23 Nov 2021 0:30 GMT"));

return event_list;
}
Expand Down Expand Up @@ -89,6 +94,13 @@ class CalendarViewEventListViewTest : public AshTestBase {
->summary_);
}

views::Label* GetTimeRange(int child_index) {
return static_cast<views::Label*>(
static_cast<CalendarEventListItemView*>(
content_view()->children()[child_index])
->time_range_);
}

std::u16string GetEmptyLabel() {
return static_cast<views::LabelButton*>(
content_view()->children()[0]->children()[0])
Expand Down Expand Up @@ -178,4 +190,28 @@ TEST_F(CalendarViewEventListViewTest, LaunchItem) {
"Ash.Calendar.UserJourneyTime.EventLaunched", 1);
}

TEST_F(CalendarViewEventListViewTest, CheckTimeFormat) {
ash::system::TimezoneSettings::GetInstance()->SetTimezoneFromID(u"GMT");

base::Time date;
ASSERT_TRUE(base::Time::FromString("22 Nov 2021 10:00 GMT", &date));

// Set the time in AM/PM format.
Shell::Get()->system_tray_model()->SetUse24HourClock(false);

CreateEventListView(date);

EXPECT_EQ(u"8:30 - 9:30 PM", GetTimeRange(0)->GetText());
EXPECT_EQ(u"11:30 PM - 12:30 AM", GetTimeRange(1)->GetText());

// Set the time in 24 hour format.
Shell::Get()->system_tray_model()->SetUse24HourClock(true);

// Regenerate the event list to refresh events time range.
CreateEventListView(date);

EXPECT_EQ(u"20:30 - 21:30", GetTimeRange(0)->GetText());
EXPECT_EQ(u"23:30 - 00:30", GetTimeRange(1)->GetText());
}

} // namespace ash
5 changes: 5 additions & 0 deletions ash/system/time/calendar_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ std::u16string GetTwelveHourClockTime(const base::Time date) {
DateFormatter::GetInstance()->twelve_hour_clock_formatter(), date);
}

std::u16string GetTwentyFourHourClockTime(const base::Time date) {
return calendar_utils::FormatDate(
DateFormatter::GetInstance()->twenty_four_hour_clock_formatter(), date);
}

std::u16string GetTimeZone(const base::Time date) {
return calendar_utils::FormatDate(
DateFormatter::GetInstance()->time_zone_formatter(), date);
Expand Down
4 changes: 4 additions & 0 deletions ash/system/time/calendar_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ ASH_EXPORT std::u16string GetMonthNameAndDayOfMonth(const base::Time date);
// (e.g. 2:31 AM)
ASH_EXPORT std::u16string GetTwelveHourClockTime(const base::Time date);

// Gets the `date`'s hour in twenty four hour clock format.
// (e.g. 22:31)
ASH_EXPORT std::u16string GetTwentyFourHourClockTime(const base::Time date);

// Gets the `date`'s time zone.
// (e.g. Greenwich Mean Time)
ASH_EXPORT std::u16string GetTimeZone(const base::Time date);
Expand Down
2 changes: 2 additions & 0 deletions ash/system/time/date_formatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void DateFormatter::ResetFormatters() {
month_name_year_formatter_ = CreateSimpleDateFormatter("MMMM yyyy");
time_zone_formatter_ = CreateSimpleDateFormatter("zzzz");
twelve_hour_clock_formatter_ = CreateSimpleDateFormatter("h:mm a");
twenty_four_hour_clock_formatter_ = CreateSimpleDateFormatter("HH:mm");
year_formatter_ = CreateSimpleDateFormatter("YYYY");
}

Expand All @@ -63,6 +64,7 @@ DateFormatter::DateFormatter()
month_name_year_formatter_(CreateSimpleDateFormatter("MMMM yyyy")),
time_zone_formatter_(CreateSimpleDateFormatter("zzzz")),
twelve_hour_clock_formatter_(CreateSimpleDateFormatter("h:mm a")),
twenty_four_hour_clock_formatter_(CreateSimpleDateFormatter("HH:mm")),
year_formatter_(CreateSimpleDateFormatter("YYYY")) {
time_zone_settings_observer_.Observe(system::TimezoneSettings::GetInstance());
}
Expand Down
7 changes: 7 additions & 0 deletions ash/system/time/date_formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class DateFormatter : public system::TimezoneSettings::Observer {
return twelve_hour_clock_formatter_;
}

icu::SimpleDateFormat& twenty_four_hour_clock_formatter() {
return twenty_four_hour_clock_formatter_;
}

icu::SimpleDateFormat& year_formatter() { return year_formatter_; }

private:
Expand Down Expand Up @@ -94,6 +98,9 @@ class DateFormatter : public system::TimezoneSettings::Observer {
// Formatter for 12 hour clock hours and minutes.
icu::SimpleDateFormat twelve_hour_clock_formatter_;

// Formatter for 24 hour clock hours and minutes.
icu::SimpleDateFormat twenty_four_hour_clock_formatter_;

// Formatter for getting the year.
icu::SimpleDateFormat year_formatter_;

Expand Down

0 comments on commit ed7509f

Please sign in to comment.