Util-Linux package development
 help / color / mirror / Atom feed
From: yumkam@gmail.com (Yuriy M. Kaminskiy)
To: util-linux@vger.kernel.org
Subject: Re: [PATCH 03/10] lsipc, lslogins, rtcwake: replace asctime() with strftime()
Date: Sun, 13 Mar 2016 16:20:42 +0300	[thread overview]
Message-ID: <m337ruiket.fsf@gmail.com> (raw)
In-Reply-To: 1457865109-2881-4-git-send-email-kerolasa@iki.fi

[-- Attachment #1: Type: text/plain, Size: 389 bytes --]


On 03/13/16 13:31 , Sami Kerola wrote:
> POSIX.1-2008 marks asctime() and asctime_r() as obsolete, recommending the
> use of strftime(3) instead.

stftime uses locale, asctime is not. And if you want *locale-specific*
time presentation, it is "%c" --- it can be very different from your "%a
%b...", e.g. compare with attached

   gcc -o asctime asctime.c
   LC_ALL=ja_JP.UTF-8 ./asctime


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: asctime.c --]
[-- Type: text/x-csrc, Size: 312 bytes --]

#include <stdio.h>
#include <time.h>
#include <locale.h>
int main()
{
	time_t t = time(NULL);
	char buf[64];
	struct tm *tm;
	setlocale(LC_ALL, "");
	puts(asctime(tm = localtime(&t)));
	strftime(buf, sizeof(buf), "%a %b %e %T %Y", tm);
	puts(buf);
	strftime(buf, sizeof(buf), "%c", tm);
	puts(buf);
	return 0;
}

[-- Attachment #3: Type: text/plain, Size: 3735 bytes --]


> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
> ---
>  include/timeutils.h    |  3 +++
>  login-utils/lslogins.c |  5 ++---
>  sys-utils/lsipc.c      |  6 ++----
>  sys-utils/rtcwake.c    | 15 +++++++++------
>  4 files changed, 16 insertions(+), 13 deletions(-)
>
> diff --git a/include/timeutils.h b/include/timeutils.h
> index 8ed501b..427f833 100644
> --- a/include/timeutils.h
> +++ b/include/timeutils.h
> @@ -51,6 +51,9 @@ typedef uint64_t nsec_t;
>  #define FORMAT_TIMESTAMP_RELATIVE_MAX 256
>  #define FORMAT_TIMESPAN_MAX 64
>  
> +/* Original asctime(3) format ends to '\n' that we do not do here.  */
> +#define STRFTIME_ASCTIME_FORMAT	"%a %b %e %T %Y"
> +
>  int parse_timestamp(const char *t, usec_t *usec);
>  
>  #endif /* UTIL_LINUX_TIME_UTIL_H */
> diff --git a/login-utils/lslogins.c b/login-utils/lslogins.c
> index 40a1343..554a1a3 100644
> --- a/login-utils/lslogins.c
> +++ b/login-utils/lslogins.c
> @@ -56,6 +56,7 @@
>  #include "pathnames.h"
>  #include "logindefs.h"
>  #include "procutils.h"
> +#include "timeutils.h"
>  
>  /*
>   * column description
> @@ -333,9 +334,7 @@ static char *make_time(int mode, time_t time)
>  
>  	switch(mode) {
>  	case TIME_FULL:
> -		asctime_r(&tm, buf);
> -		if (*(s = buf + strlen(buf) - 1) == '\n')
> -			*s = '\0';
> +		strftime(buf, sizeof(buf), STRFTIME_ASCTIME_FORMAT, &tm);
>  		break;
>  	case TIME_SHORT:
>  		if (date_is_today(time))
> diff --git a/sys-utils/lsipc.c b/sys-utils/lsipc.c
> index 938728a..19cf5a8 100644
> --- a/sys-utils/lsipc.c
> +++ b/sys-utils/lsipc.c
> @@ -39,6 +39,7 @@
>  #include "xalloc.h"
>  #include "procutils.h"
>  #include "ipcutils.h"
> +#include "timeutils.h"
>  
>  /*
>   * time modes
> @@ -445,7 +446,6 @@ static int date_is_thisyear(time_t t)
>  
>  static char *make_time(int mode, time_t time)
>  {
> -	char *s;
>  	struct tm tm;
>  	char buf[64] = {0};
>  
> @@ -453,9 +453,7 @@ static char *make_time(int mode, time_t time)
>  
>  	switch(mode) {
>  	case TIME_FULL:
> -		asctime_r(&tm, buf);
> -		if (*(s = buf + strlen(buf) - 1) == '\n')
> -			*s = '\0';
> +		strftime(buf, sizeof(buf), STRFTIME_ASCTIME_FORMAT, &tm);
>  		break;
>  	case TIME_SHORT:
>  		if (date_is_today(time))
> diff --git a/sys-utils/rtcwake.c b/sys-utils/rtcwake.c
> index 7c748dc..32df785 100644
> --- a/sys-utils/rtcwake.c
> +++ b/sys-utils/rtcwake.c
> @@ -173,7 +173,7 @@ static int get_basetimes(struct rtcwake_control *ctl, int fd)
>  		return -1;
>  	}
>  	/* Convert rtc_time to normal arithmetic-friendly form,
> -	 * updating tm.tm_wday as used by asctime().
> +	 * updating tm.tm_wday as used by strftime().
>  	 */
>  	tm.tm_sec = rtc.tm_sec;
>  	tm.tm_min = rtc.tm_min;
> @@ -193,14 +193,17 @@ static int get_basetimes(struct rtcwake_control *ctl, int fd)
>  		/* Unless the system uses UTC, either delta or tzone
>  		 * reflects a seconds offset from UTC.  The value can
>  		 * help sort out problems like bugs in your C library. */
> +		char buf[64] = { 0 };
> +		struct tm sys_tm = { 0 };
> +
>  		printf("\tdelta   = %ld\n", ctl->sys_time - ctl->rtc_time);
>  		printf("\ttzone   = %ld\n", timezone);
>  		printf("\ttzname  = %s\n", tzname[daylight]);
> -		gmtime_r(&ctl->rtc_time, &tm);
> -		printf("\tsystime = %ld, (UTC) %s",
> -				(long) ctl->sys_time, asctime(gmtime(&ctl->sys_time)));
> -		printf("\trtctime = %ld, (UTC) %s",
> -				(long) ctl->rtc_time, asctime(&tm));
> +		gmtime_r(&ctl->sys_time, &sys_tm);
> +		strftime(buf, sizeof(buf), STRFTIME_ASCTIME_FORMAT, &sys_tm);
> +		printf("\tsystime = %ld, (UTC) %s\n", (long) ctl->sys_time, buf);
> +		strftime(buf, sizeof(buf), STRFTIME_ASCTIME_FORMAT, &tm);
> +		printf("\trtctime = %ld, (UTC) %s\n", (long) ctl->rtc_time, buf);
>  	}
>  	return 0;
>  }
> -- 
> 2.7.2

  parent reply	other threads:[~2016-03-13 13:21 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-13 10:31 [PATCH 00/10] pull: release v2.28 prep work Sami Kerola
2016-03-13 10:31 ` [PATCH 01/10] logger: fix memory leak [ASAN and valgrind] Sami Kerola
2016-03-13 10:31 ` [PATCH 02/10] getopt: fix memory leaks and integer overflows [ASAN & valgrind] Sami Kerola
2016-03-13 12:11   ` Yuriy M. Kaminskiy
2016-03-14 21:24     ` Sami Kerola
2016-03-13 10:31 ` [PATCH 03/10] lsipc, lslogins, rtcwake: replace asctime() with strftime() Sami Kerola
2016-03-13 11:28   ` Ruediger Meier
2016-03-13 13:20   ` Yuriy M. Kaminskiy [this message]
2016-03-14 12:08     ` Karel Zak
2016-03-13 10:31 ` [PATCH 04/10] isosize: stop unmeaningful printing errno message Sami Kerola
2016-03-13 10:31 ` [PATCH 05/10] setsid: fix argument count bug Sami Kerola
2016-03-13 10:31 ` [PATCH 06/10] bash-completion: fsck.cramfs, isosize: find files an argument Sami Kerola
2016-03-13 10:31 ` [PATCH 07/10] bash-completion: ipcmk: add missing completion file Sami Kerola
2016-03-13 10:31 ` [PATCH 08/10] bash-completion: lslogins: " Sami Kerola
2016-03-13 10:31 ` [PATCH 09/10] bash-completion: lsns: " Sami Kerola
2016-03-13 10:31 ` [PATCH 10/10] docs: update AUTHORS file Sami Kerola
2016-03-14 12:17 ` [PATCH 00/10] pull: release v2.28 prep work Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m337ruiket.fsf@gmail.com \
    --to=yumkam@gmail.com \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox