]> git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_date.c
Merge branch 'master' of rsync://rsync.denx.de/git/u-boot
[people/ms/u-boot.git] / common / cmd_date.c
1 /*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
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.
12 *
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.
17 *
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,
21 * MA 02111-1307 USA
22 */
23
24 /*
25 * RTC, Date & Time support: get and set date & time
26 */
27 #include <common.h>
28 #include <command.h>
29 #include <rtc.h>
30
31 DECLARE_GLOBAL_DATA_PTR;
32
33 #if (CONFIG_COMMANDS & CFG_CMD_DATE)
34
35 const char *weekdays[] = {
36 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
37 };
38
39 #define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
40
41 int mk_date (char *, struct rtc_time *);
42
43 int do_date (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
44 {
45 struct rtc_time tm;
46 int rcode = 0;
47
48 switch (argc) {
49 case 2: /* set date & time */
50 if (strcmp(argv[1],"reset") == 0) {
51 puts ("Reset RTC...\n");
52 rtc_reset ();
53 } else {
54 /* initialize tm with current time */
55 rtc_get (&tm);
56 /* insert new date & time */
57 if (mk_date (argv[1], &tm) != 0) {
58 puts ("## Bad date format\n");
59 return 1;
60 }
61 /* and write to RTC */
62 rtc_set (&tm);
63 }
64 /* FALL TROUGH */
65 case 1: /* get date & time */
66 rtc_get (&tm);
67
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);
73
74 return 0;
75 default:
76 printf ("Usage:\n%s\n", cmdtp->usage);
77 rcode = 1;
78 }
79 return rcode;
80 }
81
82 /*
83 * simple conversion of two-digit string with error checking
84 */
85 static int cnvrt2 (char *str, int *valp)
86 {
87 int val;
88
89 if ((*str < '0') || (*str > '9'))
90 return (-1);
91
92 val = *str - '0';
93
94 ++str;
95
96 if ((*str < '0') || (*str > '9'))
97 return (-1);
98
99 *valp = 10 * val + (*str - '0');
100
101 return (0);
102 }
103
104 /*
105 * Convert date string: MMDDhhmm[[CC]YY][.ss]
106 *
107 * Some basic checking for valid values is done, but this will not catch
108 * all possible error conditions.
109 */
110 int mk_date (char *datestr, struct rtc_time *tmp)
111 {
112 int len, val;
113 char *ptr;
114
115 ptr = strchr (datestr,'.');
116 len = strlen (datestr);
117
118 /* Set seconds */
119 if (ptr) {
120 int sec;
121
122 *ptr++ = '\0';
123 if ((len - (ptr - datestr)) != 2)
124 return (-1);
125
126 len = strlen (datestr);
127
128 if (cnvrt2 (ptr, &sec))
129 return (-1);
130
131 tmp->tm_sec = sec;
132 } else {
133 tmp->tm_sec = 0;
134 }
135
136 if (len == 12) { /* MMDDhhmmCCYY */
137 int year, century;
138
139 if (cnvrt2 (datestr+ 8, &century) ||
140 cnvrt2 (datestr+10, &year) ) {
141 return (-1);
142 }
143 tmp->tm_year = 100 * century + year;
144 } else if (len == 10) { /* MMDDhhmmYY */
145 int year, century;
146
147 century = tmp->tm_year / 100;
148 if (cnvrt2 (datestr+ 8, &year))
149 return (-1);
150 tmp->tm_year = 100 * century + year;
151 }
152
153 switch (len) {
154 case 8: /* MMDDhhmm */
155 /* fall thru */
156 case 10: /* MMDDhhmmYY */
157 /* fall thru */
158 case 12: /* MMDDhhmmCCYY */
159 if (cnvrt2 (datestr+0, &val) ||
160 val > 12) {
161 break;
162 }
163 tmp->tm_mon = val;
164 if (cnvrt2 (datestr+2, &val) ||
165 val > ((tmp->tm_mon==2) ? 29 : 31)) {
166 break;
167 }
168 tmp->tm_mday = val;
169
170 if (cnvrt2 (datestr+4, &val) ||
171 val > 23) {
172 break;
173 }
174 tmp->tm_hour = val;
175
176 if (cnvrt2 (datestr+6, &val) ||
177 val > 59) {
178 break;
179 }
180 tmp->tm_min = val;
181
182 /* calculate day of week */
183 GregorianDay (tmp);
184
185 return (0);
186 default:
187 break;
188 }
189
190 return (-1);
191 }
192
193 /***************************************************/
194
195 U_BOOT_CMD(
196 date, 2, 1, do_date,
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"
202 );
203
204 #endif /* CFG_CMD_DATE */