3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * RTC, Date & Time support: get and set date & time
32 DECLARE_GLOBAL_DATA_PTR;
34 static const char * const weekdays[] = {
35 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
38 #ifdef CONFIG_NEEDS_MANUAL_RELOC
39 #define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
44 int mk_date (const char *, struct rtc_time *);
46 int do_date (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
52 /* switch to correct I2C bus */
53 old_bus = I2C_GET_BUS();
54 I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
57 case 2: /* set date & time */
58 if (strcmp(argv[1],"reset") == 0) {
59 puts ("Reset RTC...\n");
62 /* initialize tm with current time */
63 rcode = rtc_get (&tm);
66 /* insert new date & time */
67 if (mk_date (argv[1], &tm) != 0) {
68 puts ("## Bad date format\n");
71 /* and write to RTC */
72 rcode = rtc_set (&tm);
74 puts("## Set date failed\n");
76 puts("## Get date failed\n");
80 case 1: /* get date & time */
81 rcode = rtc_get (&tm);
84 puts("## Get date failed\n");
88 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
89 tm.tm_year, tm.tm_mon, tm.tm_mday,
90 (tm.tm_wday<0 || tm.tm_wday>6) ?
91 "unknown " : RELOC(weekdays[tm.tm_wday]),
92 tm.tm_hour, tm.tm_min, tm.tm_sec);
96 rcode = CMD_RET_USAGE;
99 /* switch back to original I2C bus */
100 I2C_SET_BUS(old_bus);
106 * simple conversion of two-digit string with error checking
108 static int cnvrt2 (const char *str, int *valp)
112 if ((*str < '0') || (*str > '9'))
119 if ((*str < '0') || (*str > '9'))
122 *valp = 10 * val + (*str - '0');
128 * Convert date string: MMDDhhmm[[CC]YY][.ss]
130 * Some basic checking for valid values is done, but this will not catch
131 * all possible error conditions.
133 int mk_date (const char *datestr, struct rtc_time *tmp)
138 ptr = strchr (datestr,'.');
139 len = strlen (datestr);
146 if ((len - (ptr - datestr)) != 2)
149 len = strlen (datestr);
151 if (cnvrt2 (ptr, &sec))
159 if (len == 12) { /* MMDDhhmmCCYY */
162 if (cnvrt2 (datestr+ 8, ¢ury) ||
163 cnvrt2 (datestr+10, &year) ) {
166 tmp->tm_year = 100 * century + year;
167 } else if (len == 10) { /* MMDDhhmmYY */
170 century = tmp->tm_year / 100;
171 if (cnvrt2 (datestr+ 8, &year))
173 tmp->tm_year = 100 * century + year;
177 case 8: /* MMDDhhmm */
179 case 10: /* MMDDhhmmYY */
181 case 12: /* MMDDhhmmCCYY */
182 if (cnvrt2 (datestr+0, &val) ||
187 if (cnvrt2 (datestr+2, &val) ||
188 val > ((tmp->tm_mon==2) ? 29 : 31)) {
193 if (cnvrt2 (datestr+4, &val) ||
199 if (cnvrt2 (datestr+6, &val) ||
205 /* calculate day of week */
216 /***************************************************/
220 "get/set/reset date & time",
221 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
222 " - without arguments: print date & time\n"
223 " - with numeric argument: set the system date & time\n"
224 " - with 'reset' argument: reset the RTC"