OSDN Git Service

Show the uptime in days, hours and minutes
authorwifiextender <router@archlinux.info>
Fri, 12 Aug 2016 20:48:51 +0000 (22:48 +0200)
committerwifiextender <router@archlinux.info>
Fri, 12 Aug 2016 20:48:51 +0000 (22:48 +0200)
README.md
src/functions.c
src/options.c

index a898a54..9c6e600 100644 (file)
--- a/README.md
+++ b/README.md
@@ -53,7 +53,7 @@ The order of supplied options will dictate how, where and what system informatio
 | -R           | --kernver   | The kernel version                                                 |
 | -u           | --kernarch  | The machine architecture                                           |
 | -k           | --kernel    | Combined kernel name and version                                   |
-| -U           | --uptime    | The system uptime in minutes                                       |
+| -U           | --uptime    | The system uptime                                                  |
 | -w           | --loadavg   | The system average load for past 1, 5 and 15 minutes               |
 | -v           | --voltage   | The system voltage                                                 |
 | -f           | --fans      | All system fans and their speed in RPM                             |
index 2941eea..bd473f3 100644 (file)
@@ -261,11 +261,27 @@ get_uptime(char *str1) {
   struct timespec tc = {0};
 #pragma GCC diagnostic pop
 
+  uintmax_t now = 0;
   if (-1 == (clock_gettime(CLOCK_BOOTTIME, &tc))) {
     FUNC_FAILED("clock_gettime()");
   }
-  FILL_ARR(str1, FMT_UINT " %s",
-    (uintmax_t)(tc.tv_sec / 60), "min");
+
+  now = (uintmax_t)tc.tv_sec;
+  if ((0 != (now / 86400))) { /* days */
+    FILL_ARR(str1, FMT_UINT "d " FMT_UINT "h " FMT_UINT "m",
+      (now / 86400),
+      ((now / 3600) % 24),
+      ((now / 60) % 60));
+    return;
+  }
+  if (59 < (now / 60)) { /* hours */
+    FILL_ARR(str1, FMT_UINT "h " FMT_UINT "m",
+      ((now / 3600) % 24),
+      ((now / 60) % 60));
+    return;
+  }
+
+  FILL_ARR(str1, FMT_UINT "m", ((now / 60) % 60));
 }
 
 
index 76064c2..f70c40e 100644 (file)
@@ -56,7 +56,7 @@ static const struct argp_option options[] = {
   { .name = "kernver",      .key = 'R',                .doc = "The kernel version."                                      },
   { .name = "kernarch",     .key = 'u',                .doc = "The machine architecture."                                },
   { .name = "kernel",       .key = 'k',                .doc = "Combined kernel name and version."                        },
-  { .name = "uptime",       .key = 'U',                .doc = "The system uptime in minutes"                             },
+  { .name = "uptime",       .key = 'U',                .doc = "The system uptime"                                        },
   { .name = "loadavg",      .key = 'w',                .doc = "The system average load for past 1, 5 and 15 minutes"     },
   { .name = "voltage",      .key = 'v',                .doc = "The system voltage"                                       },
   { .name = "fans",         .key = 'f',                .doc = "All system fans and their speed in RPM."                  },