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 struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
32 static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
38 /* switch to correct I2C bus */
40 old_bus = i2c_get_bus_num();
41 i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
43 old_bus = I2C_GET_BUS();
44 I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
48 case 2: /* set date & time */
49 if (strcmp(argv[1],"reset") == 0) {
50 puts ("Reset RTC...\n");
52 rcode = rtc_set(&default_tm);
54 puts("## Failed to set date after RTC reset\n");
56 /* initialize tm with current time */
57 rcode = rtc_get (&tm);
60 /* insert new date & time */
61 if (mk_date (argv[1], &tm) != 0) {
62 puts ("## Bad date format\n");
65 /* and write to RTC */
66 rcode = rtc_set (&tm);
68 puts("## Set date failed\n");
70 puts("## Get date failed\n");
74 case 1: /* get date & time */
75 rcode = rtc_get (&tm);
78 puts("## Get date failed\n");
82 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
83 tm.tm_year, tm.tm_mon, tm.tm_mday,
84 (tm.tm_wday<0 || tm.tm_wday>6) ?
85 "unknown " : RELOC(weekdays[tm.tm_wday]),
86 tm.tm_hour, tm.tm_min, tm.tm_sec);
90 rcode = CMD_RET_USAGE;
93 /* switch back to original I2C bus */
95 i2c_set_bus_num(old_bus);
104 * simple conversion of two-digit string with error checking
106 static int cnvrt2 (const char *str, int *valp)
110 if ((*str < '0') || (*str > '9'))
117 if ((*str < '0') || (*str > '9'))
120 *valp = 10 * val + (*str - '0');
126 * Convert date string: MMDDhhmm[[CC]YY][.ss]
128 * Some basic checking for valid values is done, but this will not catch
129 * all possible error conditions.
131 int mk_date (const char *datestr, struct rtc_time *tmp)
136 ptr = strchr (datestr,'.');
137 len = strlen (datestr);
144 if ((len - (ptr - datestr)) != 2)
147 len = strlen (datestr);
149 if (cnvrt2 (ptr, &sec))
157 if (len == 12) { /* MMDDhhmmCCYY */
160 if (cnvrt2 (datestr+ 8, ¢ury) ||
161 cnvrt2 (datestr+10, &year) ) {
164 tmp->tm_year = 100 * century + year;
165 } else if (len == 10) { /* MMDDhhmmYY */
168 century = tmp->tm_year / 100;
169 if (cnvrt2 (datestr+ 8, &year))
171 tmp->tm_year = 100 * century + year;
175 case 8: /* MMDDhhmm */
177 case 10: /* MMDDhhmmYY */
179 case 12: /* MMDDhhmmCCYY */
180 if (cnvrt2 (datestr+0, &val) ||
185 if (cnvrt2 (datestr+2, &val) ||
186 val > ((tmp->tm_mon==2) ? 29 : 31)) {
191 if (cnvrt2 (datestr+4, &val) ||
197 if (cnvrt2 (datestr+6, &val) ||
203 /* calculate day of week */
214 /***************************************************/
218 "get/set/reset date & time",
219 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
220 " - without arguments: print date & time\n"
221 " - with numeric argument: set the system date & time\n"
222 " - with 'reset' argument: reset the RTC"