]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_date.c
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
9 * RTC, Date & Time support: get and set date & time
13 #include <avr/pgmspace.h>
19 static const char * const weekdays
[] = {
20 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
23 int mk_date (const char *, struct rtc_time
*);
25 command_ret_t
do_date(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
28 int rcode
= CMD_RET_SUCCESS
;
30 (void) cmdtp
; (void) flag
;
33 case 2: /* set date & time */
34 if (strcmp_P(argv
[1],PSTR("reset")) == 0) {
35 my_puts_P(PSTR("Reset RTC...\n"));
38 /* initialize tm with current time */
39 rcode
= rtc_get (&tm
);
42 /* insert new date & time */
43 if (mk_date (argv
[1], &tm
) != 0) {
44 my_puts_P(PSTR("## Bad date format\n"));
47 /* and write to RTC */
48 rcode
= rtc_set (&tm
);
50 my_puts_P(PSTR("## Set date failed\n"));
52 my_puts_P(PSTR("## Get date failed\n"));
56 case 1: /* get date & time */
57 rcode
= rtc_get (&tm
);
60 my_puts_P(PSTR("## Get date failed\n"));
64 printf_P(PSTR("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n"),
65 tm
.tm_year
, tm
.tm_mon
, tm
.tm_mday
,
66 (tm
.tm_wday
<0 || tm
.tm_wday
>6) ?
67 "unknown " : weekdays
[tm
.tm_wday
],
68 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
72 rcode
= CMD_RET_USAGE
;
79 * simple conversion of two-digit string with error checking
81 static int cnvrt2 (const char *str
, int *valp
)
85 if ((*str
< '0') || (*str
> '9'))
92 if ((*str
< '0') || (*str
> '9'))
95 *valp
= 10 * val
+ (*str
- '0');
101 * Convert date string: MMDDhhmm[[CC]YY][.ss]
103 * Some basic checking for valid values is done, but this will not catch
104 * all possible error conditions.
106 int mk_date (const char *datestr
, struct rtc_time
*tmp
)
111 ptr
= strchr (datestr
,'.');
112 len
= strlen (datestr
);
119 if ((len
- (ptr
- datestr
)) != 2)
122 len
= strlen (datestr
);
124 if (cnvrt2 (ptr
, &sec
))
132 if (len
== 12) { /* MMDDhhmmCCYY */
135 if (cnvrt2 (datestr
+ 8, ¢ury
) ||
136 cnvrt2 (datestr
+10, &year
) ) {
139 tmp
->tm_year
= 100 * century
+ year
;
140 } else if (len
== 10) { /* MMDDhhmmYY */
143 century
= tmp
->tm_year
/ 100;
144 if (cnvrt2 (datestr
+ 8, &year
))
146 tmp
->tm_year
= 100 * century
+ year
;
150 case 8: /* MMDDhhmm */
152 case 10: /* MMDDhhmmYY */
154 case 12: /* MMDDhhmmCCYY */
155 if (cnvrt2 (datestr
+0, &val
) ||
160 if (cnvrt2 (datestr
+2, &val
) ||
161 val
> ((tmp
->tm_mon
==2) ? 29 : 31)) {
166 if (cnvrt2 (datestr
+4, &val
) ||
172 if (cnvrt2 (datestr
+6, &val
) ||
178 /* calculate day of week */