From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id DDC67139694 for ; Tue, 30 May 2017 17:49:41 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 9A47DE0D4E; Tue, 30 May 2017 17:49:40 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 73FE6E0D4C for ; Tue, 30 May 2017 17:49:40 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 3D42034171A for ; Tue, 30 May 2017 17:49:39 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id D3FA07466 for ; Tue, 30 May 2017 17:49:37 +0000 (UTC) From: "William Hubbs" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "William Hubbs" Message-ID: <1494893098.1967cd402caf6e229263f15aef40c6c4d8f60ce3.williamh@OpenRC> Subject: [gentoo-commits] proj/openrc:0.26.x commit in: src/rc/ X-VCS-Repository: proj/openrc X-VCS-Files: src/rc/rc-status.c X-VCS-Directories: src/rc/ X-VCS-Committer: williamh X-VCS-Committer-Name: William Hubbs X-VCS-Revision: 1967cd402caf6e229263f15aef40c6c4d8f60ce3 X-VCS-Branch: 0.26.x Date: Tue, 30 May 2017 17:49:37 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 7cad5da0-f7d8-4402-862a-a6872f2a47a2 X-Archives-Hash: 8a804fdcc236285752f1e43f604ab699 commit: 1967cd402caf6e229263f15aef40c6c4d8f60ce3 Author: William Hubbs gmail com> AuthorDate: Mon May 15 23:55:35 2017 +0000 Commit: William Hubbs gentoo org> CommitDate: Tue May 16 00:04:58 2017 +0000 URL: https://gitweb.gentoo.org/proj/openrc.git/commit/?id=1967cd40 rc_status: calculate time differences in time_t and display seconds in uptime src/rc/rc-status.c | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/rc/rc-status.c b/src/rc/rc-status.c index a314bb1d..9a094d47 100644 --- a/src/rc/rc-status.c +++ b/src/rc/rc-status.c @@ -83,11 +83,11 @@ static void get_uptime(const char *service, char *uptime, int uptime_size) time_t now; char *start_time_string; time_t start_time; - double time_diff; - double diff_tmp; - double diff_days; - double diff_hours; - double diff_mins; + time_t time_diff; + time_t diff_days = (time_t) 0; + time_t diff_hours = (time_t) 0; + time_t diff_mins = (time_t) 0; + time_t diff_secs = (time_t) 0; uptime[0] = '\0'; if (state & RC_SERVICE_STARTED) { @@ -96,26 +96,29 @@ static void get_uptime(const char *service, char *uptime, int uptime_size) if (start_count && start_time_string) { start_time = to_time_t(start_time_string); now = time(NULL); - time_diff = difftime(now, start_time); - diff_tmp = time_diff; - if (diff_tmp > 86400.0) { - diff_days = diff_tmp / 86400.0; - diff_tmp -= diff_days * 86400.0; + time_diff = (time_t) difftime(now, start_time); + diff_secs = time_diff; + if (diff_secs > (time_t) 86400) { + diff_days = diff_secs / (time_t) 86400; + diff_secs %= diff_days * (time_t) 86400; } - if (diff_tmp > 3600.0) { - diff_hours = diff_tmp / 3600.0; - diff_tmp -= diff_hours * 3600.0; + if (diff_secs > (time_t) 3600) { + diff_hours = diff_secs / (time_t) 3600; + diff_secs %= diff_hours * (time_t) 3600; } - if (diff_tmp > 60.0) { - diff_mins = diff_tmp / 60.0; - diff_tmp -= diff_mins * 60.0; + if (diff_secs > (time_t) 60) { + diff_mins = diff_secs / (time_t) 60; + diff_secs %= diff_mins * (time_t) 60; } - if ((int) diff_days > 0) - snprintf(uptime, uptime_size, "%.0f days %02.0f:%02.0f (%s)", - diff_days, diff_hours, diff_mins, start_count); + if (diff_days > 0) + snprintf(uptime, uptime_size, + "%ld day(s) %02ld:%02ld:%02ld (%s)", + diff_days, diff_hours, diff_mins, diff_secs, + start_count); else - snprintf(uptime, uptime_size, "%02.0f:%02.0f (%s)", - diff_hours, diff_mins, start_count); + snprintf(uptime, uptime_size, + "%02ld:%02ld:%02ld (%s)", + diff_hours, diff_mins, diff_secs, start_count); } } }