Skip to content

Commit 43448e0

Browse files
authoredFeb 24, 2025··
Use the addon date format for news screen (#5312)
Also add a function that converts day/month/year, and not TimeType, to date string
1 parent 83e029c commit 43448e0

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed
 

‎src/states_screens/online/online_screen.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,7 @@ void OnlineScreen::loadList()
195195
// Date format
196196
int yyyy, mm, dd;
197197
sscanf(date.c_str(), "%d-%d-%d", &yyyy, &mm, &dd);
198-
date = StringUtils::toString(yyyy) + "-"
199-
+ StringUtils::toString(mm) + "-"
200-
+ StringUtils::toString(dd);
198+
date = StkTime::toString(yyyy, mm, dd);
201199

202200
std::vector<GUIEngine::ListWidget::ListCell> row;
203201
row.push_back(GUIEngine::ListWidget::ListCell(str.c_str(), icon, 4, false));

‎src/utils/time.cpp

+31-3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,36 @@ std::string StkTime::toString(const TimeType &tt)
7070
{
7171
const struct tm *t = gmtime(&tt);
7272

73+
std::string date_format = getDateFormat();
74+
75+
char s[64];
76+
strftime(s, 64, date_format.c_str(), t);
77+
return s;
78+
} // toString(1)
79+
80+
// ----------------------------------------------------------------------------
81+
82+
/** Converts the date represented by year, month, day
83+
* to a human readable string. */
84+
std::string StkTime::toString(int year, int month, int day)
85+
{
86+
struct tm *t = new tm();
87+
t->tm_year = year - 1900;
88+
t->tm_mon = month - 1;
89+
t->tm_mday = day;
90+
91+
std::string date_format = getDateFormat();
92+
93+
char s[64];
94+
strftime(s, 64, date_format.c_str(), t);
95+
return s;
96+
} // toString(3)
97+
98+
// ----------------------------------------------------------------------------
99+
100+
/** Obtains the translated format of the time string. */
101+
std::string StkTime::getDateFormat()
102+
{
73103
//I18N: Format for dates (%d = day, %m = month, %Y = year). See http://www.cplusplus.com/reference/ctime/strftime/ for more info about date formats.
74104
core::stringw w_date_format = translations->w_gettext(N_("%d/%m/%Y"));
75105
core::stringc c_date_format(w_date_format.c_str());
@@ -82,9 +112,7 @@ std::string StkTime::toString(const TimeType &tt)
82112
date_format = "%d/%m/%Y";
83113
}
84114

85-
char s[64];
86-
strftime(s, 64, date_format.c_str(), t);
87-
return s;
115+
return date_format;
88116
} // toString
89117

90118
// ----------------------------------------------------------------------------

‎src/utils/time.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ class StkTime
6262
/** Converts the time in this object to a human readable string. */
6363
static std::string toString(const TimeType &tt);
6464
// ------------------------------------------------------------------------
65+
/** Converts the date represented by year, month, day
66+
* to a human readable string. */
67+
static std::string toString(int year, int month, int day);
68+
// ------------------------------------------------------------------------
69+
/** Obtains the translated format of the time string. */
70+
static std::string getDateFormat();
71+
// ------------------------------------------------------------------------
6572
/** Returns the number of seconds since 1.1.1970. This function is used
6673
* to compare access times of files, e.g. news, addons data etc.
6774
*/

0 commit comments

Comments
 (0)
Please sign in to comment.