]>
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
18 static const char * const weekdays
[] = {
19 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
22 int mk_date (const char *, struct rtc_time
*);
24 command_ret_t
do_date(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
27 int rcode
= CMD_RET_SUCCESS
;
29 (void) cmdtp
; (void) flag
;
32 case 2: /* set date & time */
33 /* initialize tm with current time */
34 rcode
= rtc_get (&tm
);
37 /* insert new date & time */
38 if (mk_date (argv
[1], &tm
) != 0) {
39 my_puts_P(PSTR("## Bad date format\n"));
42 /* and write to RTC */
43 rcode
= rtc_set (&tm
);
45 my_puts_P(PSTR("## Set date failed\n"));
47 my_puts_P(PSTR("## Get date failed\n"));
50 case 1: /* get date & time */
51 rcode
= rtc_get (&tm
);
54 my_puts_P(PSTR("## Get date failed\n"));
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
);
66 rcode
= CMD_RET_USAGE
;
73 * simple conversion of two-digit string with error checking
75 static int cnvrt2 (const char *str
, int *valp
)
79 if ((*str
< '0') || (*str
> '9'))
86 if ((*str
< '0') || (*str
> '9'))
89 *valp
= 10 * val
+ (*str
- '0');
95 * Convert date string: MMDDhhmm[[CC]YY][.ss]
97 * Some basic checking for valid values is done, but this will not catch
98 * all possible error conditions.
100 int mk_date (const char *datestr
, struct rtc_time
*tmp
)
105 ptr
= strchr (datestr
,'.');
106 len
= strlen (datestr
);
113 if ((len
- (ptr
- datestr
)) != 2)
116 len
= strlen (datestr
);
118 if (cnvrt2 (ptr
, &sec
))
126 if (len
== 12) { /* MMDDhhmmCCYY */
129 if (cnvrt2 (datestr
+ 8, ¢ury
) ||
130 cnvrt2 (datestr
+10, &year
) ) {
133 tmp
->tm_year
= 100 * century
+ year
;
134 } else if (len
== 10) { /* MMDDhhmmYY */
137 century
= tmp
->tm_year
/ 100;
138 if (cnvrt2 (datestr
+ 8, &year
))
140 tmp
->tm_year
= 100 * century
+ year
;
144 case 8: /* MMDDhhmm */
146 case 10: /* MMDDhhmmYY */
148 case 12: /* MMDDhhmmCCYY */
149 if (cnvrt2 (datestr
+0, &val
) ||
154 if (cnvrt2 (datestr
+2, &val
) ||
155 val
> ((tmp
->tm_mon
==2) ? 29 : 31)) {
160 if (cnvrt2 (datestr
+4, &val
) ||
166 if (cnvrt2 (datestr
+6, &val
) ||
172 /* calculate day of week */