]> git.ipfire.org Git - thirdparty/git.git/blobdiff - date.c
Revert "t5562: replace /dev/zero with a pipe from generate_zero_bytes"
[thirdparty/git.git] / date.c
diff --git a/date.c b/date.c
index f731803872c0fd170e711ec1cb11475116146414..9c5870e102951ed25f5def2103dab15bf8ed5c99 100644 (file)
--- a/date.c
+++ b/date.c
@@ -115,9 +115,22 @@ static int local_tzoffset(timestamp_t time)
        return local_time_tzoffset((time_t)time, &tm);
 }
 
-void show_date_relative(timestamp_t time, int tz,
-                              const struct timeval *now,
-                              struct strbuf *timebuf)
+static void get_time(struct timeval *now)
+{
+       const char *x;
+
+       x = getenv("GIT_TEST_DATE_NOW");
+       if (x) {
+               now->tv_sec = atoi(x);
+               now->tv_usec = 0;
+       }
+       else
+               gettimeofday(now, NULL);
+}
+
+void show_date_relative(timestamp_t time,
+                       const struct timeval *now,
+                       struct strbuf *timebuf)
 {
        timestamp_t diff;
        if (now->tv_sec < time) {
@@ -228,8 +241,8 @@ static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm
        /* Show "today" times as just relative times */
        if (hide.wday) {
                struct timeval now;
-               gettimeofday(&now, NULL);
-               show_date_relative(time, tz, &now, buf);
+               get_time(&now);
+               show_date_relative(time, &now, buf);
                return;
        }
 
@@ -284,7 +297,7 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
        if (mode->type == DATE_HUMAN) {
                struct timeval now;
 
-               gettimeofday(&now, NULL);
+               get_time(&now);
 
                /* Fill in the data for "current time" in human_tz and human_tm */
                human_tz = local_time_tzoffset(now.tv_sec, &human_tm);
@@ -303,8 +316,8 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
                struct timeval now;
 
                strbuf_reset(&timebuf);
-               gettimeofday(&now, NULL);
-               show_date_relative(time, tz, &now, &timebuf);
+               get_time(&now);
+               show_date_relative(time, &now, &timebuf);
                return timebuf.buf;
        }
 
@@ -978,20 +991,49 @@ static time_t update_tm(struct tm *tm, struct tm *now, time_t sec)
        return n;
 }
 
+/*
+ * Do we have a pending number at the end, or when
+ * we see a new one? Let's assume it's a month day,
+ * as in "Dec 6, 1992"
+ */
+static void pending_number(struct tm *tm, int *num)
+{
+       int number = *num;
+
+       if (number) {
+               *num = 0;
+               if (tm->tm_mday < 0 && number < 32)
+                       tm->tm_mday = number;
+               else if (tm->tm_mon < 0 && number < 13)
+                       tm->tm_mon = number-1;
+               else if (tm->tm_year < 0) {
+                       if (number > 1969 && number < 2100)
+                               tm->tm_year = number - 1900;
+                       else if (number > 69 && number < 100)
+                               tm->tm_year = number;
+                       else if (number < 38)
+                               tm->tm_year = 100 + number;
+                       /* We screw up for number = 00 ? */
+               }
+       }
+}
+
 static void date_now(struct tm *tm, struct tm *now, int *num)
 {
+       *num = 0;
        update_tm(tm, now, 0);
 }
 
 static void date_yesterday(struct tm *tm, struct tm *now, int *num)
 {
+       *num = 0;
        update_tm(tm, now, 24*60*60);
 }
 
 static void date_time(struct tm *tm, struct tm *now, int hour)
 {
        if (tm->tm_hour < hour)
-               date_yesterday(tm, now, NULL);
+               update_tm(tm, now, 24*60*60);
        tm->tm_hour = hour;
        tm->tm_min = 0;
        tm->tm_sec = 0;
@@ -999,16 +1041,19 @@ static void date_time(struct tm *tm, struct tm *now, int hour)
 
 static void date_midnight(struct tm *tm, struct tm *now, int *num)
 {
+       pending_number(tm, num);
        date_time(tm, now, 0);
 }
 
 static void date_noon(struct tm *tm, struct tm *now, int *num)
 {
+       pending_number(tm, num);
        date_time(tm, now, 12);
 }
 
 static void date_tea(struct tm *tm, struct tm *now, int *num)
 {
+       pending_number(tm, num);
        date_time(tm, now, 17);
 }
 
@@ -1044,6 +1089,7 @@ static void date_never(struct tm *tm, struct tm *now, int *num)
 {
        time_t n = 0;
        localtime_r(&n, tm);
+       *num = 0;
 }
 
 static const struct special {
@@ -1201,33 +1247,6 @@ static const char *approxidate_digit(const char *date, struct tm *tm, int *num,
        return end;
 }
 
-/*
- * Do we have a pending number at the end, or when
- * we see a new one? Let's assume it's a month day,
- * as in "Dec 6, 1992"
- */
-static void pending_number(struct tm *tm, int *num)
-{
-       int number = *num;
-
-       if (number) {
-               *num = 0;
-               if (tm->tm_mday < 0 && number < 32)
-                       tm->tm_mday = number;
-               else if (tm->tm_mon < 0 && number < 13)
-                       tm->tm_mon = number-1;
-               else if (tm->tm_year < 0) {
-                       if (number > 1969 && number < 2100)
-                               tm->tm_year = number - 1900;
-                       else if (number > 69 && number < 100)
-                               tm->tm_year = number;
-                       else if (number < 38)
-                               tm->tm_year = 100 + number;
-                       /* We screw up for number = 00 ? */
-               }
-       }
-}
-
 static timestamp_t approxidate_str(const char *date,
                                   const struct timeval *tv,
                                   int *error_ret)
@@ -1290,7 +1309,7 @@ timestamp_t approxidate_careful(const char *date, int *error_ret)
                return timestamp;
        }
 
-       gettimeofday(&tv, NULL);
+       get_time(&tv);
        return approxidate_str(date, &tv, error_ret);
 }