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