Skip to content

Commit

Permalink
Use the addon date format for news screen (#5312)
Browse files Browse the repository at this point in the history
Also add a function that converts day/month/year, and not TimeType, to date string
  • Loading branch information
kimden authored Feb 24, 2025
1 parent 83e029c commit 43448e0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
4 changes: 1 addition & 3 deletions src/states_screens/online/online_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ void OnlineScreen::loadList()
// Date format
int yyyy, mm, dd;
sscanf(date.c_str(), "%d-%d-%d", &yyyy, &mm, &dd);
date = StringUtils::toString(yyyy) + "-"
+ StringUtils::toString(mm) + "-"
+ StringUtils::toString(dd);
date = StkTime::toString(yyyy, mm, dd);

std::vector<GUIEngine::ListWidget::ListCell> row;
row.push_back(GUIEngine::ListWidget::ListCell(str.c_str(), icon, 4, false));
Expand Down
34 changes: 31 additions & 3 deletions src/utils/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,36 @@ std::string StkTime::toString(const TimeType &tt)
{
const struct tm *t = gmtime(&tt);

std::string date_format = getDateFormat();

char s[64];
strftime(s, 64, date_format.c_str(), t);
return s;
} // toString(1)

// ----------------------------------------------------------------------------

/** Converts the date represented by year, month, day
* to a human readable string. */
std::string StkTime::toString(int year, int month, int day)
{
struct tm *t = new tm();
t->tm_year = year - 1900;
t->tm_mon = month - 1;
t->tm_mday = day;

std::string date_format = getDateFormat();

char s[64];
strftime(s, 64, date_format.c_str(), t);
return s;
} // toString(3)

// ----------------------------------------------------------------------------

/** Obtains the translated format of the time string. */
std::string StkTime::getDateFormat()
{
//I18N: Format for dates (%d = day, %m = month, %Y = year). See http://www.cplusplus.com/reference/ctime/strftime/ for more info about date formats.
core::stringw w_date_format = translations->w_gettext(N_("%d/%m/%Y"));
core::stringc c_date_format(w_date_format.c_str());
Expand All @@ -82,9 +112,7 @@ std::string StkTime::toString(const TimeType &tt)
date_format = "%d/%m/%Y";
}

char s[64];
strftime(s, 64, date_format.c_str(), t);
return s;
return date_format;
} // toString

// ----------------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions src/utils/time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ class StkTime
/** Converts the time in this object to a human readable string. */
static std::string toString(const TimeType &tt);
// ------------------------------------------------------------------------
/** Converts the date represented by year, month, day
* to a human readable string. */
static std::string toString(int year, int month, int day);
// ------------------------------------------------------------------------
/** Obtains the translated format of the time string. */
static std::string getDateFormat();
// ------------------------------------------------------------------------
/** Returns the number of seconds since 1.1.1970. This function is used
* to compare access times of files, e.g. news, addons data etc.
*/
Expand Down

0 comments on commit 43448e0

Please sign in to comment.