Skip to content

Commit b841d4f

Browse files
Stephen P. Smithgitster
authored andcommitted
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>
1 parent 038a878 commit b841d4f

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,8 @@ struct date_mode *date_mode_from_type(enum date_mode_type type);
14531453
const char *show_date(timestamp_t time, int timezone, const struct date_mode *mode);
14541454
void show_date_relative(timestamp_t time, int tz, const struct timeval *now,
14551455
struct strbuf *timebuf);
1456+
void show_date_human(timestamp_t time, int tz, const struct timeval *now,
1457+
struct strbuf *timebuf);
14561458
int parse_date(const char *date, struct strbuf *out);
14571459
int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset);
14581460
int parse_expiry_date(const char *date, timestamp_t *timestamp);

date.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ static int local_tzoffset(timestamp_t time)
115115
return local_time_tzoffset((time_t)time, &tm);
116116
}
117117

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+
118131
void show_date_relative(timestamp_t time, int tz,
119132
const struct timeval *now,
120133
struct strbuf *timebuf)
@@ -228,7 +241,7 @@ static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm
228241
/* Show "today" times as just relative times */
229242
if (hide.wday) {
230243
struct timeval now;
231-
gettimeofday(&now, NULL);
244+
get_time(&now);
232245
show_date_relative(time, tz, &now, buf);
233246
return;
234247
}
@@ -284,7 +297,7 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
284297
if (mode->type == DATE_HUMAN) {
285298
struct timeval now;
286299

287-
gettimeofday(&now, NULL);
300+
get_time(&now);
288301

289302
/* Fill in the data for "current time" in human_tz and human_tm */
290303
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)
303316
struct timeval now;
304317

305318
strbuf_reset(&timebuf);
306-
gettimeofday(&now, NULL);
319+
get_time(&now);
307320
show_date_relative(time, tz, &now, &timebuf);
308321
return timebuf.buf;
309322
}
@@ -1290,7 +1303,7 @@ timestamp_t approxidate_careful(const char *date, int *error_ret)
12901303
return timestamp;
12911304
}
12921305

1293-
gettimeofday(&tv, NULL);
1306+
get_time(&tv);
12941307
return approxidate_str(date, &tv, error_ret);
12951308
}
12961309

t/helper/test-date.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
static const char *usage_msg = "\n"
55
" test-tool date relative [time_t]...\n"
6+
" test-tool date human [time_t]...\n"
67
" test-tool date show:<format> [time_t]...\n"
78
" test-tool date parse [date]...\n"
89
" test-tool date approxidate [date]...\n"
@@ -22,6 +23,14 @@ static void show_relative_dates(const char **argv, struct timeval *now)
2223
strbuf_release(&buf);
2324
}
2425

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+
2534
static void show_dates(const char **argv, const char *format)
2635
{
2736
struct date_mode mode;
@@ -87,7 +96,7 @@ int cmd__date(int argc, const char **argv)
8796
struct timeval now;
8897
const char *x;
8998

90-
x = getenv("TEST_DATE_NOW");
99+
x = getenv("GIT_TEST_DATE_NOW");
91100
if (x) {
92101
now.tv_sec = atoi(x);
93102
now.tv_usec = 0;
@@ -100,6 +109,8 @@ int cmd__date(int argc, const char **argv)
100109
usage(usage_msg);
101110
if (!strcmp(*argv, "relative"))
102111
show_relative_dates(argv+1, &now);
112+
else if (!strcmp(*argv, "human"))
113+
show_human_dates(argv+1);
103114
else if (skip_prefix(*argv, "show:", &x))
104115
show_dates(argv+1, x);
105116
else if (!strcmp(*argv, "parse"))

t/t0006-date.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ test_description='test date parsing and printing'
44
. ./test-lib.sh
55

66
# 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
88

99
check_relative() {
10-
t=$(($TEST_DATE_NOW - $1))
10+
t=$(($GIT_TEST_DATE_NOW - $1))
1111
echo "$t -> $2" >expect
1212
test_expect_${3:-success} "relative date ($2)" "
1313
test-tool date relative $t >actual &&

0 commit comments

Comments
 (0)