41#define timeval64 timeval
42#define settimeofday64 settimeofday
43#define gettimeofday64 gettimeofday
46#define localtime64 localtime
48#define mktime64 mktime
50#define DATE_ERR_INFO 1
51#define DATE_HELP_INFO 0
54#define DATE_BASE_YEAR 1900
55#define LEAPYEAR(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
58 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
59 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
62STATIC VOID
OsCopyTm(
struct tm *destTm,
const struct tm *srcTm)
65 (VOID)memset_s(destTm,
sizeof(
struct tm), 0,
sizeof(
struct tm));
67 destTm->tm_sec = srcTm->tm_sec;
68 destTm->tm_min = srcTm->tm_min;
69 destTm->tm_hour = srcTm->tm_hour;
70 destTm->tm_mday = srcTm->tm_mday;
71 destTm->tm_mon = srcTm->tm_mon;
72 destTm->tm_year = srcTm->tm_year;
73 destTm->tm_wday = srcTm->tm_wday;
74 destTm->tm_yday = srcTm->tm_yday;
75 destTm->tm_isdst = srcTm->tm_isdst;
76 destTm->tm_gmtoff = srcTm->tm_gmtoff;
77 destTm->tm_zone = srcTm->tm_zone;
84 PRINTK(
"date: invalid option or parameter.\n");
85 PRINTK(
"Try 'date --help' for more information.\n");
88 PRINTK(
"\nUsage: date [+FORMAT]\n");
89 PRINTK(
" or: date [-s] [YY/MM/DD] [hh:mm:ss]\n");
90 PRINTK(
"Display the current time in the given FORMAT, or set the system date.\n");
91 PRINTK(
"FORMAT controls the output. Interpreted sequences are:\n");
92 PRINTK(
" %%b The abbreviated month name according to the current locale.\n");
93 PRINTK(
" %%B The full month name according to the current locale.\n");
94 PRINTK(
" %%C The century number (year/100) as a 2-digit integer. (SU)\n");
95 PRINTK(
" %%d The day of the month as a decimal number (range 01 to 31).\n");
96 PRINTK(
" %%e Like %%d, the day of the month as a decimal number, \n");
97 PRINTK(
" but a leading zero is replaced by a space.\n");
98 PRINTK(
" %%h Equivalent to %%b. (SU)\n");
99 PRINTK(
" %%H The hour as a decimal number using a 24-hour clock (range 00 to 23).\n");
100 PRINTK(
" %%I The hour as a decimal number using a 12-hour clock (range 01 to 12).\n");
101 PRINTK(
" %%j The day of the year as a decimal number (range 001 to 366).\n");
102 PRINTK(
" %%k The hour (24-hour clock) as a decimal number (range 0 to 23); \n");
103 PRINTK(
" single digits are preceded by a blank. (See also %H.) (TZ)\n");
104 PRINTK(
" %%l The hour (12-hour clock) as a decimal number (range 1 to 12); \n");
105 PRINTK(
" single digits are preceded by a blank. (See also %I.) (TZ)\n");
106 PRINTK(
" %%m The month as a decimal number (range 01 to 12).\n");
107 PRINTK(
" %%M The minute as a decimal number (range 00 to 59).\n");
108 PRINTK(
" %%n A newline character. (SU)\n");
109 PRINTK(
" %%p Either \"AM\" or \"PM\" according to the given time value, \n");
110 PRINTK(
" or the corresponding strings for the current locale.\n");
111 PRINTK(
" Noon is treated as \"PM\" and midnight as \"AM\".\n");
112 PRINTK(
" %%P Like %%p but in lowercase: \"am\" or \"pm\" \n");
113 PRINTK(
" or a corresponding string for the current locale. (GNU)\n");
114 PRINTK(
" %%s The number of seconds since the Epoch, that is,\n");
115 PRINTK(
" since 1970-01-01 00:00:00 UTC. (TZ)\n");
116 PRINTK(
" %%S The second as a decimal number (range 00 to 60).\n");
117 PRINTK(
" (The range is up to 60 to allow for occasional leap seconds.)\n");
118 PRINTK(
" %%t A tab character. (SU)\n");
119 PRINTK(
" %%y The year as a decimal number without a century (range 00 to 99).\n");
120 PRINTK(
" %%Y The year as a decimal number including the century.\n");
121 PRINTK(
" %%%% A literal '%%' character.\n");
122 PRINTK(
"\nExamples:\n");
123 PRINTK(
"Set system date (2017-01-01)\n");
124 PRINTK(
"$ date -s 20170101\n");
125 PRINTK(
"Set system time (12:00:00)\n");
126 PRINTK(
"$ date -s 12:00:00\n");
127 PRINTK(
"Show the time with format Year-Month-Day\n");
128 PRINTK(
"$ date +%%Y-%%m-%%d\n");
134 UINT32 strLen = strlen(str);
137 ret = strptime(str,
"%H:%M:%S", tm);
139 ret = strptime(str,
"%Y%m%d", tm);
141 }
else if (strLen == 10) {
142 ret = strptime(str,
"%Y/%m/%d", tm);
143 }
else if (strLen == 5) {
145 ret = strptime(str,
"%H:%M", tm);
146 }
else if (str[2] ==
'/') {
147 ret = strptime(str,
"%m/%d", tm);
149 }
else if (strLen == 7) {
151 ret = strptime(str,
"%Y/%m", tm);
155 if (tm->tm_year < 70) {
156 PRINTK(
"\nUsage: date -s set system time starting from 1970.\n");
160 if (tm->tm_mday >
g_monLengths[(
INT32)LEAPYEAR(tm->tm_year + DATE_BASE_YEAR)][tm->tm_mon]) {
164 if ((tm->tm_sec < 0) || (tm->tm_sec > 59)) {
167 return (ret == NULL) ? DATE_ERR : DATE_OK;
172 CHAR timebuf[SHOW_MAX_LEN] = {0};
173 struct tm *tm = NULL;
174 struct timeval64 nowTime = {0};
176 if (strlen(formatStr) < 2) {
184 tm = localtime64(&nowTime.tv_sec);
189 if (strftime(timebuf, SHOW_MAX_LEN - 1, formatStr + 1, tm)) {
190 PRINTK(
"%s\n", timebuf);
201 struct timeval64 nowTime = {0};
202 struct timeval64 setTime = {0};
205 PRINTK(
"Setting time failed...\n");
209 setTime.tv_usec = nowTime.tv_usec;
210 OsCopyTm(&tm, localtime64(&nowTime.tv_sec));
217 setTime.tv_sec = mktime64(&tm);
220 PRINTK(
"setting time failed...\n");
230#define BUFFER_SIZE 26
231 struct stat statBuf = {0};
232 CHAR *fullpath = NULL;
234 CHAR buf[BUFFER_SIZE];
240 perror(
"date error");
244 if (stat(fullpath, &statBuf) != 0) {
249 PRINTK(
"%s\n", ctime_r(&(statBuf.st_mtim.tv_sec), buf));
257 struct timeval64 nowTime = {0};
263 PRINTK(
"%s\n", ctime64(&nowTime.tv_sec));
273 if (!(strcmp(argv[1],
"--help"))) {
277 if (!(strncmp(argv[1],
"+", 1))) {
288 if (!(strcmp(argv[1],
"-s"))) {
290 }
else if (!(strcmp(argv[1],
"-r"))) {
@ CMD_TYPE_STD
支持的标准命令参数输入,所有输入的字符都会通过命令解析后被传入。
char * OsShellGetWorkingDirectory(void)
INT32 OsShellCmdDate(INT32 argc, const CHAR **argv)
STATIC const INT32 g_monLengths[2][12]
STATIC VOID OsCopyTm(struct tm *destTm, const struct tm *srcTm)
STATIC INT32 OsViewFileTime(const CHAR *filename)
STATIC INT32 OsStrToTm(const CHAR *str, struct tm *tm)
SHELLCMD_ENTRY(date_shellcmd, CMD_TYPE_STD, "date", XARGS,(CmdCallBackFunc) OsShellCmdDate)
STATIC INT32 OsDateSetTime(const CHAR *timeStr)
STATIC INT32 OsFormatPrintTime(const CHAR *formatStr)
STATIC VOID OsCmdUsageDate(INT32 order)
int vfs_normalize_path(const char *directory, const char *filename, char **pathname)
void free(void *ptr)
释放ptr所指向的内存空间
int settimeofday64(const struct timeval64 *tv, const struct timezone *tz)
int gettimeofday64(struct timeval64 *tv, struct timezone *tz)
u32_t(* CmdCallBackFunc)(u32_t argc, const char **argv)