3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
9 * RTC, Date & Time support: get and set date & time
16 DECLARE_GLOBAL_DATA_PTR;
18 static const char * const weekdays[] = {
19 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
22 #ifdef CONFIG_NEEDS_MANUAL_RELOC
23 #define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
28 int mk_date (const char *, struct rtc_time *);
30 static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
36 /* switch to correct I2C bus */
38 old_bus = i2c_get_bus_num();
39 i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
41 old_bus = I2C_GET_BUS();
42 I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
46 case 2: /* set date & time */
47 if (strcmp(argv[1],"reset") == 0) {
48 puts ("Reset RTC...\n");
51 /* initialize tm with current time */
52 rcode = rtc_get (&tm);
55 /* insert new date & time */
56 if (mk_date (argv[1], &tm) != 0) {
57 puts ("## Bad date format\n");
60 /* and write to RTC */
61 rcode = rtc_set (&tm);
63 puts("## Set date failed\n");
65 puts("## Get date failed\n");
69 case 1: /* get date & time */
70 rcode = rtc_get (&tm);
73 puts("## Get date failed\n");
77 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
78 tm.tm_year, tm.tm_mon, tm.tm_mday,
79 (tm.tm_wday<0 || tm.tm_wday>6) ?
80 "unknown " : RELOC(weekdays[tm.tm_wday]),
81 tm.tm_hour, tm.tm_min, tm.tm_sec);
85 rcode = CMD_RET_USAGE;
88 /* switch back to original I2C bus */
90 i2c_set_bus_num(old_bus);
99 * simple conversion of two-digit string with error checking
101 static int cnvrt2 (const char *str, int *valp)
105 if ((*str < '0') || (*str > '9'))
112 if ((*str < '0') || (*str > '9'))
115 *valp = 10 * val + (*str - '0');
121 * Convert date string: MMDDhhmm[[CC]YY][.ss]
123 * Some basic checking for valid values is done, but this will not catch
124 * all possible error conditions.
126 int mk_date (const char *datestr, struct rtc_time *tmp)
131 ptr = strchr (datestr,'.');
132 len = strlen (datestr);
139 if ((len - (ptr - datestr)) != 2)
142 len = strlen (datestr);
144 if (cnvrt2 (ptr, &sec))
152 if (len == 12) { /* MMDDhhmmCCYY */
155 if (cnvrt2 (datestr+ 8, ¢ury) ||
156 cnvrt2 (datestr+10, &year) ) {
159 tmp->tm_year = 100 * century + year;
160 } else if (len == 10) { /* MMDDhhmmYY */
163 century = tmp->tm_year / 100;
164 if (cnvrt2 (datestr+ 8, &year))
166 tmp->tm_year = 100 * century + year;
170 case 8: /* MMDDhhmm */
172 case 10: /* MMDDhhmmYY */
174 case 12: /* MMDDhhmmCCYY */
175 if (cnvrt2 (datestr+0, &val) ||
180 if (cnvrt2 (datestr+2, &val) ||
181 val > ((tmp->tm_mon==2) ? 29 : 31)) {
186 if (cnvrt2 (datestr+4, &val) ||
192 if (cnvrt2 (datestr+6, &val) ||
198 /* calculate day of week */
209 /***************************************************/
213 "get/set/reset date & time",
214 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
215 " - without arguments: print date & time\n"
216 " - with numeric argument: set the system date & time\n"
217 " - with 'reset' argument: reset the RTC"