Skip to content

Commit cb71f8b

Browse files
dschogitster
authored andcommitted
PRItime: introduce a new "printf format" for timestamps
Currently, Git's source code treats all timestamps as if they were unsigned longs. Therefore, it is okay to write "%lu" when printing them. There is a substantial problem with that, though: at least on Windows, time_t is *larger* than unsigned long, and hence we will want to switch away from the ill-specified `unsigned long` data type. So let's introduce the pseudo format "PRItime" (currently simply being defined to "lu") to make it easier to change the data type used for timestamps. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1aeb7e7 commit cb71f8b

File tree

15 files changed

+31
-30
lines changed

15 files changed

+31
-30
lines changed

builtin/blame.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,11 +1727,11 @@ static int emit_one_suspect_detail(struct origin *suspect, int repeat)
17271727
get_commit_info(suspect->commit, &ci, 1);
17281728
printf("author %s\n", ci.author.buf);
17291729
printf("author-mail %s\n", ci.author_mail.buf);
1730-
printf("author-time %lu\n", ci.author_time);
1730+
printf("author-time %"PRItime"\n", ci.author_time);
17311731
printf("author-tz %s\n", ci.author_tz.buf);
17321732
printf("committer %s\n", ci.committer.buf);
17331733
printf("committer-mail %s\n", ci.committer_mail.buf);
1734-
printf("committer-time %lu\n", ci.committer_time);
1734+
printf("committer-time %"PRItime"\n", ci.committer_time);
17351735
printf("committer-tz %s\n", ci.committer_tz.buf);
17361736
printf("summary %s\n", ci.summary.buf);
17371737
if (suspect->commit->object.flags & UNINTERESTING)
@@ -1844,7 +1844,7 @@ static const char *format_time(unsigned long time, const char *tz_str,
18441844

18451845
strbuf_reset(&time_buf);
18461846
if (show_raw_time) {
1847-
strbuf_addf(&time_buf, "%lu %s", time, tz_str);
1847+
strbuf_addf(&time_buf, "%"PRItime" %s", time, tz_str);
18481848
}
18491849
else {
18501850
const char *time_str;

builtin/fsck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
407407
if (timestamp && name_objects)
408408
add_decoration(fsck_walk_options.object_names,
409409
obj,
410-
xstrfmt("%s@{%ld}", refname, timestamp));
410+
xstrfmt("%s@{%"PRItime"}", refname, timestamp));
411411
obj->used = 1;
412412
mark_object_reachable(obj);
413413
} else {

builtin/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids)
910910
static void gen_message_id(struct rev_info *info, char *base)
911911
{
912912
struct strbuf buf = STRBUF_INIT;
913-
strbuf_addf(&buf, "%s.%lu.git.%s", base,
913+
strbuf_addf(&buf, "%s.%"PRItime".git.%s", base,
914914
(unsigned long) time(NULL),
915915
git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT));
916916
info->message_id = strbuf_detach(&buf, NULL);

builtin/receive-pack.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,12 +459,12 @@ static char *prepare_push_cert_nonce(const char *path, unsigned long stamp)
459459
struct strbuf buf = STRBUF_INIT;
460460
unsigned char sha1[20];
461461

462-
strbuf_addf(&buf, "%s:%lu", path, stamp);
462+
strbuf_addf(&buf, "%s:%"PRItime, path, stamp);
463463
hmac_sha1(sha1, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed));;
464464
strbuf_release(&buf);
465465

466466
/* RFC 2104 5. HMAC-SHA1-80 */
467-
strbuf_addf(&buf, "%lu-%.*s", stamp, 20, sha1_to_hex(sha1));
467+
strbuf_addf(&buf, "%"PRItime"-%.*s", stamp, 20, sha1_to_hex(sha1));
468468
return strbuf_detach(&buf, NULL);
469469
}
470470

builtin/rev-list.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static void show_commit(struct commit *commit, void *data)
8080
}
8181

8282
if (info->show_timestamp)
83-
printf("%lu ", commit->date);
83+
printf("%"PRItime" ", commit->date);
8484
if (info->header_prefix)
8585
fputs(info->header_prefix, stdout);
8686

builtin/rev-parse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ static void show_datestring(const char *flag, const char *datestr)
218218
/* date handling requires both flags and revs */
219219
if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
220220
return;
221-
buffer = xstrfmt("%s%lu", flag, approxidate(datestr));
221+
buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
222222
show(buffer);
223223
free(buffer);
224224
}

date.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,41 +100,41 @@ void show_date_relative(unsigned long time, int tz,
100100
diff = now->tv_sec - time;
101101
if (diff < 90) {
102102
strbuf_addf(timebuf,
103-
Q_("%lu second ago", "%lu seconds ago", diff), diff);
103+
Q_("%"PRItime" second ago", "%"PRItime" seconds ago", diff), diff);
104104
return;
105105
}
106106
/* Turn it into minutes */
107107
diff = (diff + 30) / 60;
108108
if (diff < 90) {
109109
strbuf_addf(timebuf,
110-
Q_("%lu minute ago", "%lu minutes ago", diff), diff);
110+
Q_("%"PRItime" minute ago", "%"PRItime" minutes ago", diff), diff);
111111
return;
112112
}
113113
/* Turn it into hours */
114114
diff = (diff + 30) / 60;
115115
if (diff < 36) {
116116
strbuf_addf(timebuf,
117-
Q_("%lu hour ago", "%lu hours ago", diff), diff);
117+
Q_("%"PRItime" hour ago", "%"PRItime" hours ago", diff), diff);
118118
return;
119119
}
120120
/* We deal with number of days from here on */
121121
diff = (diff + 12) / 24;
122122
if (diff < 14) {
123123
strbuf_addf(timebuf,
124-
Q_("%lu day ago", "%lu days ago", diff), diff);
124+
Q_("%"PRItime" day ago", "%"PRItime" days ago", diff), diff);
125125
return;
126126
}
127127
/* Say weeks for the past 10 weeks or so */
128128
if (diff < 70) {
129129
strbuf_addf(timebuf,
130-
Q_("%lu week ago", "%lu weeks ago", (diff + 3) / 7),
130+
Q_("%"PRItime" week ago", "%"PRItime" weeks ago", (diff + 3) / 7),
131131
(diff + 3) / 7);
132132
return;
133133
}
134134
/* Say months for the past 12 months or so */
135135
if (diff < 365) {
136136
strbuf_addf(timebuf,
137-
Q_("%lu month ago", "%lu months ago", (diff + 15) / 30),
137+
Q_("%"PRItime" month ago", "%"PRItime" months ago", (diff + 15) / 30),
138138
(diff + 15) / 30);
139139
return;
140140
}
@@ -145,20 +145,20 @@ void show_date_relative(unsigned long time, int tz,
145145
unsigned long months = totalmonths % 12;
146146
if (months) {
147147
struct strbuf sb = STRBUF_INIT;
148-
strbuf_addf(&sb, Q_("%lu year", "%lu years", years), years);
148+
strbuf_addf(&sb, Q_("%"PRItime" year", "%"PRItime" years", years), years);
149149
strbuf_addf(timebuf,
150150
/* TRANSLATORS: "%s" is "<n> years" */
151-
Q_("%s, %lu month ago", "%s, %lu months ago", months),
151+
Q_("%s, %"PRItime" month ago", "%s, %"PRItime" months ago", months),
152152
sb.buf, months);
153153
strbuf_release(&sb);
154154
} else
155155
strbuf_addf(timebuf,
156-
Q_("%lu year ago", "%lu years ago", years), years);
156+
Q_("%"PRItime" year ago", "%"PRItime" years ago", years), years);
157157
return;
158158
}
159159
/* Otherwise, just years. Centuries is probably overkill. */
160160
strbuf_addf(timebuf,
161-
Q_("%lu year ago", "%lu years ago", (diff + 183) / 365),
161+
Q_("%"PRItime" year ago", "%"PRItime" years ago", (diff + 183) / 365),
162162
(diff + 183) / 365);
163163
}
164164

@@ -179,7 +179,7 @@ const char *show_date(unsigned long time, int tz, const struct date_mode *mode)
179179

180180
if (mode->type == DATE_UNIX) {
181181
strbuf_reset(&timebuf);
182-
strbuf_addf(&timebuf, "%lu", time);
182+
strbuf_addf(&timebuf, "%"PRItime, time);
183183
return timebuf.buf;
184184
}
185185

@@ -188,7 +188,7 @@ const char *show_date(unsigned long time, int tz, const struct date_mode *mode)
188188

189189
if (mode->type == DATE_RAW) {
190190
strbuf_reset(&timebuf);
191-
strbuf_addf(&timebuf, "%lu %+05d", time, tz);
191+
strbuf_addf(&timebuf, "%"PRItime" %+05d", time, tz);
192192
return timebuf.buf;
193193
}
194194

@@ -643,7 +643,7 @@ static void date_string(unsigned long date, int offset, struct strbuf *buf)
643643
offset = -offset;
644644
sign = '-';
645645
}
646-
strbuf_addf(buf, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60);
646+
strbuf_addf(buf, "%"PRItime" %c%02d%02d", date, sign, offset / 60, offset % 60);
647647
}
648648

649649
/*

fetch-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ static int find_common(struct fetch_pack_args *args,
393393
packet_buf_write(&req_buf, "deepen %d", args->depth);
394394
if (args->deepen_since) {
395395
unsigned long max_age = approxidate(args->deepen_since);
396-
packet_buf_write(&req_buf, "deepen-since %lu", max_age);
396+
packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
397397
}
398398
if (args->deepen_not) {
399399
int i;

git-compat-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ extern char *gitdirname(char *);
319319
#define PRIo32 "o"
320320
#endif
321321

322+
#define PRItime "lu"
322323
#define parse_timestamp strtoul
323324

324325
#ifndef PATH_SEP

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4131,7 +4131,7 @@ static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
41314131
printf("prune %s", message);
41324132
} else {
41334133
if (cb->newlog) {
4134-
fprintf(cb->newlog, "%s %s %s %lu %+05d\t%s",
4134+
fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s",
41354135
oid_to_hex(ooid), oid_to_hex(noid),
41364136
email, timestamp, tz, message);
41374137
oidcpy(&cb->last_kept_oid, noid);

0 commit comments

Comments
 (0)