"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Omar Polo <op@omarpolo.com>
Subject:
gotwebd: render all datetimes in a time tag
To:
gameoftrees@openbsd.org
Date:
Thu, 05 Oct 2023 11:49:00 +0200

Download raw body.

Thread
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 <op@omarpolo.com>
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;
+!}
+<time datetime="{{ rfc3339 }}">
+  {{ if fmt == TM_DIFF }}
+    {{ render gotweb_render_age(tp, t) }}
+  {{ else }}
+    {{ datebuf }} {{ " UTC" }}
+  {{ end }}
+</time>
+{{ 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 }}
     <div class="index_project_age">
-      {{ render gotweb_render_age(tp, repo_dir->age, TM_DIFF) }}
+      {{ render datetime(tp, repo_dir->age, TM_DIFF) }}
     </div>
   {{ end }}
   <div class="navs_wrapper">
@@ -283,7 +313,7 @@ static inline int rss_author(struct template *, char *
     <div class='brief'>
       <p class='brief_meta'>
         <span class='briefs_age'>
-          {{ render gotweb_render_age(tp, rc->committer_time, TM_DIFF) }}
+          {{ render datetime(tp, rc->committer_time, TM_DIFF) }}
         </span>
         {{" "}}
         <span class="briefs_author">
@@ -414,7 +444,7 @@ static inline int rss_author(struct template *, char *
 	{{ end }}
         <dt>Date:</dt>
         <dd>
-          {{ render gotweb_render_age(tp, rc->committer_time, TM_LONG) }}
+          {{ render datetime(tp, rc->committer_time, TM_LONG) }}
         </dd>
       </dl>
     </div>
@@ -451,7 +481,7 @@ static inline int rss_author(struct template *, char *
     <dl id="blob_header">
       <dt>Date:</dt>
       <dd>
-        {{ render gotweb_render_age(tp, rc->committer_time, TM_LONG) }}
+        {{ render datetime(tp, rc->committer_time, TM_LONG) }}
       </dd>
       <dt>Message:</dt>
       <dd class="commit-msg">{{ rc->commit_msg }}</dd>
@@ -498,7 +528,7 @@ static inline int rss_author(struct template *, char *
       <dd><code class="commit-id">{{ rc->tree_id }}</code></dd>
       <dt>Date:</dt>
       <dd>
-        {{ render gotweb_render_age(tp, rc->committer_time, TM_LONG) }}
+        {{ render datetime(tp, rc->committer_time, TM_LONG) }}
       </dd>
       <dt>Message:</dt>
       <dd class="commit-msg">{{ rc->commit_msg }}</dd>
@@ -646,7 +676,7 @@ static inline int rss_author(struct template *, char *
 	}
 !}
 <div class="tag_age">
-  {{ render gotweb_render_age(tp, rt->tagger_time, TM_DIFF) }}
+  {{ render datetime(tp, rt->tagger_time, TM_DIFF) }}
 </div>
 <div class="tag_name">{{ tag_name }}</div>
 <div class="tag_log">
@@ -697,7 +727,7 @@ static inline int rss_author(struct template *, char *
       <dd>{{ rt->tagger }}</dd>
       <dt>Date:</dt>
       <dd>
-        {{ render gotweb_render_age(tp, rt->tagger_time, TM_LONG)}}
+        {{ render datetime(tp, rt->tagger_time, TM_LONG)}}
       </dd>
       <dt>Message:</dt>
       <dd class="commit-msg">{{ rt->commit_msg }}</dd>
@@ -736,7 +766,7 @@ static inline int rss_author(struct template *, char *
       {{ end }}
       <dt>Date:</dt>
       <dd>
-        {{ render gotweb_render_age(tp, rc->committer_time, TM_LONG) }}
+        {{ render datetime(tp, rc->committer_time, TM_LONG) }}
       </dd>
       <dt>Message:</dt>
       <dd class="commit-msg">{{ rc->commit_msg }}</dd>
@@ -829,7 +859,7 @@ static inline int rss_author(struct template *, char *
 !}
 <section class="branches_wrapper">
   <div class="branches_age">
-    {{ render gotweb_render_age(tp, age, TM_DIFF) }}
+    {{ render datetime(tp, age, TM_DIFF) }}
   </div>
   <div class="branch">
     <a href="{{ render gotweb_render_url(c, &url) }}">{{ refname }}</a>
@@ -868,7 +898,7 @@ static inline int rss_author(struct template *, char *
   {{ if srv->show_repo_age }}
     <dt>Last Change:</dt>
     <dd>
-      {{ render gotweb_render_age(tp, t->repo_dir->age, TM_DIFF) }}
+      {{ render datetime(tp, t->repo_dir->age, TM_DIFF) }}
     </dd>
   {{ end }}
   {{ if srv->show_repo_cloneurl }}
@@ -896,7 +926,7 @@ static inline int rss_author(struct template *, char *
     <dl id="blame_header">
       <dt>Date:</dt>
       <dd>
-        {{ render gotweb_render_age(tp, rc->committer_time, TM_LONG) }}
+        {{ render datetime(tp, rc->committer_time, TM_LONG) }}
       </dd>
       <dt>Message:</dt>
       <dd class="commit-msg">{{ rc->commit_msg }}</dd>