Code

fetch: do not create ref from empty name
[git.git] / date.c
diff --git a/date.c b/date.c
index 950b88fdcf74f550a582684f1702ffb58c62c7f9..409a17d46424083b62f21e7e278393cee7bfa080 100644 (file)
--- a/date.c
+++ b/date.c
@@ -89,6 +89,11 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
        struct tm *tm;
        static char timebuf[200];
 
+       if (mode == DATE_RAW) {
+               snprintf(timebuf, sizeof(timebuf), "%lu %+05d", time, tz);
+               return timebuf;
+       }
+
        if (mode == DATE_RELATIVE) {
                unsigned long diff;
                struct timeval now;
@@ -128,7 +133,25 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
                        snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
                        return timebuf;
                }
-               /* Else fall back on absolute format.. */
+               /* Give years and months for 5 years or so */
+               if (diff < 1825) {
+                       unsigned long years = (diff + 183) / 365;
+                       unsigned long months = (diff % 365 + 15) / 30;
+                       int n;
+                       n = snprintf(timebuf, sizeof(timebuf), "%lu year%s",
+                                       years, (years > 1 ? "s" : ""));
+                       if (months)
+                               snprintf(timebuf + n, sizeof(timebuf) - n,
+                                       ", %lu month%s ago",
+                                       months, (months > 1 ? "s" : ""));
+                       else
+                               snprintf(timebuf + n, sizeof(timebuf) - n,
+                                       " ago");
+                       return timebuf;
+               }
+               /* Otherwise, just years. Centuries is probably overkill. */
+               snprintf(timebuf, sizeof(timebuf), "%lu years ago", (diff + 183) / 365);
+               return timebuf;
        }
 
        if (mode == DATE_LOCAL)
@@ -615,6 +638,8 @@ enum date_mode parse_date_format(const char *format)
                return DATE_LOCAL;
        else if (!strcmp(format, "default"))
                return DATE_NORMAL;
+       else if (!strcmp(format, "raw"))
+               return DATE_RAW;
        else
                die("unknown date format %s", format);
 }
@@ -846,13 +871,15 @@ unsigned long approxidate(const char *date)
        int number = 0;
        struct tm tm, now;
        struct timeval tv;
+       time_t time_sec;
        char buffer[50];
 
        if (parse_date(date, buffer, sizeof(buffer)) > 0)
                return strtoul(buffer, NULL, 10);
 
        gettimeofday(&tv, NULL);
-       localtime_r(&tv.tv_sec, &tm);
+       time_sec = tv.tv_sec;
+       localtime_r(&time_sec, &tm);
        now = tm;
        for (;;) {
                unsigned char c = *date;