From 5db39170b6c979655a0238dcd627e206febed88b Mon Sep 17 00:00:00 2001 From: Lars Hjemli Date: Tue, 22 May 2007 23:08:46 +0200 Subject: 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 --- cgit.css | 25 +++++++++++++++++++++++++ cgit.h | 22 +++++++++++++++++++++- ui-commit.c | 4 ++-- ui-shared.c | 47 ++++++++++++++++++++++++++++++++++++++++++++--- ui-summary.c | 4 ++-- 5 files changed, 94 insertions(+), 8 deletions(-) diff --git a/cgit.css b/cgit.css index 95c3e40..327eaba 100644 --- a/cgit.css +++ b/cgit.css @@ -388,3 +388,28 @@ td.toplevel-repo { 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 8927236..4da2d3d 100644 --- a/cgit.h +++ b/cgit.h @@ -29,6 +29,25 @@ #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 ff1fad3..59eeb1d 100644 --- a/ui-commit.c +++ b/ui-commit.c @@ -172,14 +172,14 @@ void cgit_print_commit(const char *hex) html(" "); html_txt(info->author_email); html(""); - cgit_print_date(info->author_date); + cgit_print_date(info->author_date, FMT_LONGDATE); html("\n"); html("committer"); html_txt(info->committer); html(" "); html_txt(info->committer_email); html(""); - cgit_print_date(info->committer_date); + cgit_print_date(info->committer_date, FMT_LONGDATE); html("\n"); html("tree%.0f min.", + secs * 1.0 / TM_MIN); + return; + } + if (secs < TM_DAY * 2) { + htmlf("%.0f hours", + secs * 1.0 / TM_HOUR); + return; + } + if (secs < TM_WEEK * 2) { + htmlf("%.0f days", + secs * 1.0 / TM_DAY); + return; + } + if (secs < TM_MONTH * 2) { + htmlf("%.0f weeks", + secs * 1.0 / TM_WEEK); + return; + } + if (secs < TM_YEAR * 2) { + htmlf("%.0f months", + secs * 1.0 / TM_MONTH); + return; + } + htmlf("%.0f years", + secs * 1.0 / TM_YEAR); +} + void cgit_print_docstart(char *title, struct cacheitem *item) { html("Content-Type: text/html; charset=utf-8\n"); diff --git a/ui-summary.c b/ui-summary.c index e7158cc..20394de 100644 --- a/ui-summary.c +++ b/ui-summary.c @@ -28,7 +28,7 @@ static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1, html_txt(buf); html_link_close(); html(""); - cgit_print_date(commit->date); + cgit_print_date(commit->date, FMT_LONGDATE); html(""); html_txt(info->author); html(""); @@ -108,7 +108,7 @@ static int cgit_print_tag_cb(const char *refname, const unsigned char *sha1, html_link_close(); html(""); if (info->tagger_date > 0) - cgit_print_date(info->tagger_date); + cgit_print_date(info->tagger_date, FMT_LONGDATE); html(""); if (info->tagger) html(info->tagger); -- cgit 1.4.1