summary refs log tree commit diff
path: root/ui-shared.c
diff options
context:
space:
mode:
authorLars Hjemli <hjemli@gmail.com>2007-05-22 23:08:46 +0200
committerLars Hjemli <hjemli@gmail.com>2007-05-22 23:12:41 +0200
commit5db39170b6c979655a0238dcd627e206febed88b (patch)
tree2c79691bde31f9db2861dc76010691e9dbdde1cb /ui-shared.c
parent3b86b44fc761cfa8b97c44bbbdd63c9fbf1127ed (diff)
Add cgit_print_age() function
This function can be used to print relative dates, just as in gitweb. Next
step will be to actually use the new function.

Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (limited to 'ui-shared.c')
-rw-r--r--ui-shared.c47
1 files changed, 44 insertions, 3 deletions
diff --git a/ui-shared.c b/ui-shared.c
index c7fbc5e..acc771b 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -88,16 +88,57 @@ char *cgit_currurl()
 }
 
 
-void cgit_print_date(unsigned long secs)
+void cgit_print_date(time_t secs, char *format)
 {
-	char buf[32];
+	char buf[64];
 	struct tm *time;
 
 	time = gmtime(&secs);
-	strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time);
+	strftime(buf, sizeof(buf)-1, format, time);
 	html_txt(buf);
 }
 
+void cgit_print_age(time_t t, time_t max_relative, char *format)
+{
+	time_t now, secs;
+
+	time(&now);
+	secs = now - t;
+
+	if (secs > max_relative && max_relative >= 0) {
+		cgit_print_date(t, format);
+		return;
+	}
+
+	if (secs < TM_HOUR * 2) {
+		htmlf("<span class='age-mins'>%.0f min.</span>",
+		      secs * 1.0 / TM_MIN);
+		return;
+	}
+	if (secs < TM_DAY * 2) {
+		htmlf("<span class='age-hours'>%.0f hours</span>",
+		      secs * 1.0 / TM_HOUR);
+		return;
+	}
+	if (secs < TM_WEEK * 2) {
+		htmlf("<span class='age-days'>%.0f days</span>",
+		      secs * 1.0 / TM_DAY);
+		return;
+	}
+	if (secs < TM_MONTH * 2) {
+		htmlf("<span class='age-weeks'>%.0f weeks</span>",
+		      secs * 1.0 / TM_WEEK);
+		return;
+	}
+	if (secs < TM_YEAR * 2) {
+		htmlf("<span class='age-months'>%.0f months</span>",
+		      secs * 1.0 / TM_MONTH);
+		return;
+	}
+	htmlf("<span class='age-years'>%.0f years</span>",
+	      secs * 1.0 / TM_YEAR);
+}
+
 void cgit_print_docstart(char *title, struct cacheitem *item)
 {
 	html("Content-Type: text/html; charset=utf-8\n");