Code

Update documentation links to point at 1.5.1.4
[git.git] / date.c
diff --git a/date.c b/date.c
index db4c185431b504a5d5295e21c7c6c5f08cd54d55..a9b59a289e7b22f34958ccc7b80b02a01cf793c6 100644 (file)
--- a/date.c
+++ b/date.c
@@ -4,9 +4,6 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 
-#include <time.h>
-#include <sys/time.h>
-
 #include "cache.h"
 
 static time_t my_mktime(struct tm *tm)
@@ -58,19 +55,44 @@ static struct tm *time_to_tm(unsigned long time, int tz)
        return gmtime(&t);
 }
 
-const char *show_date(unsigned long time, int tz, int relative)
+/*
+ * What value of "tz" was in effect back then at "time" in the
+ * local timezone?
+ */
+static int local_tzoffset(unsigned long time)
+{
+       time_t t, t_local;
+       struct tm tm;
+       int offset, eastwest;
+
+       t = time;
+       localtime_r(&t, &tm);
+       t_local = my_mktime(&tm);
+
+       if (t_local < t) {
+               eastwest = -1;
+               offset = t - t_local;
+       } else {
+               eastwest = 1;
+               offset = t_local - t;
+       }
+       offset /= 60; /* in minutes */
+       offset = (offset % 60) + ((offset / 60) * 100);
+       return offset * eastwest;
+}
+
+const char *show_date(unsigned long time, int tz, enum date_mode mode)
 {
        struct tm *tm;
        static char timebuf[200];
 
-       if (relative) {
+       if (mode == DATE_RELATIVE) {
                unsigned long diff;
-               time_t t = gm_time_t(time, tz);
                struct timeval now;
                gettimeofday(&now, NULL);
-               if (now.tv_sec < t)
+               if (now.tv_sec < time)
                        return "in the future";
-               diff = now.tv_sec - t;
+               diff = now.tv_sec - time;
                if (diff < 90) {
                        snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
                        return timebuf;
@@ -106,15 +128,24 @@ const char *show_date(unsigned long time, int tz, int relative)
                /* Else fall back on absolute format.. */
        }
 
+       if (mode == DATE_LOCAL)
+               tz = local_tzoffset(time);
+
        tm = time_to_tm(time, tz);
        if (!tm)
                return NULL;
-       sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
-               weekday_names[tm->tm_wday],
-               month_names[tm->tm_mon],
-               tm->tm_mday,
-               tm->tm_hour, tm->tm_min, tm->tm_sec,
-               tm->tm_year + 1900, tz);
+       if (mode == DATE_SHORT)
+               sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
+                               tm->tm_mon + 1, tm->tm_mday);
+       else
+               sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
+                               weekday_names[tm->tm_wday],
+                               month_names[tm->tm_mon],
+                               tm->tm_mday,
+                               tm->tm_hour, tm->tm_min, tm->tm_sec,
+                               tm->tm_year + 1900,
+                               (mode == DATE_LOCAL) ? 0 : ' ',
+                               tz);
        return timebuf;
 }
 
@@ -256,8 +287,12 @@ static int match_alpha(const char *date, struct tm *tm, int *offset)
        }
 
        if (match_string(date, "PM") == 2) {
-               if (tm->tm_hour > 0 && tm->tm_hour < 12)
-                       tm->tm_hour += 12;
+               tm->tm_hour = (tm->tm_hour % 12) + 12;
+               return 2;
+       }
+
+       if (match_string(date, "AM") == 2) {
+               tm->tm_hour = (tm->tm_hour % 12) + 0;
                return 2;
        }
 
@@ -600,28 +635,30 @@ static void date_tea(struct tm *tm, int *num)
 
 static void date_pm(struct tm *tm, int *num)
 {
-       int hour = *num;
+       int hour, n = *num;
        *num = 0;
 
-       if (hour > 0 && hour < 12) {
-               tm->tm_hour = hour;
+       hour = tm->tm_hour;
+       if (n) {
+               hour = n;
                tm->tm_min = 0;
                tm->tm_sec = 0;
        }
-       if (tm->tm_hour > 0 && tm->tm_hour < 12)
-               tm->tm_hour += 12;
+       tm->tm_hour = (hour % 12) + 12;
 }
 
 static void date_am(struct tm *tm, int *num)
 {
-       int hour = *num;
+       int hour, n = *num;
        *num = 0;
 
-       if (hour > 0 && hour < 12) {
-               tm->tm_hour = hour;
+       hour = tm->tm_hour;
+       if (n) {
+               hour = n;
                tm->tm_min = 0;
                tm->tm_sec = 0;
        }
+       tm->tm_hour = (hour % 12);
 }
 
 static const struct special {