Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect time format system-wide #370

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/components/datetime/DateTimeController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,30 @@ char const* DateTime::MonthsString[] = {"--", "JAN", "FEB", "MAR", "APR", "MAY",
char const* DateTime::MonthsStringLow[] = {"--", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

char const* DateTime::MonthsLow[] = {
"--", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
"--", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

void DateTime::GetTimeStr(char* string, bool is24h) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a personal preference but I dislike using a bool as a stand-in for a two state enum. Making the second argument a ClockType instead would be better IMO. This would actually make the calling code simpler too since they all end up doing a comparison like someClockTypeVariable == ClockType::H24 now anyways.

char minutesChar[3];
sprintf(minutesChar, "%02d", static_cast<int>(minute));
char hoursChar[3];
char ampmChar[3] = " ";
if (is24h) {
sprintf(hoursChar, "%02d", hour);
} else {
if (hour == 0) {
hour = 12;
Copy link
Contributor

@jonvmey jonvmey Jul 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bug. This code was moved from elsewhere in the code where hour was a local variable. Here it's a member of DateTime so modifying it here actually changes the time other parts of the code see.

sprintf(ampmChar, "AM");
} else if (hour < 12) {
sprintf(ampmChar, "AM");
} else if (hour >= 12) {
hour = hour - 12;
sprintf(ampmChar, "PM");
}
sprintf(hoursChar, "%02d", hour);
if (hoursChar[0] == '0') {
hoursChar[0] = ' ';
}
}

sprintf(string, "%c%c:%c%c %c%c", hoursChar[0], hoursChar[1], minutesChar[0], minutesChar[1], ampmChar[0], ampmChar[1]);
Comment on lines +123 to +145
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be reduced down to:

if (is24h) {
  sprintf(string, "%02u:%02u   ", hour, minute);
} else {
  char const* const ampmStr = hour < 12 ? "AM" : "PM;
  
  auto tempHour = hour;
  if (tempHour == 0) {
    tempHour = 12;
  } else if (tempHour > 12) {
    tempHour -= 12;
  }

  sprintf(string, "%2u:%02u %s", tempHour, minute, ampmStr);
}

}
2 changes: 2 additions & 0 deletions src/components/datetime/DateTimeController.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ namespace Pinetime {
std::chrono::seconds Uptime() const {
return uptime;
}

void GetTimeStr(char* string, bool is24h = true);

private:
System::SystemTask& systemTask;
Expand Down
4 changes: 3 additions & 1 deletion src/displayapp/screens/Tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ Tile::Tile(uint8_t screenID,

// Time
label_time = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_text_fmt(label_time, "%02i:%02i", dateTimeController.Hours(), dateTimeController.Minutes());
char timeStr[9];
dateTimeController.GetTimeStr(timeStr, settingsController.GetClockType() == Controllers::Settings::ClockType::H24);
lv_label_set_text(label_time, timeStr);
lv_label_set_align(label_time, LV_LABEL_ALIGN_CENTER);
lv_obj_align(label_time, nullptr, LV_ALIGN_IN_TOP_LEFT, 15, 6);

Expand Down
6 changes: 5 additions & 1 deletion src/displayapp/screens/settings/QuickSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ QuickSettings::QuickSettings(Pinetime::Applications::DisplayApp* app,

// Time
label_time = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_text_fmt(label_time, "%02i:%02i", dateTimeController.Hours(), dateTimeController.Minutes());

char timeStr[9];
dateTimeController.GetTimeStr(timeStr, settingsController.GetClockType() == Controllers::Settings::ClockType::H24);

lv_label_set_text(label_time, timeStr);
lv_label_set_align(label_time, LV_LABEL_ALIGN_CENTER);
lv_obj_align(label_time, lv_scr_act(), LV_ALIGN_IN_TOP_LEFT, 15, 4);

Expand Down