This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 09122debe nsh_timcmds: display date using set format
09122debe is described below

commit 09122debe9789aba2281e6b8a8cf01ae7d1e4b33
Author: yangjian11 <yangjia...@xiaomi.com>
AuthorDate: Fri Jan 26 11:04:33 2024 +0800

    nsh_timcmds: display date using set format
    
    Reference: 
https://www.geeksforgeeks.org/date-command-linux-examples/?ref=gcse#9-list-of-format-specifiers-used-with-date-command
    
    Signed-off-by: yangjian11 <yangjia...@xiaomi.com>
---
 nshlib/nsh_command.c |  2 +-
 nshlib/nsh_timcmds.c | 22 +++++++++++++++++-----
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c
index ea3023c3a..9da47ef3e 100644
--- a/nshlib/nsh_command.c
+++ b/nshlib/nsh_command.c
@@ -182,7 +182,7 @@ static const struct cmdmap_s g_cmdmap[] =
 
 #ifndef CONFIG_NSH_DISABLE_DATE
   CMD_MAP("date",     cmd_date,
-          1, 4, "[-s \"MMM DD HH:MM:SS YYYY\"] [-u]"),
+          1, 4, "[-s \"MMM DD HH:MM:SS YYYY\"] [-u] [+format]"),
 #endif
 
 #ifndef CONFIG_NSH_DISABLE_DD
diff --git a/nshlib/nsh_timcmds.c b/nshlib/nsh_timcmds.c
index 11ba98b5f..08d9716d4 100644
--- a/nshlib/nsh_timcmds.c
+++ b/nshlib/nsh_timcmds.c
@@ -95,7 +95,8 @@ static inline int date_month(FAR const char *abbrev)
 
 #ifndef CONFIG_NSH_DISABLE_DATE
 static inline int date_showtime(FAR struct nsh_vtbl_s *vtbl,
-                                FAR const char *name, bool utc)
+                                FAR const char *name, bool utc,
+                                FAR const char *format)
 {
   struct timespec ts;
   struct tm tm;
@@ -132,7 +133,7 @@ static inline int date_showtime(FAR struct nsh_vtbl_s *vtbl,
 
   /* Show the current time in the requested format */
 
-  ret = strftime(timbuf, MAX_TIME_STRING, "%a, %b %d %H:%M:%S %Y", &tm);
+  ret = strftime(timbuf, MAX_TIME_STRING, format, &tm);
   if (ret < 0)
     {
       nsh_error(vtbl, g_fmtcmdfailed, name, "strftime", NSH_ERRNO);
@@ -367,6 +368,7 @@ int cmd_time(FAR struct nsh_vtbl_s *vtbl, int argc, FAR 
char **argv)
 int cmd_date(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
 {
   FAR char *newtime = NULL;
+  FAR const char *format = "%a, %b %d %H:%M:%S %Y";
   FAR const char *errfmt;
   bool utc = false;
   int option;
@@ -395,11 +397,21 @@ int cmd_date(FAR struct nsh_vtbl_s *vtbl, int argc, FAR 
char **argv)
         }
     }
 
-  /* optind < argc-1 means that there are additional, unexpected arguments on
+  argc -= optind;
+
+  /* Display the time according to the format we set */
+
+  if (argv[optind] && *argv[optind] == '+')
+    {
+      format = argv[optind] + 1;
+      argc--;
+    }
+
+  /* argc > 0 means that there are additional, unexpected arguments on
    * th command-line
    */
 
-  if (optind < argc)
+  if (argc > 0)
     {
       errfmt = g_fmttoomanyargs;
       goto errout;
@@ -413,7 +425,7 @@ int cmd_date(FAR struct nsh_vtbl_s *vtbl, int argc, FAR 
char **argv)
     }
   else
     {
-      ret = date_showtime(vtbl, argv[0], utc);
+      ret = date_showtime(vtbl, argv[0], utc, format);
     }
 
   return ret;

Reply via email to