diff options
author | John-Mark Bell <jmb@netsurf-browser.org> | 2024-03-05 21:37:45 +0000 |
---|---|---|
committer | John-Mark Bell <jmb@netsurf-browser.org> | 2024-03-05 21:43:48 +0000 |
commit | 167676c335a517333005b59746006b4fccd45af5 (patch) | |
tree | 118debe8b5bc2014363cc8654c091b9015247e8c | |
parent | b5f4d905f958ebfce9be2ab8daa5d42b62f7277d (diff) | |
download | netsurf-167676c335a517333005b59746006b4fccd45af5.tar.gz netsurf-167676c335a517333005b59746006b4fccd45af5.tar.bz2 |
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.
-rw-r--r-- | desktop/cookie_manager.c | 30 | ||||
-rw-r--r-- | desktop/global_history.c | 22 | ||||
-rw-r--r-- | desktop/hotlist.c | 29 |
3 files changed, 40 insertions, 41 deletions
diff --git a/desktop/cookie_manager.c b/desktop/cookie_manager.c index 3e0417f23..7cb93dff7 100644 --- a/desktop/cookie_manager.c +++ b/desktop/cookie_manager.c @@ -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; - - 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); + fdata->value = NULL; + fdata->value_len = 0; + + 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; diff --git a/desktop/global_history.c b/desktop/global_history.c index ad39a3e41..5d13971bb 100644 --- a/desktop/global_history.c +++ b/desktop/global_history.c @@ -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) { diff --git a/desktop/hotlist.c b/desktop/hotlist.c index 2e877b7c6..4f3ca77e1 100644 --- a/desktop/hotlist.c +++ b/desktop/hotlist.c @@ -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 */ |