Quit passing 'now' to date code

Commit b841d4ff43 (Add `human` format to test-tool, 2019-01-28) added a get_time() function which allows $GIT_TEST_DATE_NOW in the environment to override the current time. So we no longer need to interpret that variable in cmd__date(). Therefore, we can stop passing the "now" parameter down through the date functions, since nobody uses them. Note that we do need to make sure all of the previous callers that took a "now" parameter are correctly using get_time(). Signed-off-by: Stephen P. Smith <ischis2@cox.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stephen P. Smith committed Sep 11, 2019 at 21:11 UTC 29f4332e66abf41e90d2f38e62370a42d8549c7e
3 files changed +24 -34
cache.h
+2 -3
@@ -1516,8 +1516,7 @@ struct date_mode {
1516 struct date_mode *date_mode_from_type(enum date_mode_type type);
1517
1518 const char *show_date(timestamp_t time, int timezone, const struct date_mode *mode);
1519 -void show_date_relative(timestamp_t time, const struct timeval *now,
1520 - struct strbuf *timebuf);
1519 +void show_date_relative(timestamp_t time, struct strbuf *timebuf);
1520 void show_date_human(timestamp_t time, int tz, const struct timeval *now,
1521 struct strbuf *timebuf);
1522 int parse_date(const char *date, struct strbuf *out);
@@ -1526,7 +1525,7 @@ int parse_expiry_date(const char *date, timestamp_t *timestamp);
1525 void datestamp(struct strbuf *out);
1526 #define approxidate(s) approxidate_careful((s), NULL)
1527 timestamp_t approxidate_careful(const char *, int *);
1529 -timestamp_t approxidate_relative(const char *date, const struct timeval *now);
1528 +timestamp_t approxidate_relative(const char *date);
1529 void parse_date_format(const char *format, struct date_mode *mode);
1530 int date_overflows(timestamp_t date);
1531
date.c
+13 -14
@@ -128,16 +128,17 @@ static void get_time(struct timeval *now)
128 gettimeofday(now, NULL);
129 }
130
131 -void show_date_relative(timestamp_t time,
132 - const struct timeval *now,
133 - struct strbuf *timebuf)
131 +void show_date_relative(timestamp_t time, struct strbuf *timebuf)
132 {
133 + struct timeval now;
134 timestamp_t diff;
136 - if (now->tv_sec < time) {
135 +
136 + get_time(&now);
137 + if (now.tv_sec < time) {
138 strbuf_addstr(timebuf, _("in the future"));
139 return;
140 }
140 - diff = now->tv_sec - time;
141 + diff = now.tv_sec - time;
142 if (diff < 90) {
143 strbuf_addf(timebuf,
144 Q_("%"PRItime" second ago", "%"PRItime" seconds ago", diff), diff);
@@ -240,9 +241,7 @@ static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm
241
242 /* Show "today" times as just relative times */
243 if (hide.wday) {
243 - struct timeval now;
244 - get_time(&now);
245 - show_date_relative(time, &now, buf);
244 + show_date_relative(time, buf);
245 return;
246 }
247
@@ -313,11 +312,8 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
312 }
313
314 if (mode->type == DATE_RELATIVE) {
316 - struct timeval now;
317 -
315 strbuf_reset(&timebuf);
319 - get_time(&now);
320 - show_date_relative(time, &now, &timebuf);
316 + show_date_relative(time, &timebuf);
317 return timebuf.buf;
318 }
319
@@ -1288,15 +1284,18 @@ static timestamp_t approxidate_str(const char *date,
1284 return (timestamp_t)update_tm(&tm, &now, 0);
1285 }
1286
1291 -timestamp_t approxidate_relative(const char *date, const struct timeval *tv)
1287 +timestamp_t approxidate_relative(const char *date)
1288 {
1289 + struct timeval tv;
1290 timestamp_t timestamp;
1291 int offset;
1292 int errors = 0;
1293
1294 if (!parse_date_basic(date, &timestamp, &offset))
1295 return timestamp;
1299 - return approxidate_str(date, tv, &errors);
1296 +
1297 + get_time(&tv);
1298 + return approxidate_str(date, (const struct timeval *) &tv, &errors);
1299 }
1300
1301 timestamp_t approxidate_careful(const char *date, int *error_ret)
t/helper/test-date.c
+9 -17
@@ -12,13 +12,13 @@ static const char *usage_msg = "\n"
12 " test-tool date is64bit\n"
13 " test-tool date time_t-is64bit\n";
14
15 -static void show_relative_dates(const char **argv, struct timeval *now)
15 +static void show_relative_dates(const char **argv)
16 {
17 struct strbuf buf = STRBUF_INIT;
18
19 for (; *argv; argv++) {
20 time_t t = atoi(*argv);
21 - show_date_relative(t, now, &buf);
21 + show_date_relative(t, &buf);
22 printf("%s -> %s\n", *argv, buf.buf);
23 }
24 strbuf_release(&buf);
@@ -74,20 +74,20 @@ static void parse_dates(const char **argv)
74 strbuf_release(&result);
75 }
76
77 -static void parse_approxidate(const char **argv, struct timeval *now)
77 +static void parse_approxidate(const char **argv)
78 {
79 for (; *argv; argv++) {
80 timestamp_t t;
81 - t = approxidate_relative(*argv, now);
81 + t = approxidate_relative(*argv);
82 printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(ISO8601)));
83 }
84 }
85
86 -static void parse_approx_timestamp(const char **argv, struct timeval *now)
86 +static void parse_approx_timestamp(const char **argv)
87 {
88 for (; *argv; argv++) {
89 timestamp_t t;
90 - t = approxidate_relative(*argv, now);
90 + t = approxidate_relative(*argv);
91 printf("%s -> %"PRItime"\n", *argv, t);
92 }
93 }
@@ -103,22 +103,14 @@ static void getnanos(const char **argv)
103
104 int cmd__date(int argc, const char **argv)
105 {
106 - struct timeval now;
106 const char *x;
108 -
107 x = getenv("GIT_TEST_DATE_NOW");
110 - if (x) {
111 - now.tv_sec = atoi(x);
112 - now.tv_usec = 0;
113 - }
114 - else
115 - gettimeofday(&now, NULL);
108
109 argv++;
110 if (!*argv)
111 usage(usage_msg);
112 if (!strcmp(*argv, "relative"))
121 - show_relative_dates(argv+1, &now);
113 + show_relative_dates(argv+1);
114 else if (!strcmp(*argv, "human"))
115 show_human_dates(argv+1);
116 else if (skip_prefix(*argv, "show:", &x))
@@ -126,9 +118,9 @@ int cmd__date(int argc, const char **argv)
118 else if (!strcmp(*argv, "parse"))
119 parse_dates(argv+1);
120 else if (!strcmp(*argv, "approxidate"))
129 - parse_approxidate(argv+1, &now);
121 + parse_approxidate(argv+1);
122 else if (!strcmp(*argv, "timestamp"))
131 - parse_approx_timestamp(argv+1, &now);
123 + parse_approx_timestamp(argv+1);
124 else if (!strcmp(*argv, "getnanos"))
125 getnanos(argv+1);
126 else if (!strcmp(*argv, "is64bit"))