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