diff options
author | William Hubbs <w.d.hubbs@gmail.com> | 2017-05-15 18:55:35 -0500 |
---|---|---|
committer | William Hubbs <w.d.hubbs@gmail.com> | 2017-05-15 18:55:47 -0500 |
commit | a2055af90054f5125cc07d4851b1dc9d16815e7c (patch) | |
tree | a25c22e38aba2981d433af605dd7675221a0384f | |
parent | cbf96967f1b6dc72ae16203dfbbb844bd08e8b6b (diff) |
rc_status: calculate time differences in time_t and display seconds in uptime
-rw-r--r-- | src/rc/rc-status.c | 45 |
1 files 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); } } } |