10d163575Sopenharmony_ci/*
20d163575Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
30d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
40d163575Sopenharmony_ci *
50d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
60d163575Sopenharmony_ci * are permitted provided that the following conditions are met:
70d163575Sopenharmony_ci *
80d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
90d163575Sopenharmony_ci *    conditions and the following disclaimer.
100d163575Sopenharmony_ci *
110d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
120d163575Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
130d163575Sopenharmony_ci *    provided with the distribution.
140d163575Sopenharmony_ci *
150d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
160d163575Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
170d163575Sopenharmony_ci *    permission.
180d163575Sopenharmony_ci *
190d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
200d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
210d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
230d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
240d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
250d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
260d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
270d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
280d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
290d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
300d163575Sopenharmony_ci */
310d163575Sopenharmony_ci
320d163575Sopenharmony_ci#include "shcmd.h"
330d163575Sopenharmony_ci#include "shell.h"
340d163575Sopenharmony_ci#include "stdlib.h"
350d163575Sopenharmony_ci#include "time.h"
360d163575Sopenharmony_ci#include "los_typedef.h"
370d163575Sopenharmony_ci#include "sys/stat.h"
380d163575Sopenharmony_ci#include "securec.h"
390d163575Sopenharmony_ci
400d163575Sopenharmony_ci#if defined(__LP64__)
410d163575Sopenharmony_ci#define  timeval64      timeval
420d163575Sopenharmony_ci#define  settimeofday64 settimeofday
430d163575Sopenharmony_ci#define  gettimeofday64 gettimeofday
440d163575Sopenharmony_ci#endif
450d163575Sopenharmony_ci
460d163575Sopenharmony_ci#define  localtime64    localtime
470d163575Sopenharmony_ci#define  ctime64        ctime
480d163575Sopenharmony_ci#define  mktime64       mktime
490d163575Sopenharmony_ci
500d163575Sopenharmony_ci#define  DATE_ERR_INFO      1
510d163575Sopenharmony_ci#define  DATE_HELP_INFO     0
520d163575Sopenharmony_ci#define  DATE_ERR           (-1)
530d163575Sopenharmony_ci#define  DATE_OK            0
540d163575Sopenharmony_ci#define  DATE_BASE_YEAR     1900
550d163575Sopenharmony_ci#define  LEAPYEAR(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
560d163575Sopenharmony_ci
570d163575Sopenharmony_ciSTATIC const INT32 g_monLengths[2][12] = { /* 2: 2 Column,Contains leap year; 12: 12 months */
580d163575Sopenharmony_ci    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
590d163575Sopenharmony_ci    {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
600d163575Sopenharmony_ci};
610d163575Sopenharmony_ci
620d163575Sopenharmony_ciSTATIC VOID OsCopyTm(struct tm *destTm, const struct tm *srcTm)
630d163575Sopenharmony_ci{
640d163575Sopenharmony_ci    if (srcTm == NULL) {
650d163575Sopenharmony_ci        (VOID)memset_s(destTm, sizeof(struct tm), 0, sizeof(struct tm));
660d163575Sopenharmony_ci    } else {
670d163575Sopenharmony_ci        destTm->tm_sec = srcTm->tm_sec;
680d163575Sopenharmony_ci        destTm->tm_min = srcTm->tm_min;
690d163575Sopenharmony_ci        destTm->tm_hour = srcTm->tm_hour;
700d163575Sopenharmony_ci        destTm->tm_mday = srcTm->tm_mday;
710d163575Sopenharmony_ci        destTm->tm_mon = srcTm->tm_mon;
720d163575Sopenharmony_ci        destTm->tm_year = srcTm->tm_year;
730d163575Sopenharmony_ci        destTm->tm_wday = srcTm->tm_wday;
740d163575Sopenharmony_ci        destTm->tm_yday = srcTm->tm_yday;
750d163575Sopenharmony_ci        destTm->tm_isdst = srcTm->tm_isdst;
760d163575Sopenharmony_ci        destTm->tm_gmtoff = srcTm->tm_gmtoff;
770d163575Sopenharmony_ci        destTm->tm_zone = srcTm->tm_zone;
780d163575Sopenharmony_ci    }
790d163575Sopenharmony_ci}
800d163575Sopenharmony_ci
810d163575Sopenharmony_ciSTATIC VOID OsCmdUsageDate(INT32 order)
820d163575Sopenharmony_ci{
830d163575Sopenharmony_ci    if (order) {
840d163575Sopenharmony_ci        PRINTK("date: invalid option or parameter.\n");
850d163575Sopenharmony_ci        PRINTK("Try 'date --help' for more information.\n");
860d163575Sopenharmony_ci        return;
870d163575Sopenharmony_ci    }
880d163575Sopenharmony_ci    PRINTK("\nUsage: date [+FORMAT]\n");
890d163575Sopenharmony_ci    PRINTK("   or: date [-s] [YY/MM/DD] [hh:mm:ss]\n");
900d163575Sopenharmony_ci    PRINTK("Display the current time in the given FORMAT, or set the system date.\n");
910d163575Sopenharmony_ci    PRINTK("FORMAT controls the output.  Interpreted sequences are:\n");
920d163575Sopenharmony_ci    PRINTK("  %%b     The abbreviated month name according to the current locale.\n");
930d163575Sopenharmony_ci    PRINTK("  %%B     The full month name according to the current locale.\n");
940d163575Sopenharmony_ci    PRINTK("  %%C     The century number (year/100) as a 2-digit integer. (SU)\n");
950d163575Sopenharmony_ci    PRINTK("  %%d     The day of the month as a decimal number (range 01 to 31).\n");
960d163575Sopenharmony_ci    PRINTK("  %%e     Like %%d, the day of the month as a decimal number, \n");
970d163575Sopenharmony_ci    PRINTK("         but a leading zero is replaced by a space.\n");
980d163575Sopenharmony_ci    PRINTK("  %%h     Equivalent to %%b.  (SU)\n");
990d163575Sopenharmony_ci    PRINTK("  %%H     The hour as a decimal number using a 24-hour clock (range 00 to 23).\n");
1000d163575Sopenharmony_ci    PRINTK("  %%I     The  hour as a decimal number using a 12-hour clock (range 01 to 12).\n");
1010d163575Sopenharmony_ci    PRINTK("  %%j     The day of the year as a decimal number (range 001 to 366).\n");
1020d163575Sopenharmony_ci    PRINTK("  %%k     The hour (24-hour clock) as a decimal number (range  0  to  23); \n");
1030d163575Sopenharmony_ci    PRINTK("         single digits are preceded by a blank.  (See also %H.)  (TZ)\n");
1040d163575Sopenharmony_ci    PRINTK("  %%l     The  hour  (12-hour  clock) as a decimal number (range 1 to 12); \n");
1050d163575Sopenharmony_ci    PRINTK("         single digits are preceded by a blank.  (See also %I.)  (TZ)\n");
1060d163575Sopenharmony_ci    PRINTK("  %%m     The month as a decimal number (range 01 to 12).\n");
1070d163575Sopenharmony_ci    PRINTK("  %%M     The minute as a decimal number (range 00 to 59).\n");
1080d163575Sopenharmony_ci    PRINTK("  %%n     A newline character. (SU)\n");
1090d163575Sopenharmony_ci    PRINTK("  %%p     Either \"AM\" or \"PM\" according to the given time  value, \n");
1100d163575Sopenharmony_ci    PRINTK("         or the corresponding  strings  for the current locale.\n");
1110d163575Sopenharmony_ci    PRINTK("         Noon is treated as \"PM\" and midnight as \"AM\".\n");
1120d163575Sopenharmony_ci    PRINTK("  %%P     Like %%p but in lowercase: \"am\" or \"pm\" \n");
1130d163575Sopenharmony_ci    PRINTK("         or a corresponding string for the current locale. (GNU)\n");
1140d163575Sopenharmony_ci    PRINTK("  %%s     The number of seconds since the Epoch, that is,\n");
1150d163575Sopenharmony_ci    PRINTK("         since 1970-01-01 00:00:00 UTC. (TZ)\n");
1160d163575Sopenharmony_ci    PRINTK("  %%S     The second as a decimal number (range 00 to 60).\n");
1170d163575Sopenharmony_ci    PRINTK("         (The range is up to 60 to allow for occasional leap seconds.)\n");
1180d163575Sopenharmony_ci    PRINTK("  %%t     A tab character. (SU)\n");
1190d163575Sopenharmony_ci    PRINTK("  %%y     The year as a decimal number without a century (range 00 to 99).\n");
1200d163575Sopenharmony_ci    PRINTK("  %%Y     The year as a decimal number including the century.\n");
1210d163575Sopenharmony_ci    PRINTK("  %%%%     A literal '%%' character.\n");
1220d163575Sopenharmony_ci    PRINTK("\nExamples:\n");
1230d163575Sopenharmony_ci    PRINTK("Set system date (2017-01-01)\n");
1240d163575Sopenharmony_ci    PRINTK("$ date -s 20170101\n");
1250d163575Sopenharmony_ci    PRINTK("Set system time (12:00:00)\n");
1260d163575Sopenharmony_ci    PRINTK("$ date -s 12:00:00\n");
1270d163575Sopenharmony_ci    PRINTK("Show the time with format Year-Month-Day\n");
1280d163575Sopenharmony_ci    PRINTK("$ date +%%Y-%%m-%%d\n");
1290d163575Sopenharmony_ci}
1300d163575Sopenharmony_ci
1310d163575Sopenharmony_ciSTATIC INT32 OsStrToTm(const CHAR *str, struct tm *tm)
1320d163575Sopenharmony_ci{
1330d163575Sopenharmony_ci    CHAR *ret = NULL;
1340d163575Sopenharmony_ci    UINT32 strLen = strlen(str);
1350d163575Sopenharmony_ci    if (strLen == 8) { /* 8:Time format string length, such as hh:mm:ss or yyyymmdd */
1360d163575Sopenharmony_ci        if (str[2] == ':') { /* 2:Index of Eigenvalues */
1370d163575Sopenharmony_ci            ret = strptime(str, "%H:%M:%S", tm);
1380d163575Sopenharmony_ci        } else {
1390d163575Sopenharmony_ci            ret = strptime(str, "%Y%m%d", tm);
1400d163575Sopenharmony_ci        }
1410d163575Sopenharmony_ci    } else if (strLen == 10) { /* 10:Time format string length,such as yyyy/mm/dd  */
1420d163575Sopenharmony_ci        ret = strptime(str, "%Y/%m/%d", tm);
1430d163575Sopenharmony_ci    } else if (strLen == 5) { /* 5:Time format string length,such as hh:mm or mm/dd */
1440d163575Sopenharmony_ci        if (str[2] == ':') { /* 2:Index of Eigenvalues */
1450d163575Sopenharmony_ci            ret = strptime(str, "%H:%M", tm);
1460d163575Sopenharmony_ci        } else if (str[2] == '/') { /* 2:Index of Eigenvalues */
1470d163575Sopenharmony_ci            ret = strptime(str, "%m/%d", tm);
1480d163575Sopenharmony_ci        }
1490d163575Sopenharmony_ci    } else if (strLen == 7) { /* 7:Time format string length,such as yyyy/mm */
1500d163575Sopenharmony_ci        if (str[4] == '/') { /* 4:Index of Eigenvalues */
1510d163575Sopenharmony_ci            ret = strptime(str, "%Y/%m", tm);
1520d163575Sopenharmony_ci        }
1530d163575Sopenharmony_ci    }
1540d163575Sopenharmony_ci
1550d163575Sopenharmony_ci    if (tm->tm_year < 70) { /* 70:the year is starting in 1970,tm_year must be greater than 70 */
1560d163575Sopenharmony_ci        PRINTK("\nUsage: date -s set system time starting from 1970.\n");
1570d163575Sopenharmony_ci        return DATE_ERR;
1580d163575Sopenharmony_ci    }
1590d163575Sopenharmony_ci
1600d163575Sopenharmony_ci    if (tm->tm_mday > g_monLengths[(INT32)LEAPYEAR(tm->tm_year + DATE_BASE_YEAR)][tm->tm_mon]) {
1610d163575Sopenharmony_ci        return DATE_ERR;
1620d163575Sopenharmony_ci    }
1630d163575Sopenharmony_ci
1640d163575Sopenharmony_ci    if ((tm->tm_sec < 0) || (tm->tm_sec > 59)) { /* Seconds (0-59), leap seconds shall not be used when set time. */
1650d163575Sopenharmony_ci        return DATE_ERR;
1660d163575Sopenharmony_ci    }
1670d163575Sopenharmony_ci    return (ret == NULL) ? DATE_ERR : DATE_OK;
1680d163575Sopenharmony_ci}
1690d163575Sopenharmony_ci
1700d163575Sopenharmony_ciSTATIC INT32 OsFormatPrintTime(const CHAR *formatStr)
1710d163575Sopenharmony_ci{
1720d163575Sopenharmony_ci    CHAR timebuf[SHOW_MAX_LEN] = {0};
1730d163575Sopenharmony_ci    struct tm *tm = NULL;
1740d163575Sopenharmony_ci    struct timeval64 nowTime = {0};
1750d163575Sopenharmony_ci
1760d163575Sopenharmony_ci    if (strlen(formatStr) < 2) { /* 2:check format string length */
1770d163575Sopenharmony_ci        OsCmdUsageDate(DATE_ERR_INFO);
1780d163575Sopenharmony_ci        return DATE_ERR;
1790d163575Sopenharmony_ci    }
1800d163575Sopenharmony_ci
1810d163575Sopenharmony_ci    if (gettimeofday64(&nowTime, NULL)) {
1820d163575Sopenharmony_ci        return DATE_ERR;
1830d163575Sopenharmony_ci    }
1840d163575Sopenharmony_ci    tm = localtime64(&nowTime.tv_sec);
1850d163575Sopenharmony_ci    if (tm == NULL) {
1860d163575Sopenharmony_ci        return DATE_ERR;
1870d163575Sopenharmony_ci    }
1880d163575Sopenharmony_ci
1890d163575Sopenharmony_ci    if (strftime(timebuf, SHOW_MAX_LEN - 1, formatStr + 1, tm)) {
1900d163575Sopenharmony_ci        PRINTK("%s\n", timebuf);
1910d163575Sopenharmony_ci    } else {
1920d163575Sopenharmony_ci        OsCmdUsageDate(DATE_ERR_INFO);
1930d163575Sopenharmony_ci        return DATE_ERR;
1940d163575Sopenharmony_ci    }
1950d163575Sopenharmony_ci    return DATE_OK;
1960d163575Sopenharmony_ci}
1970d163575Sopenharmony_ci
1980d163575Sopenharmony_ciSTATIC INT32 OsDateSetTime(const CHAR *timeStr)
1990d163575Sopenharmony_ci{
2000d163575Sopenharmony_ci    struct tm tm = {0};
2010d163575Sopenharmony_ci    struct timeval64 nowTime = {0};
2020d163575Sopenharmony_ci    struct timeval64 setTime = {0};
2030d163575Sopenharmony_ci
2040d163575Sopenharmony_ci    if (gettimeofday64(&nowTime, NULL)) {
2050d163575Sopenharmony_ci        PRINTK("Setting time failed...\n");
2060d163575Sopenharmony_ci        return DATE_ERR;
2070d163575Sopenharmony_ci    }
2080d163575Sopenharmony_ci
2090d163575Sopenharmony_ci    setTime.tv_usec = nowTime.tv_usec;
2100d163575Sopenharmony_ci    OsCopyTm(&tm, localtime64(&nowTime.tv_sec));
2110d163575Sopenharmony_ci
2120d163575Sopenharmony_ci    if (OsStrToTm(timeStr, &tm)) {
2130d163575Sopenharmony_ci        OsCmdUsageDate(DATE_ERR_INFO);
2140d163575Sopenharmony_ci        return DATE_ERR;
2150d163575Sopenharmony_ci    }
2160d163575Sopenharmony_ci
2170d163575Sopenharmony_ci    setTime.tv_sec = mktime64(&tm);
2180d163575Sopenharmony_ci
2190d163575Sopenharmony_ci    if (settimeofday64(&setTime, NULL)) {
2200d163575Sopenharmony_ci        PRINTK("setting time failed...\n");
2210d163575Sopenharmony_ci        return DATE_ERR;
2220d163575Sopenharmony_ci    }
2230d163575Sopenharmony_ci
2240d163575Sopenharmony_ci    return DATE_OK;
2250d163575Sopenharmony_ci}
2260d163575Sopenharmony_ci
2270d163575Sopenharmony_ci#ifdef  LOSCFG_FS_VFS
2280d163575Sopenharmony_ciSTATIC INT32 OsViewFileTime(const CHAR *filename)
2290d163575Sopenharmony_ci{
2300d163575Sopenharmony_ci#define BUFFER_SIZE 26 /* The buffer size is equal to the size used by the asctime_r interface */
2310d163575Sopenharmony_ci    struct stat statBuf = {0};
2320d163575Sopenharmony_ci    CHAR *fullpath = NULL;
2330d163575Sopenharmony_ci    INT32 ret;
2340d163575Sopenharmony_ci    CHAR buf[BUFFER_SIZE];
2350d163575Sopenharmony_ci    CHAR *shellWorkingDirectory = OsShellGetWorkingDirectory();
2360d163575Sopenharmony_ci
2370d163575Sopenharmony_ci    ret = vfs_normalize_path(shellWorkingDirectory, filename, &fullpath);
2380d163575Sopenharmony_ci    if (ret < 0) {
2390d163575Sopenharmony_ci        set_errno(-ret);
2400d163575Sopenharmony_ci        perror("date error");
2410d163575Sopenharmony_ci        return DATE_ERR;
2420d163575Sopenharmony_ci    }
2430d163575Sopenharmony_ci
2440d163575Sopenharmony_ci    if (stat(fullpath, &statBuf) != 0) {
2450d163575Sopenharmony_ci        OsCmdUsageDate(DATE_ERR_INFO);
2460d163575Sopenharmony_ci        free(fullpath);
2470d163575Sopenharmony_ci        return DATE_ERR;
2480d163575Sopenharmony_ci    }
2490d163575Sopenharmony_ci    PRINTK("%s\n", ctime_r(&(statBuf.st_mtim.tv_sec), buf));
2500d163575Sopenharmony_ci    free(fullpath);
2510d163575Sopenharmony_ci    return DATE_OK;
2520d163575Sopenharmony_ci}
2530d163575Sopenharmony_ci#endif
2540d163575Sopenharmony_ci
2550d163575Sopenharmony_ciINT32 OsShellCmdDate(INT32 argc, const CHAR **argv)
2560d163575Sopenharmony_ci{
2570d163575Sopenharmony_ci    struct timeval64 nowTime = {0};
2580d163575Sopenharmony_ci
2590d163575Sopenharmony_ci    if (argc == 1) { /* 1:count of parameters */
2600d163575Sopenharmony_ci        if (gettimeofday64(&nowTime, NULL)) {
2610d163575Sopenharmony_ci            return DATE_ERR;
2620d163575Sopenharmony_ci        }
2630d163575Sopenharmony_ci        PRINTK("%s\n", ctime64(&nowTime.tv_sec));
2640d163575Sopenharmony_ci        return DATE_OK;
2650d163575Sopenharmony_ci    }
2660d163575Sopenharmony_ci
2670d163575Sopenharmony_ci    if (argc == 2) { /* 2:count of parameters */
2680d163575Sopenharmony_ci        if (argv == NULL) {
2690d163575Sopenharmony_ci            OsCmdUsageDate(DATE_HELP_INFO);
2700d163575Sopenharmony_ci            return DATE_ERR;
2710d163575Sopenharmony_ci        }
2720d163575Sopenharmony_ci
2730d163575Sopenharmony_ci        if (!(strcmp(argv[1], "--help"))) {
2740d163575Sopenharmony_ci            OsCmdUsageDate(DATE_HELP_INFO);
2750d163575Sopenharmony_ci            return DATE_OK;
2760d163575Sopenharmony_ci        }
2770d163575Sopenharmony_ci        if (!(strncmp(argv[1], "+", 1))) {
2780d163575Sopenharmony_ci            return OsFormatPrintTime(argv[1]);
2790d163575Sopenharmony_ci        }
2800d163575Sopenharmony_ci    }
2810d163575Sopenharmony_ci
2820d163575Sopenharmony_ci    if (argc > 2) { /* 2:count of parameters */
2830d163575Sopenharmony_ci        if (argv == NULL) {
2840d163575Sopenharmony_ci            OsCmdUsageDate(DATE_HELP_INFO);
2850d163575Sopenharmony_ci            return DATE_ERR;
2860d163575Sopenharmony_ci        }
2870d163575Sopenharmony_ci
2880d163575Sopenharmony_ci        if (!(strcmp(argv[1], "-s"))) {
2890d163575Sopenharmony_ci            return OsDateSetTime(argv[2]); /* 2:index of parameters */
2900d163575Sopenharmony_ci        } else if (!(strcmp(argv[1], "-r"))) {
2910d163575Sopenharmony_ci#ifdef  LOSCFG_FS_VFS
2920d163575Sopenharmony_ci            return OsViewFileTime(argv[2]); /* 2:index of parameters */
2930d163575Sopenharmony_ci#endif
2940d163575Sopenharmony_ci        }
2950d163575Sopenharmony_ci    }
2960d163575Sopenharmony_ci
2970d163575Sopenharmony_ci    OsCmdUsageDate(DATE_ERR_INFO);
2980d163575Sopenharmony_ci    return DATE_OK;
2990d163575Sopenharmony_ci}
3000d163575Sopenharmony_ci
3010d163575Sopenharmony_ciSHELLCMD_ENTRY(date_shellcmd, CMD_TYPE_STD, "date", XARGS, (CmdCallBackFunc)OsShellCmdDate);
302