Skip to content

Commit 36d2fca

Browse files
committed
Merge branch 'ss/get-time-cleanup'
Code simplification. * ss/get-time-cleanup: test_date.c: remove reference to GIT_TEST_DATE_NOW Quit passing 'now' to date code
2 parents ed68228 + 47b27c9 commit 36d2fca

File tree

3 files changed

+24
-35
lines changed

3 files changed

+24
-35
lines changed

cache.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,8 +1554,7 @@ struct date_mode {
15541554
struct date_mode *date_mode_from_type(enum date_mode_type type);
15551555

15561556
const char *show_date(timestamp_t time, int timezone, const struct date_mode *mode);
1557-
void show_date_relative(timestamp_t time, const struct timeval *now,
1558-
struct strbuf *timebuf);
1557+
void show_date_relative(timestamp_t time, struct strbuf *timebuf);
15591558
void show_date_human(timestamp_t time, int tz, const struct timeval *now,
15601559
struct strbuf *timebuf);
15611560
int parse_date(const char *date, struct strbuf *out);
@@ -1564,7 +1563,7 @@ int parse_expiry_date(const char *date, timestamp_t *timestamp);
15641563
void datestamp(struct strbuf *out);
15651564
#define approxidate(s) approxidate_careful((s), NULL)
15661565
timestamp_t approxidate_careful(const char *, int *);
1567-
timestamp_t approxidate_relative(const char *date, const struct timeval *now);
1566+
timestamp_t approxidate_relative(const char *date);
15681567
void parse_date_format(const char *format, struct date_mode *mode);
15691568
int date_overflows(timestamp_t date);
15701569

date.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,17 @@ static void get_time(struct timeval *now)
128128
gettimeofday(now, NULL);
129129
}
130130

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)
134132
{
133+
struct timeval now;
135134
timestamp_t diff;
136-
if (now->tv_sec < time) {
135+
136+
get_time(&now);
137+
if (now.tv_sec < time) {
137138
strbuf_addstr(timebuf, _("in the future"));
138139
return;
139140
}
140-
diff = now->tv_sec - time;
141+
diff = now.tv_sec - time;
141142
if (diff < 90) {
142143
strbuf_addf(timebuf,
143144
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
240241

241242
/* Show "today" times as just relative times */
242243
if (hide.wday) {
243-
struct timeval now;
244-
get_time(&now);
245-
show_date_relative(time, &now, buf);
244+
show_date_relative(time, buf);
246245
return;
247246
}
248247

@@ -313,11 +312,8 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
313312
}
314313

315314
if (mode->type == DATE_RELATIVE) {
316-
struct timeval now;
317-
318315
strbuf_reset(&timebuf);
319-
get_time(&now);
320-
show_date_relative(time, &now, &timebuf);
316+
show_date_relative(time, &timebuf);
321317
return timebuf.buf;
322318
}
323319

@@ -1288,15 +1284,18 @@ static timestamp_t approxidate_str(const char *date,
12881284
return (timestamp_t)update_tm(&tm, &now, 0);
12891285
}
12901286

1291-
timestamp_t approxidate_relative(const char *date, const struct timeval *tv)
1287+
timestamp_t approxidate_relative(const char *date)
12921288
{
1289+
struct timeval tv;
12931290
timestamp_t timestamp;
12941291
int offset;
12951292
int errors = 0;
12961293

12971294
if (!parse_date_basic(date, &timestamp, &offset))
12981295
return timestamp;
1299-
return approxidate_str(date, tv, &errors);
1296+
1297+
get_time(&tv);
1298+
return approxidate_str(date, (const struct timeval *) &tv, &errors);
13001299
}
13011300

13021301
timestamp_t approxidate_careful(const char *date, int *error_ret)

t/helper/test-date.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ static const char *usage_msg = "\n"
1212
" test-tool date is64bit\n"
1313
" test-tool date time_t-is64bit\n";
1414

15-
static void show_relative_dates(const char **argv, struct timeval *now)
15+
static void show_relative_dates(const char **argv)
1616
{
1717
struct strbuf buf = STRBUF_INIT;
1818

1919
for (; *argv; argv++) {
2020
time_t t = atoi(*argv);
21-
show_date_relative(t, now, &buf);
21+
show_date_relative(t, &buf);
2222
printf("%s -> %s\n", *argv, buf.buf);
2323
}
2424
strbuf_release(&buf);
@@ -74,20 +74,20 @@ static void parse_dates(const char **argv)
7474
strbuf_release(&result);
7575
}
7676

77-
static void parse_approxidate(const char **argv, struct timeval *now)
77+
static void parse_approxidate(const char **argv)
7878
{
7979
for (; *argv; argv++) {
8080
timestamp_t t;
81-
t = approxidate_relative(*argv, now);
81+
t = approxidate_relative(*argv);
8282
printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(ISO8601)));
8383
}
8484
}
8585

86-
static void parse_approx_timestamp(const char **argv, struct timeval *now)
86+
static void parse_approx_timestamp(const char **argv)
8787
{
8888
for (; *argv; argv++) {
8989
timestamp_t t;
90-
t = approxidate_relative(*argv, now);
90+
t = approxidate_relative(*argv);
9191
printf("%s -> %"PRItime"\n", *argv, t);
9292
}
9393
}
@@ -103,32 +103,23 @@ static void getnanos(const char **argv)
103103

104104
int cmd__date(int argc, const char **argv)
105105
{
106-
struct timeval now;
107106
const char *x;
108107

109-
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);
116-
117108
argv++;
118109
if (!*argv)
119110
usage(usage_msg);
120111
if (!strcmp(*argv, "relative"))
121-
show_relative_dates(argv+1, &now);
112+
show_relative_dates(argv+1);
122113
else if (!strcmp(*argv, "human"))
123114
show_human_dates(argv+1);
124115
else if (skip_prefix(*argv, "show:", &x))
125116
show_dates(argv+1, x);
126117
else if (!strcmp(*argv, "parse"))
127118
parse_dates(argv+1);
128119
else if (!strcmp(*argv, "approxidate"))
129-
parse_approxidate(argv+1, &now);
120+
parse_approxidate(argv+1);
130121
else if (!strcmp(*argv, "timestamp"))
131-
parse_approx_timestamp(argv+1, &now);
122+
parse_approx_timestamp(argv+1);
132123
else if (!strcmp(*argv, "getnanos"))
133124
getnanos(argv+1);
134125
else if (!strcmp(*argv, "is64bit"))

0 commit comments

Comments
 (0)