summaryrefslogtreecommitdiff
path: root/avr/cmd_date.c
blob: 3e2e01680513df316623da108b4112761cf67a29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
 *
 * (C) Copyright 2001
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

/*
 * RTC, Date & Time support: get and set date & time
 */
#include "common.h"
#include <string.h>
#include "time.h"
#include "rtc.h"
#include "command.h"


/*
 * simple conversion of two-digit string with error checking
 */
static int cnvrt2 (const char *str, int *valp)
{
	int val;

	if ((*str < '0') || (*str > '9'))
		return (-1);

	val = *str - '0';

	++str;

	if ((*str < '0') || (*str > '9'))
		return (-1);

	*valp = 10 * val + (*str - '0');

	return (0);
}

/*
 * Convert date string: MMDDhhmm[[CC]YY][.ss]
 *
 * Some basic checking for valid values is done, but this will not catch
 * all possible error conditions.
 */
int mk_date (const char *datestr, struct tm *tmp)
{
	int len, val;
	char *ptr;

	ptr = strchr (datestr,'.');
	len = strlen (datestr);

	/* Set seconds */
	if (ptr) {
		int sec;

		*ptr++ = '\0';
		if ((len - (ptr - datestr)) != 2)
			return (-1);

		len = strlen (datestr);

		if (cnvrt2 (ptr, &sec))
			return (-1);

		tmp->tm_sec = sec;
	} else {
		tmp->tm_sec = 0;
	}

	if (len == 12) {			/* MMDDhhmmCCYY	*/
		int year, century;

		if (cnvrt2 (datestr+ 8, &century) ||
		    cnvrt2 (datestr+10, &year) ) {
			return (-1);
		}
		tmp->tm_year = 100 * century + year - 1900;
	} else if (len == 10) {		/* MMDDhhmmYY	*/
		int year, century;

		century = (tmp->tm_year + 1900) / 100;
		if (cnvrt2 (datestr+ 8, &year))
			return (-1);
		tmp->tm_year = 100 * century + year -1900;
	}

	switch (len) {
	case 8:					/* MMDDhhmm	*/
		/* fall thru */
	case 10:				/* MMDDhhmmYY	*/
		/* fall thru */
	case 12:				/* MMDDhhmmCCYY	*/
		if (cnvrt2 (datestr+0, &val) ||
		    val > 12) {
			break;
		}
		tmp->tm_mon  = val - 1;
		if (cnvrt2 (datestr+2, &val) ||
		    val > ((tmp->tm_mon==2-1) ? 29 : 31)) {
			break;
		}
		tmp->tm_mday = val;

		if (cnvrt2 (datestr+4, &val) ||
		    val > 23) {
			break;
		}
		tmp->tm_hour = val;

		if (cnvrt2 (datestr+6, &val) ||
		    val > 59) {
			break;
		}
		tmp->tm_min  = val;

		return (0);
	default:
		break;
	}

	return (-1);
}

command_ret_t do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	struct tm t;
	char buf[30];
	int rc;
	command_ret_t rcode = CMD_RET_FAILURE;

	(void) cmdtp; (void) flag;

	switch (argc) {
	case 2:			/* set date & time */
		/* initialize t with current time */
		if(rtc_get(&t) < 0) {
			my_puts_P(PSTR("## Get date failed\n"));
			break;
		} else {
			/* insert new date & time */
			if (mk_date (argv[1], &t) != 0) {
				my_puts_P(PSTR("## Bad date format\n"));
				break;
			}

			time_t time;
			time = mk_gmtime(&t);
			gmtime_r(&time, &t);

			/* and write to RTC */
			if(rtc_set(&t) < 0) {
				my_puts_P(PSTR("## Set date failed\n"));
				break;
			}
		}
		/* FALL TROUGH */
	case 1:			/* get date & time */
		rc = rtc_get(&t);
		if (rc >= 0) {
			asctime_r(&t, buf);
			printf_P(PSTR("%s"), buf);
			if (rc == 1)
				printf_P(PSTR("  (Invalid)"));
			putchar('\n');
			rcode = CMD_RET_SUCCESS;
		} else {
			my_puts_P(PSTR("## Get date failed\n"));
		}
		break;

	default:
		rcode = CMD_RET_USAGE;
	}

	return rcode;
}