You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/09/20 15:49:31 UTC

[GitHub] [incubator-nuttx-apps] Junbo-Zheng commented on a diff in pull request #1319: apps/nshlib: add uptime command support

Junbo-Zheng commented on code in PR #1319:
URL: https://github.com/apache/incubator-nuttx-apps/pull/1319#discussion_r975544215


##########
nshlib/nsh_proccmds.c:
##########
@@ -758,3 +769,114 @@ int cmd_usleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
   return OK;
 }
 #endif
+
+/****************************************************************************
+ * Name: cmd_uptime
+ ****************************************************************************/
+
+#ifndef CONFIG_NSH_DISABLE_UPTIME
+int cmd_uptime(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
+{
+  uint32_t updays;
+  uint32_t uphours;
+  uint32_t upminutes;
+
+  time_t current_time_seconds;
+  FAR struct tm *current_time;
+
+  struct sysinfo sys_info;
+
+  time_t uptime = 0;
+
+  bool pretty_format_opt = false;
+  bool system_load_opt = false;
+
+  if (argc < 2)
+    {
+      system_load_opt = true;
+
+      current_time_seconds = time(NULL);
+      current_time = localtime(&current_time_seconds);
+      nsh_output(vtbl, "%02u:%02u:%02u ", current_time->tm_hour,
+                  current_time->tm_min, current_time->tm_sec);

Review Comment:
   done.



##########
nshlib/nsh_proccmds.c:
##########
@@ -758,3 +769,114 @@ int cmd_usleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
   return OK;
 }
 #endif
+
+/****************************************************************************
+ * Name: cmd_uptime
+ ****************************************************************************/
+
+#ifndef CONFIG_NSH_DISABLE_UPTIME
+int cmd_uptime(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
+{
+  uint32_t updays;
+  uint32_t uphours;
+  uint32_t upminutes;
+
+  time_t current_time_seconds;
+  FAR struct tm *current_time;
+
+  struct sysinfo sys_info;
+
+  time_t uptime = 0;
+
+  bool pretty_format_opt = false;
+  bool system_load_opt = false;
+
+  if (argc < 2)
+    {
+      system_load_opt = true;
+
+      current_time_seconds = time(NULL);
+      current_time = localtime(&current_time_seconds);
+      nsh_output(vtbl, "%02u:%02u:%02u ", current_time->tm_hour,
+                  current_time->tm_min, current_time->tm_sec);
+    }
+  else if (strcmp(argv[1], "-p") == 0)
+    {
+      pretty_format_opt = true;
+    }
+  else if (strcmp(argv[1], "-s") == 0)
+    {
+      sysinfo(&sys_info);
+      time(&current_time_seconds);
+      current_time_seconds -= sys_info.uptime;
+      current_time = localtime(&current_time_seconds);
+      nsh_output(vtbl, "%04u-%02u-%02u %02u:%02u:%02u\n",
+                 current_time->tm_year + 1900, current_time->tm_mon + 1,
+                 current_time->tm_mday, current_time->tm_hour,
+                 current_time->tm_min, current_time->tm_sec);
+      return OK;
+    }
+  else
+    {
+      if (strcmp(argv[1], "-h") != 0)
+        {
+          nsh_output(vtbl, "uptime: invalid option -- %s\n", argv[1]);
+        }
+
+      nsh_output(vtbl, "Usage:\n");
+      nsh_output(vtbl, "uptime [options]\n");
+
+      nsh_output(vtbl, "Options:\n");
+      nsh_output(vtbl, "-p, show uptime in pretty format\n");
+      nsh_output(vtbl, "-h, display this help and exit\n");
+      nsh_output(vtbl, "-s, system up since\n");
+
+      return ERROR;
+    }
+
+  sysinfo(&sys_info);
+  uptime = sys_info.uptime;
+
+  updays = uptime / 86400;
+  uptime -= updays * 86400;
+  uphours = uptime / 3600;
+  uptime -= uphours * 3600;
+  upminutes = uptime / 60;
+
+  nsh_output(vtbl, "up ");
+
+  if (updays)
+    {
+      nsh_output(vtbl, "%" PRIu32 " day%s, ", updays,
+                 (updays > 1) ? "s" : "");
+    }
+
+  if (pretty_format_opt)
+    {
+      if (uphours)
+        {
+          nsh_output(vtbl, "%" PRIu32 " hour%s, ", uphours,
+                     (uphours > 1) ? "s" : "");
+        }
+
+      nsh_output(vtbl, "%" PRIu32 " minute%s", upminutes,
+                   (upminutes > 1) ? "s" : "");

Review Comment:
   done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org