This is an automated email from the ASF dual-hosted git repository.
aguettouche pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git
The following commit(s) were added to refs/heads/master by this push:
new 8e06d06 nsh/date: Support -u option
8e06d06 is described below
commit 8e06d060c24fdeddb6cfc41f8062aec011057814
Author: Xiang Xiao <[email protected]>
AuthorDate: Tue Jun 1 11:16:52 2021 +0800
nsh/date: Support -u option
user can use both UTC and local time now
Signed-off-by: Xiang Xiao <[email protected]>
---
nshlib/README.md | 2 +-
nshlib/nsh_command.c | 2 +-
nshlib/nsh_timcmds.c | 39 +++++++++++++++++++++++++++++----------
3 files changed, 31 insertions(+), 12 deletions(-)
diff --git a/nshlib/README.md b/nshlib/README.md
index 9906649..bb40f2c 100644
--- a/nshlib/README.md
+++ b/nshlib/README.md
@@ -418,7 +418,7 @@ All of the startup-behavior is contained in `rcS.template`.
The role of
Copy of the contents of the file at `<source-path>` to the location in the
file system indicated by `<path-path>`
-- `date [-s "MMM DD HH:MM:SS YYYY"]`
+- `date [-s "MMM DD HH:MM:SS YYYY"] [-u]`
Show or set the current date and time.
diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c
index 5dbe6c3..9622b39 100644
--- a/nshlib/nsh_command.c
+++ b/nshlib/nsh_command.c
@@ -155,7 +155,7 @@ static const struct cmdmap_s g_cmdmap[] =
#endif
#ifndef CONFIG_NSH_DISABLE_DATE
- { "date", cmd_date, 1, 3, "[-s \"MMM DD HH:MM:SS YYYY\"]" },
+ { "date", cmd_date, 1, 4, "[-s \"MMM DD HH:MM:SS YYYY\"] [-u]" },
#endif
#ifndef CONFIG_NSH_DISABLE_DD
diff --git a/nshlib/nsh_timcmds.c b/nshlib/nsh_timcmds.c
index f0d1b19..e6a3c3a 100644
--- a/nshlib/nsh_timcmds.c
+++ b/nshlib/nsh_timcmds.c
@@ -1,5 +1,5 @@
/****************************************************************************
- * apps/nshlib/dbg_timcmds.c
+ * apps/nshlib/nsh_timcmds.c
*
* Copyright (C) 2011-2012, 2014, 2019 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
@@ -110,7 +110,7 @@ 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)
+ FAR const char *name, bool utc)
{
static const char format[] = "%a, %b %d %H:%M:%S %Y";
struct timespec ts;
@@ -129,10 +129,21 @@ static inline int date_showtime(FAR struct nsh_vtbl_s
*vtbl,
/* Break the current time up into the format needed by strftime */
- if (gmtime_r((FAR const time_t *)&ts.tv_sec, &tm) == NULL)
+ if (utc)
{
- nsh_error(vtbl, g_fmtcmdfailed, name, "gmtime_r", NSH_ERRNO);
- return ERROR;
+ if (gmtime_r((FAR const time_t *)&ts.tv_sec, &tm) == NULL)
+ {
+ nsh_error(vtbl, g_fmtcmdfailed, name, "gmtime_r", NSH_ERRNO);
+ return ERROR;
+ }
+ }
+ else
+ {
+ if (localtime_r((FAR const time_t *)&ts.tv_sec, &tm) == NULL)
+ {
+ nsh_error(vtbl, g_fmtcmdfailed, name, "localtime_r", NSH_ERRNO);
+ return ERROR;
+ }
}
/* Show the current time in the requested format */
@@ -155,7 +166,8 @@ static inline int date_showtime(FAR struct nsh_vtbl_s *vtbl,
#ifndef CONFIG_NSH_DISABLE_DATE
static inline int date_settime(FAR struct nsh_vtbl_s *vtbl,
- FAR const char *name, FAR char *newtime)
+ FAR const char *name, bool utc,
+ FAR char *newtime)
{
struct timespec ts;
struct tm tm;
@@ -266,7 +278,7 @@ static inline int date_settime(FAR struct nsh_vtbl_s *vtbl,
/* Convert this to the right form, then set the timer */
- ts.tv_sec = mktime(&tm);
+ ts.tv_sec = utc ? timegm(&tm): mktime(&tm);
ts.tv_nsec = 0;
ret = clock_settime(CLOCK_REALTIME, &ts);
@@ -376,12 +388,13 @@ int cmd_date(FAR struct nsh_vtbl_s *vtbl, int argc, char
**argv)
{
FAR char *newtime = NULL;
FAR const char *errfmt;
+ bool utc = false;
int option;
int ret;
/* Get the date options: date [-s time] [+FORMAT] */
- while ((option = getopt(argc, argv, "s:")) != ERROR)
+ while ((option = getopt(argc, argv, "s:u")) != ERROR)
{
if (option == 's')
{
@@ -389,6 +402,12 @@ int cmd_date(FAR struct nsh_vtbl_s *vtbl, int argc, char
**argv)
newtime = optarg;
}
+ else if (option == 'u')
+ {
+ /* We will use the UTC time */
+
+ utc = true;
+ }
else /* option = '?' */
{
errfmt = g_fmtarginvalid;
@@ -410,11 +429,11 @@ int cmd_date(FAR struct nsh_vtbl_s *vtbl, int argc, char
**argv)
if (newtime)
{
- ret = date_settime(vtbl, argv[0], newtime);
+ ret = date_settime(vtbl, argv[0], utc, newtime);
}
else
{
- ret = date_showtime(vtbl, argv[0]);
+ ret = date_showtime(vtbl, argv[0], utc);
}
return ret;