{"thread":{"id":"5408","subject":"[PATCH] Simplify code outputting relative timestamps in git log","startedAt":"2006-08-27T14:23:28Z","lastAt":"2006-08-27T14:27:52Z","messageCount":2,"participants":["Nikolai Weibull"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"25986","messageId":"11566886081901-git-send-email-now@bitwi.se","threadId":"5408","inReplyTo":null,"subject":"[PATCH] Simplify code outputting relative timestamps in git log","fromName":"Nikolai Weibull","fromEmail":"now@bitwi.se","sentAt":"2006-08-27T14:23:28Z","receivedAt":"2006-08-27T14:23:28Z","isPatch":true,"sender":{"key":"now@bitwi.se","avatar":"https://gravatar.com/avatar/d9242f067845cf9a72be23e4213c3b6e53492178e5df97372088a441af846133?d=mp&s=160"},"body":"From: Nikolai Weibull <now@puritan.pcp.ath.cx>\n\nThe code that outputs relative timestamps is repetitive and can be\nsimplified by using an array to deal with the various cutoffs.  This makes\nit easier to modify and remove the cutoffs if we in the future desire to do\nso.\n\nSigned-off-by: Nikolai Weibull <now@bitwi.se>\n---\n date.c |   63 +++++++++++++++++++++++++++++++--------------------------------\n 1 files changed, 31 insertions(+), 32 deletions(-)\n\ndiff --git a/date.c b/date.c\nindex e387dcd..5891fa8 100644\n--- a/date.c\n+++ b/date.c\n@@ -64,6 +64,29 @@ const char *show_date(unsigned long time\n \tstatic char timebuf[200];\n \n \tif (relative) {\n+\t\tstatic struct {\n+\t\t\tchar name[8];\n+\t\t\tunsigned long cutoff;\n+\t\t\tunsigned long factor;\n+\t\t\tunsigned long term;\n+\t\t} cutoffs[] = {\n+#define CUTOFF(name, cutoff, factor, ceiling) \\\n+\t\t\t{ (name), (cutoff) * (factor), (factor), (ceiling) }\n+#define MINUTES(minutes)        ((minutes) * 60)\n+#define HOURS(hours)            ((hours) * MINUTES(60))\n+#define DAYS(days)              ((days) * HOURS(24))\n+\t\t\tCUTOFF(\"seconds\", 90, 1, 0),\n+\t\t\tCUTOFF(\"minutes\", 90, MINUTES(1), 30),\n+\t\t\tCUTOFF(\"hours\", 36, HOURS(1), MINUTES(30)),\n+\t\t\tCUTOFF(\"days\", 14, DAYS(1), HOURS(12)),\n+\t\t\tCUTOFF(\"weeks\", 12, DAYS(7), HOURS(12)),\n+\t\t\tCUTOFF(\"months\", 12, DAYS(30), HOURS(12))\n+#undef MINUTES\n+#undef DAYS\n+#undef HOURS\n+#undef CUTOFF\n+\t\t};\n+\t\tint i;\n \t\tunsigned long diff;\n \t\ttime_t t = gm_time_t(time, tz);\n \t\tstruct timeval now;\n@@ -71,39 +94,15 @@ const char *show_date(unsigned long time\n \t\tif (now.tv_sec < t)\n \t\t\treturn \"in the future\";\n \t\tdiff = now.tv_sec - t;\n-\t\tif (diff < 90) {\n-\t\t\tsnprintf(timebuf, sizeof(timebuf), \"%lu seconds ago\", diff);\n-\t\t\treturn timebuf;\n-\t\t}\n-\t\t/* Turn it into minutes */\n-\t\tdiff = (diff + 30) / 60;\n-\t\tif (diff < 90) {\n-\t\t\tsnprintf(timebuf, sizeof(timebuf), \"%lu minutes ago\", diff);\n-\t\t\treturn timebuf;\n-\t\t}\n-\t\t/* Turn it into hours */\n-\t\tdiff = (diff + 30) / 60;\n-\t\tif (diff < 36) {\n-\t\t\tsnprintf(timebuf, sizeof(timebuf), \"%lu hours ago\", diff);\n-\t\t\treturn timebuf;\n-\t\t}\n-\t\t/* We deal with number of days from here on */\n-\t\tdiff = (diff + 12) / 24;\n-\t\tif (diff < 14) {\n-\t\t\tsnprintf(timebuf, sizeof(timebuf), \"%lu days ago\", diff);\n-\t\t\treturn timebuf;\n-\t\t}\n-\t\t/* Say weeks for the past 10 weeks or so */\n-\t\tif (diff < 70) {\n-\t\t\tsnprintf(timebuf, sizeof(timebuf), \"%lu weeks ago\", (diff + 3) / 7);\n-\t\t\treturn timebuf;\n-\t\t}\n-\t\t/* Say months for the past 12 months or so */\n-\t\tif (diff < 360) {\n-\t\t\tsnprintf(timebuf, sizeof(timebuf), \"%lu months ago\", (diff + 15) / 30);\n-\t\t\treturn timebuf;\n+\t\tfor (i = 0; i < ARRAY_SIZE(cutoffs); i++) {\n+\t\t\tif (diff < cutoffs[i].cutoff) {\n+\t\t\t\tsnprintf(timebuf, sizeof(timebuf), \"%lu %s ago\",\n+\t\t\t\t\t(diff + cutoffs[i].term) / cutoffs[i].factor,\n+\t\t\t\t\tcutoffs[i].name);\n+\t\t\t\treturn timebuf;\n+\t\t\t}\n \t\t}\n-\t\t/* Else fall back on absolute format.. */\n+\t\t/* If we went beyond the month cutoff, use absolute format. */\n \t}\n \n \ttm = time_to_tm(time, tz);\n-- \n1.4.2.GIT-dirty\n"},{"id":"25987","messageId":"dbfc82860608270727w783688ecva69013e2437c7729@mail.gmail.com","threadId":"5408","inReplyTo":"11566886081901-git-send-email-now@bitwi.se","subject":"Re: [PATCH] Simplify code outputting relative timestamps in git log","fromName":"Nikolai Weibull","fromEmail":"now@bitwi.se","sentAt":"2006-08-27T14:27:52Z","receivedAt":"2006-08-27T14:27:52Z","isPatch":true,"sender":{"key":"now@bitwi.se","avatar":"https://gravatar.com/avatar/d9242f067845cf9a72be23e4213c3b6e53492178e5df97372088a441af846133?d=mp&s=160"},"body":"On 8/27/06, Nikolai Weibull <now@bitwi.se> wrote:\n> From: Nikolai Weibull <now@puritan.pcp.ath.cx>\n\nHm...why is format-patch not using user.email?\n\n> The code that outputs relative timestamps is repetitive and can be\n> simplified by using an array to deal with the various cutoffs.  This makes\n> it easier to modify and remove the cutoffs if we in the future desire to do\n> so.\n\nI just realized that I forgot to update the cutoffs to those in the\nrepository.  I was playing around with other cutoffs.  I'll send a new\npatch if it is of any interest.\n\n  nikolai\n"}]}