Author: Lars Hjemli <hjemli@gmail.com>
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>
cgit.css | 25 +++++++++++++++++++++++++ cgit.h | 22 +++++++++++++++++++++- ui-commit.c | 4 ++-- ui-shared.c | 47 ++++++++++++++++++++++++++++++++++++++++++++--- ui-summary.c | 4 ++--
diff --git a/cgit.css b/cgit.css index 95c3e40d2cea24ce0a5c41003f16eba566a325d2..327eaba9d3bb3150bf6926373ad3571c1a8844af 100644 --- a/cgit.css +++ b/cgit.css @@ -388,3 +388,28 @@ table.list td.sublevel-repo { padding-left: 1.5em; } + +span.age-mins { + font-weight: bold; + color: #080; +} + +span.age-hours { + color: #080; +} + +span.age-days { + color: #040; +} + +span.age-weeks { + color: #444; +} + +span.age-months { + color: #888; +} + +span.age-years { + color: #bbb; +} diff --git a/cgit.h b/cgit.h index 892723651361b622629aa4a39f42528654882d7f..4da2d3d0d5fd3db235c7064be89e58c48e1ba866 100644 --- a/cgit.h +++ b/cgit.h @@ -29,6 +29,25 @@ #define CMD_VIEW 5 #define CMD_BLOB 6 #define CMD_SNAPSHOT 7 + +/* + * Dateformats used on misc. pages + */ +#define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" +#define FMT_SHORTDATE "%Y-%m-%d" + + +/* + * Limits used for relative dates + */ +#define TM_MIN 60 +#define TM_HOUR (TM_MIN * 60) +#define TM_DAY (TM_HOUR * 24) +#define TM_WEEK (TM_DAY * 7) +#define TM_YEAR (TM_DAY * 365) +#define TM_MONTH (TM_YEAR / 12.0) + + typedef void (*configfn)(const char *name, const char *value); typedef void (*filepair_fn)(struct diff_filepair *pair); typedef void (*linediff_fn)(char *line, int len); @@ -181,7 +200,8 @@ extern char *cgit_pageurl(const char *reponame, const char *pagename, const char *query); extern void cgit_print_error(char *msg); -extern void cgit_print_date(unsigned long secs); +extern void cgit_print_date(time_t secs, char *format); +extern void cgit_print_age(time_t t, time_t max_relative, char *format); extern void cgit_print_docstart(char *title, struct cacheitem *item); extern void cgit_print_docend(); extern void cgit_print_pageheader(char *title, int show_search); diff --git a/ui-commit.c b/ui-commit.c index ff1fad33c9c73f074e93d1984bff354ec1b6381e..59eeb1dfba0400fe6e8c90327bbcdb566fcc3a7d 100644 --- a/ui-commit.c +++ b/ui-commit.c @@ -172,14 +172,14 @@ html_txt(info->author); html(" "); html_txt(info->author_email); html("</td><td class='right'>"); - cgit_print_date(info->author_date); + cgit_print_date(info->author_date, FMT_LONGDATE); html("</td></tr>\n"); html("<tr><th>committer</th><td>"); html_txt(info->committer); html(" "); html_txt(info->committer_email); html("</td><td class='right'>"); - cgit_print_date(info->committer_date); + cgit_print_date(info->committer_date, FMT_LONGDATE); html("</td></tr>\n"); html("<tr><th>tree</th><td colspan='2' class='sha1'><a href='"); query = fmt("h=%s&id=%s", sha1_to_hex(commit->object.sha1), diff --git a/ui-shared.c b/ui-shared.c index c7fbc5e576a9ca6dec19890c5bb29d1229d7e313..acc771b98a2e511a4b269284fd33e58b3ab47f47 100644 --- a/ui-shared.c +++ b/ui-shared.c @@ -88,14 +88,55 @@ 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) diff --git a/ui-summary.c b/ui-summary.c index e7158cc954d9bb9b1f0edef58cca5894586f3051..20394dea73f263c941897fe0ec89f18ec227c4a3 100644 --- a/ui-summary.c +++ b/ui-summary.c @@ -28,7 +28,7 @@ html_link_open(url, NULL, NULL); html_txt(buf); html_link_close(); html("</td><td>"); - cgit_print_date(commit->date); + cgit_print_date(commit->date, FMT_LONGDATE); html("</td><td>"); html_txt(info->author); html("</td><td>"); @@ -108,7 +108,7 @@ html_txt(buf); html_link_close(); html("</td><td>"); if (info->tagger_date > 0) - cgit_print_date(info->tagger_date); + cgit_print_date(info->tagger_date, FMT_LONGDATE); html("</td><td>"); if (info->tagger) html(info->tagger);