更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
date_shellcmd.c 文件参考

浏览源代码.

函数

STATIC VOID OsCopyTm (struct tm *destTm, const struct tm *srcTm)
 
STATIC VOID OsCmdUsageDate (INT32 order)
 
STATIC INT32 OsStrToTm (const CHAR *str, struct tm *tm)
 
STATIC INT32 OsFormatPrintTime (const CHAR *formatStr)
 
STATIC INT32 OsDateSetTime (const CHAR *timeStr)
 
STATIC INT32 OsViewFileTime (const CHAR *filename)
 
INT32 OsShellCmdDate (INT32 argc, const CHAR **argv)
 
 SHELLCMD_ENTRY (date_shellcmd, CMD_TYPE_STD, "date", XARGS,(CmdCallBackFunc) OsShellCmdDate)
 

变量

STATIC const INT32 g_monLengths [2][12]
 

函数说明

◆ OsCmdUsageDate()

STATIC VOID OsCmdUsageDate ( INT32  order)

在文件 date_shellcmd.c81 行定义.

82{
83 if (order) {
84 PRINTK("date: invalid option or parameter.\n");
85 PRINTK("Try 'date --help' for more information.\n");
86 return;
87 }
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");
129}
这是这个函数的调用关系图:

◆ OsCopyTm()

STATIC VOID OsCopyTm ( struct tm *  destTm,
const struct tm *  srcTm 
)

在文件 date_shellcmd.c62 行定义.

63{
64 if (srcTm == NULL) {
65 (VOID)memset_s(destTm, sizeof(struct tm), 0, sizeof(struct tm));
66 } else {
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;
78 }
79}
这是这个函数的调用关系图:

◆ OsDateSetTime()

STATIC INT32 OsDateSetTime ( const CHAR timeStr)

在文件 date_shellcmd.c198 行定义.

199{
200 struct tm tm = {0};
201 struct timeval64 nowTime = {0};
202 struct timeval64 setTime = {0};
203
204 if (gettimeofday64(&nowTime, NULL)) {
205 PRINTK("Setting time failed...\n");
206 return DATE_ERR;
207 }
208
209 setTime.tv_usec = nowTime.tv_usec;
210 OsCopyTm(&tm, localtime64(&nowTime.tv_sec));
211
212 if (OsStrToTm(timeStr, &tm)) {
213 OsCmdUsageDate(DATE_ERR_INFO);
214 return DATE_ERR;
215 }
216
217 setTime.tv_sec = mktime64(&tm);
218
219 if (settimeofday64(&setTime, NULL)) {
220 PRINTK("setting time failed...\n");
221 return DATE_ERR;
222 }
223
224 return DATE_OK;
225}
STATIC VOID OsCopyTm(struct tm *destTm, const struct tm *srcTm)
Definition: date_shellcmd.c:62
STATIC INT32 OsStrToTm(const CHAR *str, struct tm *tm)
STATIC VOID OsCmdUsageDate(INT32 order)
Definition: date_shellcmd.c:81
int settimeofday64(const struct timeval64 *tv, const struct timezone *tz)
Definition: time.c:346
int gettimeofday64(struct timeval64 *tv, struct timezone *tz)
Definition: time.c:391
函数调用图:
这是这个函数的调用关系图:

◆ OsFormatPrintTime()

STATIC INT32 OsFormatPrintTime ( const CHAR formatStr)

在文件 date_shellcmd.c170 行定义.

171{
172 CHAR timebuf[SHOW_MAX_LEN] = {0};
173 struct tm *tm = NULL;
174 struct timeval64 nowTime = {0};
175
176 if (strlen(formatStr) < 2) { /* 2:check format string length */
177 OsCmdUsageDate(DATE_ERR_INFO);
178 return DATE_ERR;
179 }
180
181 if (gettimeofday64(&nowTime, NULL)) {
182 return DATE_ERR;
183 }
184 tm = localtime64(&nowTime.tv_sec);
185 if (tm == NULL) {
186 return DATE_ERR;
187 }
188
189 if (strftime(timebuf, SHOW_MAX_LEN - 1, formatStr + 1, tm)) {
190 PRINTK("%s\n", timebuf);
191 } else {
192 OsCmdUsageDate(DATE_ERR_INFO);
193 return DATE_ERR;
194 }
195 return DATE_OK;
196}
char CHAR
Definition: los_typedef.h:63
函数调用图:
这是这个函数的调用关系图:

◆ OsShellCmdDate()

INT32 OsShellCmdDate ( INT32  argc,
const CHAR **  argv 
)

在文件 date_shellcmd.c255 行定义.

256{
257 struct timeval64 nowTime = {0};
258
259 if (argc == 1) { /* 1:count of parameters */
260 if (gettimeofday64(&nowTime, NULL)) {
261 return DATE_ERR;
262 }
263 PRINTK("%s\n", ctime64(&nowTime.tv_sec));
264 return DATE_OK;
265 }
266
267 if (argc == 2) { /* 2:count of parameters */
268 if (argv == NULL) {
269 OsCmdUsageDate(DATE_HELP_INFO);
270 return DATE_ERR;
271 }
272
273 if (!(strcmp(argv[1], "--help"))) {
274 OsCmdUsageDate(DATE_HELP_INFO);
275 return DATE_OK;
276 }
277 if (!(strncmp(argv[1], "+", 1))) {
278 return OsFormatPrintTime(argv[1]);
279 }
280 }
281
282 if (argc > 2) { /* 2:count of parameters */
283 if (argv == NULL) {
284 OsCmdUsageDate(DATE_HELP_INFO);
285 return DATE_ERR;
286 }
287
288 if (!(strcmp(argv[1], "-s"))) {
289 return OsDateSetTime(argv[2]); /* 2:index of parameters */
290 } else if (!(strcmp(argv[1], "-r"))) {
291#ifdef LOSCFG_FS_VFS
292 return OsViewFileTime(argv[2]); /* 2:index of parameters */
293#endif
294 }
295 }
296
297 OsCmdUsageDate(DATE_ERR_INFO);
298 return DATE_OK;
299}
STATIC INT32 OsViewFileTime(const CHAR *filename)
STATIC INT32 OsDateSetTime(const CHAR *timeStr)
STATIC INT32 OsFormatPrintTime(const CHAR *formatStr)
函数调用图:

◆ OsStrToTm()

STATIC INT32 OsStrToTm ( const CHAR str,
struct tm *  tm 
)

在文件 date_shellcmd.c131 行定义.

132{
133 CHAR *ret = NULL;
134 UINT32 strLen = strlen(str);
135 if (strLen == 8) { /* 8:Time format string length, such as hh:mm:ss or yyyymmdd */
136 if (str[2] == ':') { /* 2:Index of Eigenvalues */
137 ret = strptime(str, "%H:%M:%S", tm);
138 } else {
139 ret = strptime(str, "%Y%m%d", tm);
140 }
141 } else if (strLen == 10) { /* 10:Time format string length,such as yyyy/mm/dd */
142 ret = strptime(str, "%Y/%m/%d", tm);
143 } else if (strLen == 5) { /* 5:Time format string length,such as hh:mm or mm/dd */
144 if (str[2] == ':') { /* 2:Index of Eigenvalues */
145 ret = strptime(str, "%H:%M", tm);
146 } else if (str[2] == '/') { /* 2:Index of Eigenvalues */
147 ret = strptime(str, "%m/%d", tm);
148 }
149 } else if (strLen == 7) { /* 7:Time format string length,such as yyyy/mm */
150 if (str[4] == '/') { /* 4:Index of Eigenvalues */
151 ret = strptime(str, "%Y/%m", tm);
152 }
153 }
154
155 if (tm->tm_year < 70) { /* 70:the year is starting in 1970,tm_year must be greater than 70 */
156 PRINTK("\nUsage: date -s set system time starting from 1970.\n");
157 return DATE_ERR;
158 }
159
160 if (tm->tm_mday > g_monLengths[(INT32)LEAPYEAR(tm->tm_year + DATE_BASE_YEAR)][tm->tm_mon]) {
161 return DATE_ERR;
162 }
163
164 if ((tm->tm_sec < 0) || (tm->tm_sec > 59)) { /* Seconds (0-59), leap seconds shall not be used when set time. */
165 return DATE_ERR;
166 }
167 return (ret == NULL) ? DATE_ERR : DATE_OK;
168}
STATIC const INT32 g_monLengths[2][12]
Definition: date_shellcmd.c:57
signed int INT32
Definition: los_typedef.h:60
unsigned int UINT32
Definition: los_typedef.h:57
这是这个函数的调用关系图:

◆ OsViewFileTime()

STATIC INT32 OsViewFileTime ( const CHAR filename)

在文件 date_shellcmd.c228 行定义.

229{
230#define BUFFER_SIZE 26 /* The buffer size is equal to the size used by the asctime_r interface */
231 struct stat statBuf = {0};
232 CHAR *fullpath = NULL;
233 INT32 ret;
234 CHAR buf[BUFFER_SIZE];
235 CHAR *shellWorkingDirectory = OsShellGetWorkingDirectory();
236
237 ret = vfs_normalize_path(shellWorkingDirectory, filename, &fullpath);
238 if (ret < 0) {
239 set_errno(-ret);
240 perror("date error");
241 return DATE_ERR;
242 }
243
244 if (stat(fullpath, &statBuf) != 0) {
245 OsCmdUsageDate(DATE_ERR_INFO);
246 free(fullpath);
247 return DATE_ERR;
248 }
249 PRINTK("%s\n", ctime_r(&(statBuf.st_mtim.tv_sec), buf));
250 free(fullpath);
251 return DATE_OK;
252}
char * OsShellGetWorkingDirectory(void)
Definition: shcmd.c:94
int vfs_normalize_path(const char *directory, const char *filename, char **pathname)
Definition: fullpath.c:245
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
函数调用图:
这是这个函数的调用关系图:

◆ SHELLCMD_ENTRY()

SHELLCMD_ENTRY ( date_shellcmd  ,
CMD_TYPE_STD  ,
"date"  ,
XARGS  ,
(CmdCallBackFunc OsShellCmdDate 
)

变量说明

◆ g_monLengths

STATIC const INT32 g_monLengths[2][12]
初始值:
= {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
}

在文件 date_shellcmd.c57 行定义.