Cookie/History/Hotlist: locale-aware time formatting

(as)ctime are defined as always formatting times using English
day/month names. As these views are for the user's benefit, use
the selected locale to format the information, instead.

Use of (as)ctime on RISC OS with UnixLib is fragile, anyway, as
that implementation always produces a locale-aware serialization
(which is not what the spec defines). This caused assertions to
fire in the previous implementation (which expected ctime to be
locale-unaware). Fixes #2869.
This commit is contained in:
John-Mark Bell 2024-03-05 21:37:45 +00:00
parent b5f4d905f9
commit 167676c335
3 changed files with 39 additions and 40 deletions

View File

@ -232,10 +232,6 @@ cookie_manager_field_builder(enum cookie_manager_field field,
*
* The time should be converted to text in the users locacle
*
* \todo This should probably generate the user text using localtime
* and strftime with the c format specifier. Currently ctime will
* always generate output in the C locale.
*
* \param field Cookie manager treeview field to build
* \param fdata Cookie manager entry field data to set
* \param value Time to show in field
@ -246,22 +242,20 @@ cookie_manager_field_builder_time(enum cookie_manager_field field,
struct treeview_field_data *fdata,
const time_t *value)
{
const char *date;
char *date2;
struct tm ftime;
fdata->field = cm_ctx.fields[field].field;
fdata->value = NULL;
fdata->value_len = 0;
date = ctime(value);
date2 = strdup(date);
if (date2 == NULL) {
fdata->value = NULL;
fdata->value_len = 0;
} else {
assert(date2[24] == '\n');
date2[24] = '\0';
fdata->value = date2;
fdata->value_len = strlen(date2);
if (localtime_r(value, &ftime) != NULL) {
const size_t vsize = 256;
char *value = malloc(vsize);
if (value != NULL) {
fdata->value = value;
fdata->value_len = strftime(value, vsize,
"%a %b %e %H:%M:%S %Y", &ftime);
}
}
return NSERROR_OK;

View File

@ -266,9 +266,9 @@ static nserror global_history_create_treeview_field_data(
const char *title = (data->title != NULL) ?
data->title : messages_get("NoTitle");
char buffer[16];
const char *last_visited;
char *last_visited2;
int len;
struct tm lvtime;
char *last_visited = NULL;
size_t len = 0;
e->data[GH_TITLE].field = gh_ctx.fields[GH_TITLE].field;
e->data[GH_TITLE].value = strdup(title);
@ -279,16 +279,18 @@ static nserror global_history_create_treeview_field_data(
e->data[GH_URL].value = nsurl_access(e->url);
e->data[GH_URL].value_len = nsurl_length(e->url);
last_visited = ctime(&data->last_visit);
last_visited2 = strdup(last_visited);
if (last_visited2 != NULL) {
assert(last_visited2[24] == '\n');
last_visited2[24] = '\0';
if (localtime_r(&data->last_visit, &lvtime) != NULL) {
const size_t lvsize = 256;
last_visited = malloc(lvsize);
if (last_visited != NULL) {
len = strftime(last_visited, lvsize,
"%a %b %e %H:%M:%S %Y", &lvtime);
}
}
e->data[GH_LAST_VISIT].field = gh_ctx.fields[GH_LAST_VISIT].field;
e->data[GH_LAST_VISIT].value = last_visited2;
e->data[GH_LAST_VISIT].value_len = (last_visited2 != NULL) ? 24 : 0;
e->data[GH_LAST_VISIT].value = last_visited;
e->data[GH_LAST_VISIT].value_len = len;
len = snprintf(buffer, 16, "%u", data->visits);
if (len == 16) {

View File

@ -193,29 +193,32 @@ static nserror hotlist_create_treeview_field_visits_data(
struct hotlist_entry *e, const struct url_data *data)
{
char buffer[16];
const char *last_visited;
char *last_visited2;
int len;
char *last_visited = NULL;
size_t len = 0;
/* Last visited */
if (data->visits != 0) {
last_visited = ctime(&data->last_visit);
last_visited2 = strdup(last_visited);
len = 24;
const size_t lvsize = 256;
struct tm lvtime;
if (localtime_r(&data->last_visit, &lvtime) != NULL) {
last_visited = malloc(lvsize);
if (last_visited != NULL) {
len = strftime(last_visited, lvsize,
"%a %b %e %H:%M:%S %Y",
&lvtime);
}
}
} else {
last_visited2 = strdup("-");
last_visited = strdup("-");
len = 1;
}
if (last_visited2 == NULL) {
if (last_visited == NULL) {
return NSERROR_NOMEM;
} else if (len == 24) {
assert(last_visited2[24] == '\n');
last_visited2[24] = '\0';
}
e->data[HL_LAST_VISIT].field = hl_ctx.fields[HL_LAST_VISIT].field;
e->data[HL_LAST_VISIT].value = last_visited2;
e->data[HL_LAST_VISIT].value = last_visited;
e->data[HL_LAST_VISIT].value_len = len;
/* Visits */