commit 4cc0851e23123ddf7f312ece3b974f652d01c6b9 from: Omar Polo via: Thomas Adam date: Sun Oct 08 11:13:51 2023 UTC gotwebd: render all the datetimes in a time tag fixes an unused variable that should have been dropped in previous commit too. ok stsp@ commit - 10fa70e266dcd8539efd48ef30628e48068cb5b4 commit + 4cc0851e23123ddf7f312ece3b974f652d01c6b9 blob - 17655d809061e6c714a1d7b150302233dafe4f5e blob + 7cfd0bb7c8b86231b15609ba3157f2edf4ad6c5f --- gotwebd/gotweb.c +++ gotwebd/gotweb.c @@ -1352,68 +1352,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 - 973ce5ba19977634ab3437a3557bdff3a8f03439 blob + f37bcb65b0a961df4a1dda281bf7726a20fe121e --- gotwebd/gotwebd.h +++ gotwebd/gotwebd.h @@ -437,11 +437,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 *, @@ -457,7 +452,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 - 7c8ae023b9acffb8f74ebb838e31638043b1e25b blob + 6d8b3f89b8e9dced575cd6ef3e3d7ab58f60502d --- 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 *, @@ -53,6 +59,30 @@ static inline int rss_tag_item(struct template *, stru 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 }}