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 #if (CONFIG_COMMANDS & CFG_CMD_DATE)
36 const char *weekdays[] = {
37 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
40 #define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
42 int mk_date (char *, struct rtc_time *);
44 int do_date (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
50 /* switch to correct I2C bus */
51 old_bus = I2C_GET_BUS();
52 I2C_SET_BUS(CFG_RTC_BUS_NUM);
55 case 2: /* set date & time */
56 if (strcmp(argv[1],"reset") == 0) {
57 puts ("Reset RTC...\n");
60 /* initialize tm with current time */
62 /* insert new date & time */
63 if (mk_date (argv[1], &tm) != 0) {
64 puts ("## Bad date format\n");
67 /* and write to RTC */
71 case 1: /* get date & time */
74 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
75 tm.tm_year, tm.tm_mon, tm.tm_mday,
76 (tm.tm_wday<0 || tm.tm_wday>6) ?
77 "unknown " : RELOC(weekdays[tm.tm_wday]),
78 tm.tm_hour, tm.tm_min, tm.tm_sec);
82 printf ("Usage:\n%s\n", cmdtp->usage);
86 /* switch back to original I2C bus */
93 * simple conversion of two-digit string with error checking
95 static int cnvrt2 (char *str, int *valp)
99 if ((*str < '0') || (*str > '9'))
106 if ((*str < '0') || (*str > '9'))
109 *valp = 10 * val + (*str - '0');
115 * Convert date string: MMDDhhmm[[CC]YY][.ss]
117 * Some basic checking for valid values is done, but this will not catch
118 * all possible error conditions.
120 int mk_date (char *datestr, struct rtc_time *tmp)
125 ptr = strchr (datestr,'.');
126 len = strlen (datestr);
133 if ((len - (ptr - datestr)) != 2)
136 len = strlen (datestr);
138 if (cnvrt2 (ptr, &sec))
146 if (len == 12) { /* MMDDhhmmCCYY */
149 if (cnvrt2 (datestr+ 8, ¢ury) ||
150 cnvrt2 (datestr+10, &year) ) {
153 tmp->tm_year = 100 * century + year;
154 } else if (len == 10) { /* MMDDhhmmYY */
157 century = tmp->tm_year / 100;
158 if (cnvrt2 (datestr+ 8, &year))
160 tmp->tm_year = 100 * century + year;
164 case 8: /* MMDDhhmm */
166 case 10: /* MMDDhhmmYY */
168 case 12: /* MMDDhhmmCCYY */
169 if (cnvrt2 (datestr+0, &val) ||
174 if (cnvrt2 (datestr+2, &val) ||
175 val > ((tmp->tm_mon==2) ? 29 : 31)) {
180 if (cnvrt2 (datestr+4, &val) ||
186 if (cnvrt2 (datestr+6, &val) ||
192 /* calculate day of week */
203 /***************************************************/
207 "date - get/set/reset date & time\n",
208 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
209 " - without arguments: print date & time\n"
210 " - with numeric argument: set the system date & time\n"
211 " - with 'reset' argument: reset the RTC\n"
214 #endif /* CFG_CMD_DATE */