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