-rw-r--r-- | ui-shared.c | 47 |
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 @@ -59,74 +59,115 @@ char *cgit_repourl(const char *reponame) char *cgit_pageurl(const char *reponame, const char *pagename, const char *query) { if (cgit_virtual_root) { if (query) return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame, pagename, query); else return fmt("%s/%s/%s/", cgit_virtual_root, reponame, pagename); } else { if (query) return fmt("?r=%s&p=%s&%s", reponame, pagename, query); else return fmt("?r=%s&p=%s", reponame, pagename); } } char *cgit_currurl() { if (!cgit_virtual_root) return cgit_script_name; else if (cgit_query_page) return fmt("%s/%s/%s/", cgit_virtual_root, cgit_query_repo, cgit_query_page); else if (cgit_query_repo) return fmt("%s/%s/", cgit_virtual_root, cgit_query_repo); else return fmt("%s/", cgit_virtual_root); } -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"); htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); htmlf("Expires: %s\n", http_date(item->st.st_mtime + ttl_seconds(item->ttl))); html("\n"); html(cgit_doctype); html("<html>\n"); html("<head>\n"); html("<title>"); html_txt(title); html("</title>\n"); htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version); html("<link rel='stylesheet' type='text/css' href='"); html_attr(cgit_css); html("'/>\n"); html("</head>\n"); html("<body>\n"); } void cgit_print_docend() { html("</td></tr></table>"); html("</body>\n</html>\n"); } void cgit_print_pageheader(char *title, int show_search) { html("<table id='layout'>"); html("<tr><td id='header'>"); html(cgit_root_title); |