]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_date.c
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * SPDX-License-Identifier: GPL-2.0+
11 * RTC, Date & Time support: get and set date & time
21 * simple conversion of two-digit string with error checking
23 static int cnvrt2 (const char *str
, int *valp
)
27 if ((*str
< '0') || (*str
> '9'))
34 if ((*str
< '0') || (*str
> '9'))
37 *valp
= 10 * val
+ (*str
- '0');
43 * Convert date string: MMDDhhmm[[CC]YY][.ss]
45 * Some basic checking for valid values is done, but this will not catch
46 * all possible error conditions.
48 int mk_date (const char *datestr
, struct tm
*tmp
)
53 ptr
= strchr (datestr
,'.');
54 len
= strlen (datestr
);
61 if ((len
- (ptr
- datestr
)) != 2)
64 len
= strlen (datestr
);
66 if (cnvrt2 (ptr
, &sec
))
74 if (len
== 12) { /* MMDDhhmmCCYY */
77 if (cnvrt2 (datestr
+ 8, ¢ury
) ||
78 cnvrt2 (datestr
+10, &year
) ) {
81 tmp
->tm_year
= 100 * century
+ year
- 1900;
82 } else if (len
== 10) { /* MMDDhhmmYY */
85 century
= (tmp
->tm_year
+ 1900) / 100;
86 if (cnvrt2 (datestr
+ 8, &year
))
88 tmp
->tm_year
= 100 * century
+ year
-1900;
92 case 8: /* MMDDhhmm */
94 case 10: /* MMDDhhmmYY */
96 case 12: /* MMDDhhmmCCYY */
97 if (cnvrt2 (datestr
+0, &val
) ||
101 tmp
->tm_mon
= val
- 1;
102 if (cnvrt2 (datestr
+2, &val
) ||
103 val
> ((tmp
->tm_mon
==2-1) ? 29 : 31)) {
108 if (cnvrt2 (datestr
+4, &val
) ||
114 if (cnvrt2 (datestr
+6, &val
) ||
128 command_ret_t
do_date(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
132 int rcode
= CMD_RET_SUCCESS
;
134 (void) cmdtp
; (void) flag
;
137 case 2: /* set date & time */
138 /* initialize t with current time */
139 rcode
= rtc_get (&t
);
142 /* insert new date & time */
143 if (mk_date (argv
[1], &t
) != 0) {
144 my_puts_P(PSTR("## Bad date format\n"));
149 time
= mk_gmtime(&t
);
152 /* and write to RTC */
153 rcode
= rtc_set (&t
);
155 my_puts_P(PSTR("## Set date failed\n"));
157 my_puts_P(PSTR("## Get date failed\n"));
160 case 1: /* get date & time */
161 rcode
= rtc_get (&t
);
164 my_puts_P(PSTR("## Get date failed\n"));
168 printf_P(PSTR("%s\n"), buf
);
172 rcode
= CMD_RET_USAGE
;