git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Can show relative date of the commit with future timestamp
@ 2012-07-05 16:18 Tom Xue
  2012-07-05 17:26 ` Junio C Hamano
  2012-08-16 23:02 ` [PATCH 0/2] relative future date Junio C Hamano
  0 siblings, 2 replies; 8+ messages in thread
From: Tom Xue @ 2012-07-05 16:18 UTC (permalink / raw)
  To: git, gitster; +Cc: Tom Xue

---
 date.c | 186 ++++++++++++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 125 insertions(+), 61 deletions(-)

diff --git a/date.c b/date.c
index 1fdcf7c..f1ed2ba 100644
--- a/date.c
+++ b/date.c
@@ -92,72 +92,136 @@ void show_date_relative(unsigned long time, int tz,
 {
 	unsigned long diff;
 	if (now->tv_sec < time) {
-		strbuf_addstr(timebuf, _("in the future"));
-		return;
-	}
-	diff = now->tv_sec - time;
-	if (diff < 90) {
-		strbuf_addf(timebuf,
-			 Q_("%lu second ago", "%lu seconds ago", diff), diff);
-		return;
-	}
-	/* Turn it into minutes */
-	diff = (diff + 30) / 60;
-	if (diff < 90) {
-		strbuf_addf(timebuf,
-			 Q_("%lu minute ago", "%lu minutes ago", diff), diff);
-		return;
-	}
-	/* Turn it into hours */
-	diff = (diff + 30) / 60;
-	if (diff < 36) {
-		strbuf_addf(timebuf,
-			 Q_("%lu hour ago", "%lu hours ago", diff), diff);
-		return;
-	}
-	/* We deal with number of days from here on */
-	diff = (diff + 12) / 24;
-	if (diff < 14) {
-		strbuf_addf(timebuf,
-			 Q_("%lu day ago", "%lu days ago", diff), diff);
-		return;
-	}
-	/* Say weeks for the past 10 weeks or so */
-	if (diff < 70) {
-		strbuf_addf(timebuf,
-			 Q_("%lu week ago", "%lu weeks ago", (diff + 3) / 7),
-			 (diff + 3) / 7);
-		return;
-	}
-	/* Say months for the past 12 months or so */
-	if (diff < 365) {
+		strbuf_addstr(timebuf, _("in the future: "));
+		diff = time - now->tv_sec;
+		if (diff < 90) {
+			strbuf_addf(timebuf,
+				 Q_("%lu second later", "%lu seconds later", diff), diff);
+			return;
+		}
+		/* Turn it into minutes */
+		diff = (diff + 30) / 60;
+		if (diff < 90) {
+			strbuf_addf(timebuf,
+				 Q_("%lu minute later", "%lu minutes later", diff), diff);
+			return;
+		}
+		/* Turn it into hours */
+		diff = (diff + 30) / 60;
+		if (diff < 36) {
+			strbuf_addf(timebuf,
+				 Q_("%lu hour later", "%lu hours later", diff), diff);
+			return;
+		}
+		/* We deal with number of days from here on */
+		diff = (diff + 12) / 24;
+		if (diff < 14) {
+			strbuf_addf(timebuf,
+				 Q_("%lu day later", "%lu days later", diff), diff);
+			return;
+		}
+		/* Say weeks for the past 10 weeks or so */
+		if (diff < 70) {
+			strbuf_addf(timebuf,
+				 Q_("%lu week later", "%lu weeks later", (diff + 3) / 7),
+				 (diff + 3) / 7);
+			return;
+		}
+		/* Say months for the past 12 months or so */
+		if (diff < 365) {
+			strbuf_addf(timebuf,
+				 Q_("%lu month later", "%lu months later", (diff + 15) / 30),
+				 (diff + 15) / 30);
+			return;
+		}
+		/* Give years and months for 5 years or so */
+		if (diff < 1825) {
+			unsigned long totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
+			unsigned long years = totalmonths / 12;
+			unsigned long months = totalmonths % 12;
+			if (months) {
+				struct strbuf sb = STRBUF_INIT;
+				strbuf_addf(&sb, Q_("%lu year", "%lu years", years), years);
+				/* TRANSLATORS: "%s" is "<n> years" */
+				strbuf_addf(timebuf,
+					 Q_("%s, %lu month later", "%s, %lu months later", months),
+					 sb.buf, months);
+				strbuf_release(&sb);
+			} else
+				strbuf_addf(timebuf,
+					 Q_("%lu year later", "%lu years later", years), years);
+			return;
+		}
+		/* Otherwise, just years. Centuries is probably overkill. */
 		strbuf_addf(timebuf,
-			 Q_("%lu month ago", "%lu months ago", (diff + 15) / 30),
-			 (diff + 15) / 30);
-		return;
+			 Q_("%lu year later", "%lu years later", (diff + 183) / 365),
+			 (diff + 183) / 365);
 	}
-	/* Give years and months for 5 years or so */
-	if (diff < 1825) {
-		unsigned long totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
-		unsigned long years = totalmonths / 12;
-		unsigned long months = totalmonths % 12;
-		if (months) {
-			struct strbuf sb = STRBUF_INIT;
-			strbuf_addf(&sb, Q_("%lu year", "%lu years", years), years);
-			/* TRANSLATORS: "%s" is "<n> years" */
+	else {
+		diff = now->tv_sec - time;
+		if (diff < 90) {
+			strbuf_addf(timebuf,
+				 Q_("%lu second ago", "%lu seconds ago", diff), diff);
+			return;
+		}
+		/* Turn it into minutes */
+		diff = (diff + 30) / 60;
+		if (diff < 90) {
+			strbuf_addf(timebuf,
+				 Q_("%lu minute ago", "%lu minutes ago", diff), diff);
+			return;
+		}
+		/* Turn it into hours */
+		diff = (diff + 30) / 60;
+		if (diff < 36) {
 			strbuf_addf(timebuf,
-				 Q_("%s, %lu month ago", "%s, %lu months ago", months),
-				 sb.buf, months);
-			strbuf_release(&sb);
-		} else
+				 Q_("%lu hour ago", "%lu hours ago", diff), diff);
+			return;
+		}
+		/* We deal with number of days from here on */
+		diff = (diff + 12) / 24;
+		if (diff < 14) {
 			strbuf_addf(timebuf,
-				 Q_("%lu year ago", "%lu years ago", years), years);
-		return;
+				 Q_("%lu day ago", "%lu days ago", diff), diff);
+			return;
+		}
+		/* Say weeks for the past 10 weeks or so */
+		if (diff < 70) {
+			strbuf_addf(timebuf,
+				 Q_("%lu week ago", "%lu weeks ago", (diff + 3) / 7),
+				 (diff + 3) / 7);
+			return;
+		}
+		/* Say months for the past 12 months or so */
+		if (diff < 365) {
+			strbuf_addf(timebuf,
+				 Q_("%lu month ago", "%lu months ago", (diff + 15) / 30),
+				 (diff + 15) / 30);
+			return;
+		}
+		/* Give years and months for 5 years or so */
+		if (diff < 1825) {
+			unsigned long totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
+			unsigned long years = totalmonths / 12;
+			unsigned long months = totalmonths % 12;
+			if (months) {
+				struct strbuf sb = STRBUF_INIT;
+				strbuf_addf(&sb, Q_("%lu year", "%lu years", years), years);
+				/* TRANSLATORS: "%s" is "<n> years" */
+				strbuf_addf(timebuf,
+					 Q_("%s, %lu month ago", "%s, %lu months ago", months),
+					 sb.buf, months);
+				strbuf_release(&sb);
+			} else
+				strbuf_addf(timebuf,
+					 Q_("%lu year ago", "%lu years ago", years), years);
+			return;
+		}
+		/* Otherwise, just years. Centuries is probably overkill. */
+		strbuf_addf(timebuf,
+			 Q_("%lu year ago", "%lu years ago", (diff + 183) / 365),
+			 (diff + 183) / 365);
 	}
-	/* Otherwise, just years. Centuries is probably overkill. */
-	strbuf_addf(timebuf,
-		 Q_("%lu year ago", "%lu years ago", (diff + 183) / 365),
-		 (diff + 183) / 365);
 }
 
 const char *show_date(unsigned long time, int tz, enum date_mode mode)
-- 
1.7.11.GIT

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-08-21  9:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-07-05 16:18 [PATCH] Can show relative date of the commit with future timestamp Tom Xue
2012-07-05 17:26 ` Junio C Hamano
     [not found]   ` <CA+P7oHuCbeJ5shfL5OVhSypgygzmQwn-RGPD8UvFUFuqnoV9fA@mail.gmail.com>
2012-07-06  8:15     ` Jundong Xue
2012-08-20 22:51       ` Junio C Hamano
2012-08-21  9:17         ` Jundong Xue
2012-08-16 23:02 ` [PATCH 0/2] relative future date Junio C Hamano
2012-08-16 23:02   ` [PATCH 1/2] date: refactor the relative date logic from presentation Junio C Hamano
2012-08-16 23:02   ` [PATCH 2/2] date: show relative dates in the future Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).