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