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
31 DECLARE_GLOBAL_DATA_PTR;
33 #if (CONFIG_COMMANDS & CFG_CMD_DATE)
35 const char *weekdays[] = {
36 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
39 #define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
41 int mk_date (char *, struct rtc_time *);
43 int do_date (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
49 case 2: /* set date & time */
50 if (strcmp(argv[1],"reset") == 0) {
51 puts ("Reset RTC...\n");
54 /* initialize tm with current time */
56 /* insert new date & time */
57 if (mk_date (argv[1], &tm) != 0) {
58 puts ("## Bad date format\n");
61 /* and write to RTC */
65 case 1: /* get date & time */
68 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
69 tm.tm_year, tm.tm_mon, tm.tm_mday,
70 (tm.tm_wday<0 || tm.tm_wday>6) ?
71 "unknown " : RELOC(weekdays[tm.tm_wday]),
72 tm.tm_hour, tm.tm_min, tm.tm_sec);
76 printf ("Usage:\n%s\n", cmdtp->usage);
83 * simple conversion of two-digit string with error checking
85 static int cnvrt2 (char *str, int *valp)
89 if ((*str < '0') || (*str > '9'))
96 if ((*str < '0') || (*str > '9'))
99 *valp = 10 * val + (*str - '0');
105 * Convert date string: MMDDhhmm[[CC]YY][.ss]
107 * Some basic checking for valid values is done, but this will not catch
108 * all possible error conditions.
110 int mk_date (char *datestr, struct rtc_time *tmp)
115 ptr = strchr (datestr,'.');
116 len = strlen (datestr);
123 if ((len - (ptr - datestr)) != 2)
126 len = strlen (datestr);
128 if (cnvrt2 (ptr, &sec))
136 if (len == 12) { /* MMDDhhmmCCYY */
139 if (cnvrt2 (datestr+ 8, ¢ury) ||
140 cnvrt2 (datestr+10, &year) ) {
143 tmp->tm_year = 100 * century + year;
144 } else if (len == 10) { /* MMDDhhmmYY */
147 century = tmp->tm_year / 100;
148 if (cnvrt2 (datestr+ 8, &year))
150 tmp->tm_year = 100 * century + year;
154 case 8: /* MMDDhhmm */
156 case 10: /* MMDDhhmmYY */
158 case 12: /* MMDDhhmmCCYY */
159 if (cnvrt2 (datestr+0, &val) ||
164 if (cnvrt2 (datestr+2, &val) ||
165 val > ((tmp->tm_mon==2) ? 29 : 31)) {
170 if (cnvrt2 (datestr+4, &val) ||
176 if (cnvrt2 (datestr+6, &val) ||
182 /* calculate day of week */
193 /***************************************************/
197 "date - get/set/reset date & time\n",
198 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
199 " - without arguments: print date & time\n"
200 " - with numeric argument: set the system date & time\n"
201 " - with 'reset' argument: reset the RTC\n"
204 #endif /* CFG_CMD_DATE */