]>
Commit | Line | Data |
---|---|---|
61b0cfe9 L |
1 | /* |
2 | * (C) Copyright 2001 | |
3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
4 | * | |
5 | * SPDX-License-Identifier: GPL-2.0+ | |
6 | */ | |
7 | ||
8 | /* | |
9 | * RTC, Date & Time support: get and set date & time | |
10 | */ | |
11 | #include <common.h> | |
12 | #include <string.h> | |
61b0cfe9 L |
13 | #include <command.h> |
14 | #include <rtc.h> | |
15 | #include <i2c.h> | |
16 | ||
17 | ||
c93a1fd4 L |
18 | static const FLASH char * const FLASH weekdays[] = { |
19 | FSTR("Mon"), | |
20 | FSTR("Tues"), | |
21 | FSTR("Wednes"), | |
22 | FSTR("Thurs"), | |
23 | FSTR("Fri"), | |
24 | FSTR("Satur"), | |
25 | FSTR("Sun") | |
61b0cfe9 L |
26 | }; |
27 | ||
28 | int mk_date (const char *, struct rtc_time *); | |
29 | ||
30 | command_ret_t do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
31 | { | |
32 | struct rtc_time tm; | |
33 | int rcode = CMD_RET_SUCCESS; | |
c93a1fd4 | 34 | |
61b0cfe9 L |
35 | (void) cmdtp; (void) flag; |
36 | ||
37 | switch (argc) { | |
38 | case 2: /* set date & time */ | |
f14850db L |
39 | /* initialize tm with current time */ |
40 | rcode = rtc_get (&tm); | |
61b0cfe9 | 41 | |
f14850db L |
42 | if(!rcode) { |
43 | /* insert new date & time */ | |
44 | if (mk_date (argv[1], &tm) != 0) { | |
45 | my_puts_P(PSTR("## Bad date format\n")); | |
46 | break; | |
61b0cfe9 | 47 | } |
f14850db L |
48 | /* and write to RTC */ |
49 | rcode = rtc_set (&tm); | |
50 | if(rcode) | |
51 | my_puts_P(PSTR("## Set date failed\n")); | |
52 | } else { | |
53 | my_puts_P(PSTR("## Get date failed\n")); | |
61b0cfe9 L |
54 | } |
55 | /* FALL TROUGH */ | |
56 | case 1: /* get date & time */ | |
57 | rcode = rtc_get (&tm); | |
58 | ||
59 | if (rcode) { | |
60 | my_puts_P(PSTR("## Get date failed\n")); | |
61 | break; | |
62 | } | |
c93a1fd4 L |
63 | printf_P(PSTR("Date: %4d-%02d-%02d ("), |
64 | tm.tm_year, tm.tm_mon, tm.tm_mday); | |
65 | my_puts_P( (tm.tm_wday<0 || tm.tm_wday>6) ? | |
66 | PSTR("unknown ") : weekdays[tm.tm_wday]); | |
67 | printf_P(PSTR("day) Time: %2d:%02d:%02d\n"), | |
61b0cfe9 | 68 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
61b0cfe9 | 69 | break; |
c93a1fd4 | 70 | |
61b0cfe9 L |
71 | default: |
72 | rcode = CMD_RET_USAGE; | |
73 | } | |
74 | ||
75 | return rcode; | |
76 | } | |
77 | ||
78 | /* | |
79 | * simple conversion of two-digit string with error checking | |
80 | */ | |
81 | static int cnvrt2 (const char *str, int *valp) | |
82 | { | |
83 | int val; | |
84 | ||
85 | if ((*str < '0') || (*str > '9')) | |
86 | return (-1); | |
87 | ||
88 | val = *str - '0'; | |
89 | ||
90 | ++str; | |
91 | ||
92 | if ((*str < '0') || (*str > '9')) | |
93 | return (-1); | |
94 | ||
95 | *valp = 10 * val + (*str - '0'); | |
96 | ||
97 | return (0); | |
98 | } | |
99 | ||
100 | /* | |
101 | * Convert date string: MMDDhhmm[[CC]YY][.ss] | |
102 | * | |
103 | * Some basic checking for valid values is done, but this will not catch | |
104 | * all possible error conditions. | |
105 | */ | |
106 | int mk_date (const char *datestr, struct rtc_time *tmp) | |
107 | { | |
108 | int len, val; | |
109 | char *ptr; | |
110 | ||
111 | ptr = strchr (datestr,'.'); | |
112 | len = strlen (datestr); | |
113 | ||
114 | /* Set seconds */ | |
115 | if (ptr) { | |
116 | int sec; | |
117 | ||
118 | *ptr++ = '\0'; | |
119 | if ((len - (ptr - datestr)) != 2) | |
120 | return (-1); | |
121 | ||
122 | len = strlen (datestr); | |
123 | ||
124 | if (cnvrt2 (ptr, &sec)) | |
125 | return (-1); | |
126 | ||
127 | tmp->tm_sec = sec; | |
128 | } else { | |
129 | tmp->tm_sec = 0; | |
130 | } | |
131 | ||
132 | if (len == 12) { /* MMDDhhmmCCYY */ | |
133 | int year, century; | |
134 | ||
135 | if (cnvrt2 (datestr+ 8, ¢ury) || | |
136 | cnvrt2 (datestr+10, &year) ) { | |
137 | return (-1); | |
138 | } | |
139 | tmp->tm_year = 100 * century + year; | |
140 | } else if (len == 10) { /* MMDDhhmmYY */ | |
141 | int year, century; | |
142 | ||
143 | century = tmp->tm_year / 100; | |
144 | if (cnvrt2 (datestr+ 8, &year)) | |
145 | return (-1); | |
146 | tmp->tm_year = 100 * century + year; | |
147 | } | |
148 | ||
149 | switch (len) { | |
150 | case 8: /* MMDDhhmm */ | |
151 | /* fall thru */ | |
152 | case 10: /* MMDDhhmmYY */ | |
153 | /* fall thru */ | |
154 | case 12: /* MMDDhhmmCCYY */ | |
155 | if (cnvrt2 (datestr+0, &val) || | |
156 | val > 12) { | |
157 | break; | |
158 | } | |
159 | tmp->tm_mon = val; | |
160 | if (cnvrt2 (datestr+2, &val) || | |
161 | val > ((tmp->tm_mon==2) ? 29 : 31)) { | |
162 | break; | |
163 | } | |
164 | tmp->tm_mday = val; | |
165 | ||
166 | if (cnvrt2 (datestr+4, &val) || | |
167 | val > 23) { | |
168 | break; | |
169 | } | |
170 | tmp->tm_hour = val; | |
171 | ||
172 | if (cnvrt2 (datestr+6, &val) || | |
173 | val > 59) { | |
174 | break; | |
175 | } | |
176 | tmp->tm_min = val; | |
177 | ||
178 | /* calculate day of week */ | |
179 | GregorianDay (tmp); | |
180 | ||
181 | return (0); | |
182 | default: | |
183 | break; | |
184 | } | |
185 | ||
186 | return (-1); | |
187 | } |