Signed-off-by: Sami Kerola <kerolasa@xxxxxx> --- login-utils/last.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/login-utils/last.c b/login-utils/last.c index b8c4b83..06708c6 100644 --- a/login-utils/last.c +++ b/login-utils/last.c @@ -49,6 +49,7 @@ #include "closestream.h" #include "carefulputc.h" #include "strutils.h" +#include "timestamps.h" #include "timeutils.h" #include "monotonic.h" @@ -315,35 +316,34 @@ static int dns_lookup(char *result, int size, int useip, int32_t *a) static int time_formatter(const struct last_control *ctl, char *dst, size_t dlen, time_t *when, int pos) { - struct tm *tm; int ret = 0; + struct ul_time t = { 0 }; + struct tm tm; + if (ctl->time_fmt == LAST_TIMEFTM_NONE) { + *dst = '\0'; + return 0; + } + t.tm = localtime_r(when, &tm); + if (pos) { + ret = snprintf(dst, dlen, "- "); + if (ret == 0) + return -1; + } + t.buf = dst + ret; + t.bufsz = dlen - ret; switch (ctl->time_fmt) { - case LAST_TIMEFTM_NONE: - *dst = 0; - break; case LAST_TIMEFTM_SHORT_CTIME: - if (pos == 0) - ret = sprintf(dst, "%s", ctime(when)); - else { - tm = localtime(when); - if (!strftime(dst, dlen, "- %H:%M", tm)) - ret = -1; - } + if (pos) + ret = ul_timestamp_hours_minutes(&t); + else + ret = ul_timestamp_ctime_full(&t); break; case LAST_TIMEFTM_FULL_CTIME: - if (pos == 0) - ret = sprintf(dst, "%s", ctime(when)); - else - ret = sprintf(dst, "- %s", ctime(when)); + ret = ul_timestamp_ctime_full(&t); break; case LAST_TIMEFTM_ISO8601: - tm = localtime(when); - if (pos == 0) { - if (!strftime(dst, dlen, "%Y-%m-%dT%H:%M:%S%z", tm)) - ret = -1; - } else if (!strftime(dst, dlen, "- %Y-%m-%dT%H:%M:%S%z", tm)) - ret = -1; + ret = ul_timestamp_iso_full(&t); break; default: abort(); -- 2.8.2 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html