Add `human` format to test-tool
Add the human format support to the test tool so that GIT_TEST_DATE_NOW can be used to specify the current time. The get_time() helper function was created and and checks the GIT_TEST_DATE_NOW environment variable. If GIT_TEST_DATE_NOW is set, then that date is used instead of the date returned by by gettimeofday(). All calls to gettimeofday() were replaced by calls to get_time(). Renamed occurances of TEST_DATE_NOW to GIT_TEST_DATE_NOW since the variable is now used in the get binary and not just in the test-tool. Signed-off-by: Stephen P. Smith <ischis2@cox.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Stephen P. Smith committed
Jan 28, 2019 at 20:50 UTC
b841d4ff438576fe0c3f69b7d48ba46a442f162b
4 files changed
+33
-7
cache.h
+2
@@ -1453,6 +1453,8 @@ struct date_mode *date_mode_from_type(enum date_mode_type type);
1453
const char *show_date(timestamp_t time, int timezone, const struct date_mode *mode);
1454
void show_date_relative(timestamp_t time, int tz, const struct timeval *now,
1455
struct strbuf *timebuf);
1456
+void show_date_human(timestamp_t time, int tz, const struct timeval *now,
1457
+ struct strbuf *timebuf);
1458
int parse_date(const char *date, struct strbuf *out);
1459
int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset);
1460
int parse_expiry_date(const char *date, timestamp_t *timestamp);
date.c
+17
-4
@@ -115,6 +115,19 @@ static int local_tzoffset(timestamp_t time)
115
return local_time_tzoffset((time_t)time, &tm);
116
}
117
118
+static void get_time(struct timeval *now)
119
+{
120
+ const char *x;
121
+
122
+ x = getenv("GIT_TEST_DATE_NOW");
123
+ if (x) {
124
+ now->tv_sec = atoi(x);
125
+ now->tv_usec = 0;
126
+ }
127
+ else
128
+ gettimeofday(now, NULL);
129
+}
130
+
131
void show_date_relative(timestamp_t time, int tz,
132
const struct timeval *now,
133
struct strbuf *timebuf)
@@ -228,7 +241,7 @@ static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm
241
/* Show "today" times as just relative times */
242
if (hide.wday) {
243
struct timeval now;
231
- gettimeofday(&now, NULL);
244
+ get_time(&now);
245
show_date_relative(time, tz, &now, buf);
246
return;
247
}
@@ -284,7 +297,7 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
297
if (mode->type == DATE_HUMAN) {
298
struct timeval now;
299
287
- gettimeofday(&now, NULL);
300
+ get_time(&now);
301
302
/* Fill in the data for "current time" in human_tz and human_tm */
303
human_tz = local_time_tzoffset(now.tv_sec, &human_tm);
@@ -303,7 +316,7 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
316
struct timeval now;
317
318
strbuf_reset(&timebuf);
306
- gettimeofday(&now, NULL);
319
+ get_time(&now);
320
show_date_relative(time, tz, &now, &timebuf);
321
return timebuf.buf;
322
}
@@ -1290,7 +1303,7 @@ timestamp_t approxidate_careful(const char *date, int *error_ret)
1303
return timestamp;
1304
}
1305
1293
- gettimeofday(&tv, NULL);
1306
+ get_time(&tv);
1307
return approxidate_str(date, &tv, error_ret);
1308
}
1309
t/helper/test-date.c
+12
-1
@@ -3,6 +3,7 @@
3
4
static const char *usage_msg = "\n"
5
" test-tool date relative [time_t]...\n"
6
+" test-tool date human [time_t]...\n"
7
" test-tool date show:<format> [time_t]...\n"
8
" test-tool date parse [date]...\n"
9
" test-tool date approxidate [date]...\n"
@@ -22,6 +23,14 @@ static void show_relative_dates(const char **argv, struct timeval *now)
23
strbuf_release(&buf);
24
}
25
26
+static void show_human_dates(const char **argv)
27
+{
28
+ for (; *argv; argv++) {
29
+ time_t t = atoi(*argv);
30
+ printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(HUMAN)));
31
+ }
32
+}
33
+
34
static void show_dates(const char **argv, const char *format)
35
{
36
struct date_mode mode;
@@ -87,7 +96,7 @@ int cmd__date(int argc, const char **argv)
96
struct timeval now;
97
const char *x;
98
90
- x = getenv("TEST_DATE_NOW");
99
+ x = getenv("GIT_TEST_DATE_NOW");
100
if (x) {
101
now.tv_sec = atoi(x);
102
now.tv_usec = 0;
@@ -100,6 +109,8 @@ int cmd__date(int argc, const char **argv)
109
usage(usage_msg);
110
if (!strcmp(*argv, "relative"))
111
show_relative_dates(argv+1, &now);
112
+ else if (!strcmp(*argv, "human"))
113
+ show_human_dates(argv+1);
114
else if (skip_prefix(*argv, "show:", &x))
115
show_dates(argv+1, x);
116
else if (!strcmp(*argv, "parse"))
t/t0006-date.sh
+2
-2
@@ -4,10 +4,10 @@ test_description='test date parsing and printing'
4
. ./test-lib.sh
5
6
# arbitrary reference time: 2009-08-30 19:20:00
7
-TEST_DATE_NOW=1251660000; export TEST_DATE_NOW
7
+GIT_TEST_DATE_NOW=1251660000; export GIT_TEST_DATE_NOW
8
9
check_relative() {
10
- t=$(($TEST_DATE_NOW - $1))
10
+ t=$(($GIT_TEST_DATE_NOW - $1))
11
echo "$t -> $2" >expect
12
test_expect_${3:-success} "relative date ($2)" "
13
test-tool date relative $t >actual &&