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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Apr 21, 2017 at 12:45 UTC cb71f8bdb5a105cd5b66142b887989d9addc82d0
15 files changed +31 -30
builtin/blame.c
+3 -3
@@ -1727,11 +1727,11 @@ static int emit_one_suspect_detail(struct origin *suspect, int repeat)
1727 get_commit_info(suspect->commit, &ci, 1);
1728 printf("author %s\n", ci.author.buf);
1729 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);
1731 printf("author-tz %s\n", ci.author_tz.buf);
1732 printf("committer %s\n", ci.committer.buf);
1733 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);
1735 printf("committer-tz %s\n", ci.committer_tz.buf);
1736 printf("summary %s\n", ci.summary.buf);
1737 if (suspect->commit->object.flags & UNINTERESTING)
@@ -1844,7 +1844,7 @@ static const char *format_time(unsigned long time, const char *tz_str,
1844
1845 strbuf_reset(&time_buf);
1846 if (show_raw_time) {
1847 - strbuf_addf(&time_buf, "%lu %s", time, tz_str);
1847 + strbuf_addf(&time_buf, "%"PRItime" %s", time, tz_str);
1848 }
1849 else {
1850 const char *time_str;
builtin/fsck.c
+1 -1
@@ -407,7 +407,7 @@ static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
407 if (timestamp && name_objects)
408 add_decoration(fsck_walk_options.object_names,
409 obj,
410 - xstrfmt("%s@{%ld}", refname, timestamp));
410 + xstrfmt("%s@{%"PRItime"}", refname, timestamp));
411 obj->used = 1;
412 mark_object_reachable(obj);
413 } else {
builtin/log.c
+1 -1
@@ -910,7 +910,7 @@ static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids)
910 static void gen_message_id(struct rev_info *info, char *base)
911 {
912 struct strbuf buf = STRBUF_INIT;
913 - strbuf_addf(&buf, "%s.%lu.git.%s", base,
913 + strbuf_addf(&buf, "%s.%"PRItime".git.%s", base,
914 (unsigned long) time(NULL),
915 git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT));
916 info->message_id = strbuf_detach(&buf, NULL);
builtin/receive-pack.c
+2 -2
@@ -459,12 +459,12 @@ static char *prepare_push_cert_nonce(const char *path, unsigned long stamp)
459 struct strbuf buf = STRBUF_INIT;
460 unsigned char sha1[20];
461
462 - strbuf_addf(&buf, "%s:%lu", path, stamp);
462 + strbuf_addf(&buf, "%s:%"PRItime, path, stamp);
463 hmac_sha1(sha1, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed));;
464 strbuf_release(&buf);
465
466 /* 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));
468 return strbuf_detach(&buf, NULL);
469 }
470
builtin/rev-list.c
+1 -1
@@ -80,7 +80,7 @@ static void show_commit(struct commit *commit, void *data)
80 }
81
82 if (info->show_timestamp)
83 - printf("%lu ", commit->date);
83 + printf("%"PRItime" ", commit->date);
84 if (info->header_prefix)
85 fputs(info->header_prefix, stdout);
86
builtin/rev-parse.c
+1 -1
@@ -218,7 +218,7 @@ static void show_datestring(const char *flag, const char *datestr)
218 /* date handling requires both flags and revs */
219 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
220 return;
221 - buffer = xstrfmt("%s%lu", flag, approxidate(datestr));
221 + buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
222 show(buffer);
223 free(buffer);
224 }
date.c
+13 -13
@@ -100,41 +100,41 @@ void show_date_relative(unsigned long time, int tz,
100 diff = now->tv_sec - time;
101 if (diff < 90) {
102 strbuf_addf(timebuf,
103 - Q_("%lu second ago", "%lu seconds ago", diff), diff);
103 + Q_("%"PRItime" second ago", "%"PRItime" seconds ago", diff), diff);
104 return;
105 }
106 /* Turn it into minutes */
107 diff = (diff + 30) / 60;
108 if (diff < 90) {
109 strbuf_addf(timebuf,
110 - Q_("%lu minute ago", "%lu minutes ago", diff), diff);
110 + Q_("%"PRItime" minute ago", "%"PRItime" minutes ago", diff), diff);
111 return;
112 }
113 /* Turn it into hours */
114 diff = (diff + 30) / 60;
115 if (diff < 36) {
116 strbuf_addf(timebuf,
117 - Q_("%lu hour ago", "%lu hours ago", diff), diff);
117 + Q_("%"PRItime" hour ago", "%"PRItime" hours ago", diff), diff);
118 return;
119 }
120 /* We deal with number of days from here on */
121 diff = (diff + 12) / 24;
122 if (diff < 14) {
123 strbuf_addf(timebuf,
124 - Q_("%lu day ago", "%lu days ago", diff), diff);
124 + Q_("%"PRItime" day ago", "%"PRItime" days ago", diff), diff);
125 return;
126 }
127 /* Say weeks for the past 10 weeks or so */
128 if (diff < 70) {
129 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),
131 (diff + 3) / 7);
132 return;
133 }
134 /* Say months for the past 12 months or so */
135 if (diff < 365) {
136 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),
138 (diff + 15) / 30);
139 return;
140 }
@@ -145,20 +145,20 @@ void show_date_relative(unsigned long time, int tz,
145 unsigned long months = totalmonths % 12;
146 if (months) {
147 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);
149 strbuf_addf(timebuf,
150 /* 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),
152 sb.buf, months);
153 strbuf_release(&sb);
154 } else
155 strbuf_addf(timebuf,
156 - Q_("%lu year ago", "%lu years ago", years), years);
156 + Q_("%"PRItime" year ago", "%"PRItime" years ago", years), years);
157 return;
158 }
159 /* Otherwise, just years. Centuries is probably overkill. */
160 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),
162 (diff + 183) / 365);
163 }
164
@@ -179,7 +179,7 @@ const char *show_date(unsigned long time, int tz, const struct date_mode *mode)
179
180 if (mode->type == DATE_UNIX) {
181 strbuf_reset(&timebuf);
182 - strbuf_addf(&timebuf, "%lu", time);
182 + strbuf_addf(&timebuf, "%"PRItime, time);
183 return timebuf.buf;
184 }
185
@@ -188,7 +188,7 @@ const char *show_date(unsigned long time, int tz, const struct date_mode *mode)
188
189 if (mode->type == DATE_RAW) {
190 strbuf_reset(&timebuf);
191 - strbuf_addf(&timebuf, "%lu %+05d", time, tz);
191 + strbuf_addf(&timebuf, "%"PRItime" %+05d", time, tz);
192 return timebuf.buf;
193 }
194
@@ -643,7 +643,7 @@ static void date_string(unsigned long date, int offset, struct strbuf *buf)
643 offset = -offset;
644 sign = '-';
645 }
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);
647 }
648
649 /*
fetch-pack.c
+1 -1
@@ -393,7 +393,7 @@ static int find_common(struct fetch_pack_args *args,
393 packet_buf_write(&req_buf, "deepen %d", args->depth);
394 if (args->deepen_since) {
395 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);
397 }
398 if (args->deepen_not) {
399 int i;
git-compat-util.h
+1
@@ -319,6 +319,7 @@ extern char *gitdirname(char *);
319 #define PRIo32 "o"
320 #endif
321
322 +#define PRItime "lu"
323 #define parse_timestamp strtoul
324
325 #ifndef PATH_SEP
refs/files-backend.c
+1 -1
@@ -4131,7 +4131,7 @@ static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
4131 printf("prune %s", message);
4132 } else {
4133 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",
4135 oid_to_hex(ooid), oid_to_hex(noid),
4136 email, timestamp, tz, message);
4137 oidcpy(&cb->last_kept_oid, noid);
t/helper/test-date.c
+1 -1
@@ -53,7 +53,7 @@ static void parse_dates(const char **argv, struct timeval *now)
53
54 strbuf_reset(&result);
55 parse_date(*argv, &result);
56 - if (sscanf(result.buf, "%lu %d", &t, &tz) == 2)
56 + if (sscanf(result.buf, "%"PRItime" %d", &t, &tz) == 2)
57 printf("%s -> %s\n",
58 *argv, show_date(t, tz, DATE_MODE(ISO8601)));
59 else
t/helper/test-parse-options.c
+1 -1
@@ -161,7 +161,7 @@ int cmd_main(int argc, const char **argv)
161 show(&expect, &ret, "boolean: %d", boolean);
162 show(&expect, &ret, "integer: %d", integer);
163 show(&expect, &ret, "magnitude: %lu", magnitude);
164 - show(&expect, &ret, "timestamp: %lu", timestamp);
164 + show(&expect, &ret, "timestamp: %"PRItime, timestamp);
165 show(&expect, &ret, "string: %s", string ? string : "(not set)");
166 show(&expect, &ret, "abbrev: %d", abbrev);
167 show(&expect, &ret, "verbose: %d", verbose);
t/helper/test-ref-store.c
+1 -1
@@ -141,7 +141,7 @@ static int each_reflog(struct object_id *old_oid, struct object_id *new_oid,
141 const char *committer, unsigned long timestamp,
142 int tz, const char *msg, void *cb_data)
143 {
144 - printf("%s %s %s %lu %d %s\n",
144 + printf("%s %s %s %"PRItime" %d %s\n",
145 oid_to_hex(old_oid), oid_to_hex(new_oid),
146 committer, timestamp, tz, msg);
147 return 0;
upload-pack.c
+1 -1
@@ -863,7 +863,7 @@ static void receive_needs(void)
863
864 argv_array_push(&av, "rev-list");
865 if (deepen_since)
866 - argv_array_pushf(&av, "--max-age=%lu", deepen_since);
866 + argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
867 if (deepen_not.nr) {
868 argv_array_push(&av, "--not");
869 for (i = 0; i < deepen_not.nr; i++) {
vcs-svn/fast_export.c
+2 -2
@@ -73,7 +73,7 @@ void fast_export_begin_note(uint32_t revision, const char *author,
73 static int firstnote = 1;
74 size_t loglen = strlen(log);
75 printf("commit %s\n", note_ref);
76 - printf("committer %s <%s@%s> %lu +0000\n", author, author, "local", timestamp);
76 + printf("committer %s <%s@%s> %"PRItime" +0000\n", author, author, "local", timestamp);
77 printf("data %"PRIuMAX"\n", (uintmax_t)loglen);
78 fwrite(log, loglen, 1, stdout);
79 if (firstnote) {
@@ -107,7 +107,7 @@ void fast_export_begin_commit(uint32_t revision, const char *author,
107 }
108 printf("commit %s\n", local_ref);
109 printf("mark :%"PRIu32"\n", revision);
110 - printf("committer %s <%s@%s> %lu +0000\n",
110 + printf("committer %s <%s@%s> %"PRItime" +0000\n",
111 *author ? author : "nobody",
112 *author ? author : "nobody",
113 *uuid ? uuid : "local", timestamp);