From: Omar Polo Subject: gotwebd: render all datetimes in a time tag To: gameoftrees@openbsd.org Date: Thu, 05 Oct 2023 11:49:00 +0200 I'm not aware of any browser that makes content of time more user-friendly using the datetime attribute, but it's nevertheless the right tag to mark up times and I think we should use it. gotweb_render_age() un-learns how to do multiple formats and only renders an age string (i.e. "3 weeks ago"). I suggest to run got diff -w gotweb.c after applying the diff to see that it only removes one case of the switch, leaving the TM_DIFF body as-is. The rest of the diff is a boring s/gotweb_render_age/datetime in pages.tmpl. fwiw I have this applied on my instance. ----------------------------------------------- commit 9699d45c244bb09530d5a121bc00d246496f3894 (main) from: Omar Polo date: Thu Oct 5 09:32:14 2023 UTC gotwebd: render all the datetimes in a time tag diff bf26a633636ba2058b6bb747b0dd4ab17cb185a8 9699d45c244bb09530d5a121bc00d246496f3894 commit - bf26a633636ba2058b6bb747b0dd4ab17cb185a8 commit + 9699d45c244bb09530d5a121bc00d246496f3894 blob - 31ec1b7671fbb5d5b67b5d5ef6f6ac7742ca1782 blob + 5d1283460327c8a36e76c5f8ed29b5fd573a5ce8 --- gotwebd/gotweb.c +++ gotwebd/gotweb.c @@ -1353,68 +1353,48 @@ done: } int -gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm) +gotweb_render_age(struct template *tp, time_t committer_time) { struct request *c = tp->tp_arg; - struct tm tm; long long diff_time; const char *years = "years ago", *months = "months ago"; const char *weeks = "weeks ago", *days = "days ago"; const char *hours = "hours ago", *minutes = "minutes ago"; const char *seconds = "seconds ago", *now = "right now"; - char *s; - char datebuf[64]; - size_t r; - switch (ref_tm) { - case TM_DIFF: - diff_time = time(NULL) - committer_time; - if (diff_time > 60 * 60 * 24 * 365 * 2) { - if (tp_writef(c->tp, "%lld %s", - (diff_time / 60 / 60 / 24 / 365), years) == -1) - return -1; - } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) { - if (tp_writef(c->tp, "%lld %s", - (diff_time / 60 / 60 / 24 / (365 / 12)), - months) == -1) - return -1; - } else if (diff_time > 60 * 60 * 24 * 7 * 2) { - if (tp_writef(c->tp, "%lld %s", - (diff_time / 60 / 60 / 24 / 7), weeks) == -1) - return -1; - } else if (diff_time > 60 * 60 * 24 * 2) { - if (tp_writef(c->tp, "%lld %s", - (diff_time / 60 / 60 / 24), days) == -1) - return -1; - } else if (diff_time > 60 * 60 * 2) { - if (tp_writef(c->tp, "%lld %s", - (diff_time / 60 / 60), hours) == -1) - return -1; - } else if (diff_time > 60 * 2) { - if (tp_writef(c->tp, "%lld %s", (diff_time / 60), - minutes) == -1) - return -1; - } else if (diff_time > 2) { - if (tp_writef(c->tp, "%lld %s", diff_time, - seconds) == -1) - return -1; - } else { - if (tp_writes(tp, now) == -1) - return -1; - } - break; - case TM_LONG: - if (gmtime_r(&committer_time, &tm) == NULL) + diff_time = time(NULL) - committer_time; + if (diff_time > 60 * 60 * 24 * 365 * 2) { + if (tp_writef(c->tp, "%lld %s", + (diff_time / 60 / 60 / 24 / 365), years) == -1) return -1; - - s = asctime_r(&tm, datebuf); - if (s == NULL) + } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) { + if (tp_writef(c->tp, "%lld %s", + (diff_time / 60 / 60 / 24 / (365 / 12)), + months) == -1) return -1; - - if (tp_writes(tp, datebuf) == -1 || - tp_writes(tp, " UTC") == -1) + } else if (diff_time > 60 * 60 * 24 * 7 * 2) { + if (tp_writef(c->tp, "%lld %s", + (diff_time / 60 / 60 / 24 / 7), weeks) == -1) return -1; - break; + } else if (diff_time > 60 * 60 * 24 * 2) { + if (tp_writef(c->tp, "%lld %s", + (diff_time / 60 / 60 / 24), days) == -1) + return -1; + } else if (diff_time > 60 * 60 * 2) { + if (tp_writef(c->tp, "%lld %s", + (diff_time / 60 / 60), hours) == -1) + return -1; + } else if (diff_time > 60 * 2) { + if (tp_writef(c->tp, "%lld %s", (diff_time / 60), + minutes) == -1) + return -1; + } else if (diff_time > 2) { + if (tp_writef(c->tp, "%lld %s", diff_time, + seconds) == -1) + return -1; + } else { + if (tp_writes(tp, now) == -1) + return -1; } return 0; } blob - 908f76ea062a793a125040df6011a8852db3b00a blob + 1ea7f7b6e6083db17472436ab39cbdb63fb3dc1f --- gotwebd/gotwebd.h +++ gotwebd/gotwebd.h @@ -435,11 +435,6 @@ enum query_actions { ACTIONS__MAX, }; -enum gotweb_ref_tm { - TM_DIFF, - TM_LONG, -}; - extern struct gotwebd *gotwebd_env; typedef int (*got_render_blame_line_cb)(struct template *, const char *, @@ -455,7 +450,7 @@ int sockets_privinit(struct gotwebd *, struct socket * /* gotweb.c */ void gotweb_get_navs(struct request *, struct gotweb_url *, int *, struct gotweb_url *, int *); -int gotweb_render_age(struct template *, time_t, int); +int gotweb_render_age(struct template *, time_t); const struct got_error *gotweb_init_transport(struct transport **); const char *gotweb_action_name(int); int gotweb_render_url(struct request *, struct gotweb_url *); blob - 675bb083cd6fc3a41a5fcd288e910040a637d289 blob + a51f74abb8dfc67479ed3558a8c7efd83400e068 --- gotwebd/pages.tmpl +++ gotwebd/pages.tmpl @@ -39,6 +39,12 @@ #include "gotwebd.h" #include "tmpl.h" +enum gotweb_ref_tm { + TM_DIFF, + TM_LONG, +}; + +static int datetime(struct template *, time_t, int); static int gotweb_render_blob_line(struct template *, const char *, size_t); static int gotweb_render_tree_item(struct template *, struct got_tree_entry *); static int blame_line(struct template *, const char *, struct blame_line *, @@ -54,6 +60,30 @@ static inline int rss_author(struct template *, char * !} +{{ define datetime(struct template *tp, time_t t, int fmt) }} +{! + struct tm tm; + char rfc3339[64]; + char datebuf[64]; + + if (gmtime_r(&t, &tm) == NULL) + return -1; + + if (strftime(rfc3339, sizeof(rfc3339), "%FT%TZ", &tm) == 0) + return -1; + + if (fmt != TM_DIFF && asctime_r(&tm, datebuf) == NULL) + return -1; +!} + +{{ end }} + {{ define gotweb_render_page(struct template *tp, int (*body)(struct template *)) }} {! @@ -216,7 +246,7 @@ static inline int rss_author(struct template *, char * {{ end }} {{ if srv->show_repo_age }}
- {{ render gotweb_render_age(tp, repo_dir->age, TM_DIFF) }} + {{ render datetime(tp, repo_dir->age, TM_DIFF) }}
{{ end }}