"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Stefan Sperling <stsp@stsp.name>
Subject:
Re: tidy get_datestr()
To:
Omar Polo <op@omarpolo.com>
Cc:
gameoftrees@openbsd.org
Date:
Tue, 9 Apr 2024 15:51:10 +0200

Download raw body.

Thread
On Tue, Apr 09, 2024 at 03:32:01PM +0200, Omar Polo wrote:
> wrote this diff while debugging the date formatting issue just fixed,
> when i realized that we could save a few lines by using strftime().
> since I already wrote this diff for repo_write.c, here's also the rest
> of the get_datestr() copies converted.  (well, except got-notify-email)
> 
> the output is exactly the same as before.

asctime_r() is locale independent, but strftime() may have locale-specific
behaviour.

This change might affect the output of -portable running in non-English
locales, resulting in partially translated display.
Given that we don't support i18n I think it makes more sense to keep
everything in English.
 
> diff /home/op/w/got
> commit - 166674b8808a9a16a3cff59d166410cd37833014
> path + /home/op/w/got
> blob - 38191d6109e5f5caf08654969f4f3af3d9fb2c70
> file + cvg/cvg.c
> --- cvg/cvg.c
> +++ cvg/cvg.c
> @@ -3345,21 +3345,16 @@ done:
>  }
>  
>  static char *
> -get_datestr(time_t *time, char *datebuf)
> +get_datestr(time_t *time, char *datebuf, size_t len)
>  {
>  	struct tm mytm, *tm;
> -	char *p, *s;
>  
>  	tm = gmtime_r(time, &mytm);
>  	if (tm == NULL)
>  		return NULL;
> -	s = asctime_r(tm, datebuf);
> -	if (s == NULL)
> +	if (strftime(datebuf, len, "%a %b %e %X %Y UTC", tm) == 0)
>  		return NULL;
> -	p = strchr(s, '\n');
> -	if (p)
> -		*p = '\0';
> -	return s;
> +	return datebuf;
>  }
>  
>  static const struct got_error *
> @@ -3644,7 +3639,7 @@ print_commit(struct got_commit_object *commit, struct 
>  	const struct got_error *err = NULL;
>  	FILE *f = NULL;
>  	char *id_str, *datestr, *logmsg0, *logmsg, *line;
> -	char datebuf[26];
> +	char datebuf[30];
>  	time_t committer_time;
>  	const char *author, *committer;
>  	char *refs_str = NULL;
> @@ -3681,9 +3676,9 @@ print_commit(struct got_commit_object *commit, struct 
>  	if (strcmp(author, committer) != 0)
>  		printf("via: %s\n", committer);
>  	committer_time = got_object_commit_get_committer_time(commit);
> -	datestr = get_datestr(&committer_time, datebuf);
> +	datestr = get_datestr(&committer_time, datebuf, sizeof(datebuf));
>  	if (datestr)
> -		printf("date: %s UTC\n", datestr);
> +		printf("date: %s\n", datestr);
>  	if (got_object_commit_get_nparents(commit) > 1) {
>  		const struct got_object_id_queue *parent_ids;
>  		struct got_object_qid *qid;
> @@ -5901,7 +5896,7 @@ list_tags(struct got_repository *repo, const char *tag
>  	TAILQ_FOREACH(re, &refs, entry) {
>  		const char *refname;
>  		char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
> -		char datebuf[26];
> +		char datebuf[30];
>  		const char *tagger, *ssh_sig = NULL;
>  		char *sig_msg = NULL;
>  		time_t tagger_time;
> @@ -5968,9 +5963,9 @@ list_tags(struct got_repository *repo, const char *tag
>  		printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
>  		free(refstr);
>  		printf("from: %s\n", tagger);
> -		datestr = get_datestr(&tagger_time, datebuf);
> +		datestr = get_datestr(&tagger_time, datebuf, sizeof(datebuf));
>  		if (datestr)
> -			printf("date: %s UTC\n", datestr);
> +			printf("date: %s\n", datestr);
>  		if (commit)
>  			printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
>  		else {
> blob - 4486b08390756e709d18c25cbfc370d75101ecc2
> file + got/got.c
> --- got/got.c
> +++ got/got.c
> @@ -4036,21 +4036,16 @@ done:
>  }
>  
>  static char *
> -get_datestr(time_t *time, char *datebuf)
> +get_datestr(time_t *time, char *datebuf, size_t len)
>  {
>  	struct tm mytm, *tm;
> -	char *p, *s;
>  
>  	tm = gmtime_r(time, &mytm);
>  	if (tm == NULL)
>  		return NULL;
> -	s = asctime_r(tm, datebuf);
> -	if (s == NULL)
> +	if (strftime(datebuf, len, "%a %b %e %X %Y UTC", tm) == 0)
>  		return NULL;
> -	p = strchr(s, '\n');
> -	if (p)
> -		*p = '\0';
> -	return s;
> +	return datebuf;
>  }
>  
>  static const struct got_error *
> @@ -4335,7 +4330,7 @@ print_commit(struct got_commit_object *commit, struct 
>  	const struct got_error *err = NULL;
>  	FILE *f = NULL;
>  	char *id_str, *datestr, *logmsg0, *logmsg, *line;
> -	char datebuf[26];
> +	char datebuf[30];
>  	time_t committer_time;
>  	const char *author, *committer;
>  	char *refs_str = NULL;
> @@ -4372,9 +4367,9 @@ print_commit(struct got_commit_object *commit, struct 
>  	if (strcmp(author, committer) != 0)
>  		printf("via: %s\n", committer);
>  	committer_time = got_object_commit_get_committer_time(commit);
> -	datestr = get_datestr(&committer_time, datebuf);
> +	datestr = get_datestr(&committer_time, datebuf, sizeof(datebuf));
>  	if (datestr)
> -		printf("date: %s UTC\n", datestr);
> +		printf("date: %s\n", datestr);
>  	if (got_object_commit_get_nparents(commit) > 1) {
>  		const struct got_object_id_queue *parent_ids;
>  		struct got_object_qid *qid;
> @@ -7460,7 +7455,7 @@ list_tags(struct got_repository *repo, const char *tag
>  	TAILQ_FOREACH(re, &refs, entry) {
>  		const char *refname;
>  		char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
> -		char datebuf[26];
> +		char datebuf[30];
>  		const char *tagger, *ssh_sig = NULL;
>  		char *sig_msg = NULL;
>  		time_t tagger_time;
> @@ -7527,9 +7522,9 @@ list_tags(struct got_repository *repo, const char *tag
>  		printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
>  		free(refstr);
>  		printf("from: %s\n", tagger);
> -		datestr = get_datestr(&tagger_time, datebuf);
> +		datestr = get_datestr(&tagger_time, datebuf, sizeof(datebuf));
>  		if (datestr)
> -			printf("date: %s UTC\n", datestr);
> +			printf("date: %s\n", datestr);
>  		if (commit)
>  			printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
>  		else {
> blob - 35a2c6bb85273ba11c1a12f58fd24d93c3e3843c
> file + gotd/libexec/got-notify-email/got-notify-email.c
> --- gotd/libexec/got-notify-email/got-notify-email.c
> +++ gotd/libexec/got-notify-email/got-notify-email.c
> @@ -193,21 +193,16 @@ send_smtp_msg(int s, const char *fmt, ...)
>  }
>  
>  static char *
> -get_datestr(time_t *time, char *datebuf)
> +get_datestr(time_t *time, char *datebuf, size_t len)
>  {
>  	struct tm mytm, *tm;
> -	char *p, *s;
>  
>  	tm = gmtime_r(time, &mytm);
>  	if (tm == NULL)
>  		return NULL;
> -	s = asctime_r(tm, datebuf);
> -	if (s == NULL)
> +	if (strftime(datebuf, len, "%a %b %e %X %Y +0000 (UTC)", tm) == 0)
>  		return NULL;
> -	p = strchr(s, '\n');
> -	if (p)
> -		*p = '\0';
> -	return s;
> +	return datebuf;
>  }
>  
>  static void
> @@ -220,11 +215,11 @@ send_email(int s, const char *myfromaddr, const char *
>  	size_t linesize = 0;
>  	ssize_t linelen;
>  	time_t now;
> -	char datebuf[26];
> +	char datebuf[38];
>  	char *datestr;
>  
>  	now = time(NULL);
> -	datestr = get_datestr(&now, datebuf);
> +	datestr = get_datestr(&now, datebuf, sizeof(datebuf));
>  
>  	if (read_smtp_code(s, "220"))
>  		errx(1, "unexpected SMTP greeting received");
> @@ -257,7 +252,7 @@ send_email(int s, const char *myfromaddr, const char *
>  		if (send_smtp_msg(s, "Reply-To: %s\r\n", replytoaddr))
>  			errx(1, "could not send Reply-To header");
>  	}
> -	if (send_smtp_msg(s, "Date: %s +0000 (UTC)\r\n", datestr))
> +	if (send_smtp_msg(s, "Date: %s\r\n", datestr))
>  		errx(1, "could not send Date header");
>  
>  	if (send_smtp_msg(s, "Subject: %s\r\n", subject))
> blob - 787504ad4f6f0817c648d3f24b5d75d65da66010
> file + gotd/repo_write.c
> --- gotd/repo_write.c
> +++ gotd/repo_write.c
> @@ -1630,21 +1630,16 @@ receive_pack_idx(struct imsg *imsg, struct gotd_imsgev
>  }
>  
>  static char *
> -get_datestr(time_t *time, char *datebuf)
> +get_datestr(time_t *time, char *datebuf, size_t len)
>  {
>  	struct tm mytm, *tm;
> -	char *p, *s;
>  
>  	tm = gmtime_r(time, &mytm);
>  	if (tm == NULL)
>  		return NULL;
> -	s = asctime_r(tm, datebuf);
> -	if (s == NULL)
> +	if (strftime(datebuf, len, "%a %b %e %X %Y UTC", tm) == 0)
>  		return NULL;
> -	p = strchr(s, '\n');
> -	if (p)
> -		*p = '\0';
> -	return s;
> +	return datebuf;
>  }
>  
>  static const struct got_error *
> @@ -1776,7 +1771,7 @@ print_commit(struct got_commit_object *commit, struct 
>  {
>  	const struct got_error *err = NULL;
>  	char *id_str, *datestr, *logmsg0, *logmsg, *line;
> -	char datebuf[26];
> +	char datebuf[30];
>  	time_t committer_time;
>  	const char *author, *committer;
>  
> @@ -1793,9 +1788,9 @@ print_commit(struct got_commit_object *commit, struct 
>  	if (strcmp(author, committer) != 0)
>  		dprintf(fd, "via: %s\n", committer);
>  	committer_time = got_object_commit_get_committer_time(commit);
> -	datestr = get_datestr(&committer_time, datebuf);
> +	datestr = get_datestr(&committer_time, datebuf, sizeof(datebuf));
>  	if (datestr)
> -		dprintf(fd, "date: %s UTC\n", datestr);
> +		dprintf(fd, "date: %s\n", datestr);
>  	if (got_object_commit_get_nparents(commit) > 1) {
>  		const struct got_object_id_queue *parent_ids;
>  		struct got_object_qid *qid;
> @@ -2005,7 +2000,7 @@ print_tag(struct got_object_id *id,
>  	struct got_tag_object *tag = NULL;
>  	const char *tagger = NULL;
>  	char *id_str = NULL, *tagmsg0 = NULL, *tagmsg, *line, *datestr;
> -	char datebuf[26];
> +	char datebuf[30];
>  	time_t tagger_time;
>  
>  	err = got_object_open_as_tag(&tag, repo, id);
> @@ -2021,9 +2016,9 @@ print_tag(struct got_object_id *id,
>  
>  	dprintf(fd, "tag %s\n", refname);
>  	dprintf(fd, "from: %s\n", tagger);
> -	datestr = get_datestr(&tagger_time, datebuf);
> +	datestr = get_datestr(&tagger_time, datebuf, sizeof(datebuf));
>  	if (datestr)
> -		dprintf(fd, "date: %s UTC\n", datestr);
> +		dprintf(fd, "date: %s\n", datestr);
>  
>  	switch (got_object_tag_get_object_type(tag)) {
>  	case GOT_OBJ_TYPE_BLOB:
> blob - d6f39ddd87489a5be6dceeaae758836ca25385f3
> file + gotwebd/pages.tmpl
> --- gotwebd/pages.tmpl
> +++ gotwebd/pages.tmpl
> @@ -89,14 +89,15 @@ nextsep(char *s, char **t)
>  	if (strftime(rfc3339, sizeof(rfc3339), "%FT%TZ", &tm) == 0)
>  		return -1;
>  
> -	if (fmt != TM_DIFF && asctime_r(&tm, datebuf) == NULL)
> +	if (fmt != TM_DIFF &&
> +	    strftime(datebuf, sizeof(datebuf), "%a %b %e %X %Y UTC" &tm) == 0)
>  		return -1;
>  !}
>  <time datetime="{{ rfc3339 }}">
>    {{ if fmt == TM_DIFF }}
>      {{ render gotweb_render_age(tp, t) }}
>    {{ else }}
> -    {{ datebuf }} {{ " UTC" }}
> +    {{ datebuf }}
>    {{ end }}
>  </time>
>  {{ end }}
> blob - 49be7dc0e431407602c4dba0ac7030028d5a567e
> file + tog/tog.c
> --- tog/tog.c
> +++ tog/tog.c
> @@ -5032,21 +5032,16 @@ draw_file(struct tog_view *view, const char *header)
>  }
>  
>  static char *
> -get_datestr(time_t *time, char *datebuf)
> +get_datestr(time_t *time, char *datebuf, size_t len)
>  {
>  	struct tm mytm, *tm;
> -	char *p, *s;
>  
>  	tm = gmtime_r(time, &mytm);
>  	if (tm == NULL)
>  		return NULL;
> -	s = asctime_r(tm, datebuf);
> -	if (s == NULL)
> +	if (strftime(datebuf, len, "%a %b %e %X %Y UTC", tm) == 0)
>  		return NULL;
> -	p = strchr(s, '\n');
> -	if (p)
> -		*p = '\0';
> -	return s;
> +	return datebuf;
>  }
>  
>  static const struct got_error *
> @@ -5128,7 +5123,7 @@ write_commit_info(struct got_diff_line **lines, size_t
>      struct got_diffstat_cb_arg *dsa, FILE *outfile)
>  {
>  	const struct got_error *err = NULL;
> -	char datebuf[26], *datestr;
> +	char datebuf[30], *datestr;
>  	struct got_commit_object *commit;
>  	char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
>  	time_t committer_time;
> @@ -5193,9 +5188,9 @@ write_commit_info(struct got_diff_line **lines, size_t
>  			goto done;
>  	}
>  	committer_time = got_object_commit_get_committer_time(commit);
> -	datestr = get_datestr(&committer_time, datebuf);
> +	datestr = get_datestr(&committer_time, datebuf, sizeof(datebuf));
>  	if (datestr) {
> -		n = fprintf(outfile, "date: %s UTC\n", datestr);
> +		n = fprintf(outfile, "date: %s\n", datestr);
>  		if (n < 0) {
>  			err = got_error_from_errno("fprintf");
>  			goto done;
> 
>