]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_date.c
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 #if (CONFIG_COMMANDS & CFG_CMD_DATE)
33 const char *weekdays
[] = {
34 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
37 int mk_date (char *, struct rtc_time
*);
39 int do_date (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
45 case 2: /* set date & time */
46 if (strcmp(argv
[1],"reset") == 0) {
47 printf ("Reset RTC...\n");
50 /* initialize tm with current time */
52 /* insert new date & time */
53 if (mk_date (argv
[1], &tm
) != 0) {
54 printf ("## Bad date format\n");
57 /* and write to RTC */
61 case 1: /* get date & time */
64 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
65 tm
.tm_year
, tm
.tm_mon
, tm
.tm_mday
,
66 (tm
.tm_wday
<0 || tm
.tm_wday
>6) ?
67 "unknown " : weekdays
[tm
.tm_wday
],
68 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
72 printf ("Usage:\n%s\n", cmdtp
->usage
);
79 * simple conversion of two-digit string with error checking
81 static int cnvrt2 (char *str
, int *valp
)
85 if ((*str
< '0') || (*str
> '9'))
92 if ((*str
< '0') || (*str
> '9'))
95 *valp
= 10 * val
+ (*str
- '0');
101 * Convert date string: MMDDhhmm[[CC]YY][.ss]
103 * Some basic checking for valid values is done, but this will not catch
104 * all possible error conditions.
106 int mk_date (char *datestr
, struct rtc_time
*tmp
)
111 ptr
= strchr (datestr
,'.');
112 len
= strlen (datestr
);
119 if ((len
- (ptr
- datestr
)) != 2)
122 len
= strlen (datestr
);
124 if (cnvrt2 (ptr
, &sec
))
132 if (len
== 12) { /* MMDDhhmmCCYY */
135 if (cnvrt2 (datestr
+ 8, ¢ury
) ||
136 cnvrt2 (datestr
+10, &year
) ) {
139 tmp
->tm_year
= 100 * century
+ year
;
140 } else if (len
== 10) { /* MMDDhhmmYY */
143 century
= tmp
->tm_year
/ 100;
144 if (cnvrt2 (datestr
+ 8, &year
))
146 tmp
->tm_year
= 100 * century
+ year
;
150 case 8: /* MMDDhhmm */
152 case 10: /* MMDDhhmmYY */
154 case 12: /* MMDDhhmmCCYY */
155 if (cnvrt2 (datestr
+0, &val
) ||
160 if (cnvrt2 (datestr
+2, &val
) ||
161 val
> ((tmp
->tm_mon
==2) ? 29 : 31)) {
166 if (cnvrt2 (datestr
+4, &val
) ||
172 if (cnvrt2 (datestr
+6, &val
) ||
178 /* calculate day of week */
189 #endif /* CFG_CMD_DATE */