]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/cmd_date.c
Merge branch 'fatfs-integration' into fatcommands
[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 *
93ea25f2 7 * SPDX-License-Identifier: GPL-2.0
61b0cfe9
L
8 */
9
10/*
11 * RTC, Date & Time support: get and set date & time
12 */
228b1c5f 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
93ea25f2 126command_ret_t do_date(cmd_tbl_t *cmdtp, uint_fast8_t flag, 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
L
132
133 (void) cmdtp; (void) flag;
134
135 switch (argc) {
136 case 2: /* set date & time */
137 /* initialize t with current time */
85046f8c
L
138 if(rtc_get(&t) < 0) {
139 my_puts_P(PSTR("## Get date failed\n"));
140 break;
141 } else {
e63b2f75
L
142 /* insert new date & time */
143 if (mk_date (argv[1], &t) != 0) {
144 my_puts_P(PSTR("## Bad date format\n"));
145 break;
146 }
147
148 time_t time;
149 time = mk_gmtime(&t);
150 gmtime_r(&time, &t);
151
152 /* and write to RTC */
85046f8c 153 if(rtc_set(&t) < 0) {
e63b2f75 154 my_puts_P(PSTR("## Set date failed\n"));
85046f8c
L
155 break;
156 }
e63b2f75
L
157 }
158 /* FALL TROUGH */
159 case 1: /* get date & time */
85046f8c
L
160 rc = rtc_get(&t);
161 if (rc >= 0) {
162 asctime_r(&t, buf);
163 printf_P(PSTR("%s"), buf);
164 if (rc == 1)
165 printf_P(PSTR(" (Invalid)"));
166 putchar('\n');
167 rcode = CMD_RET_SUCCESS;
168 } else {
e63b2f75 169 my_puts_P(PSTR("## Get date failed\n"));
e63b2f75 170 }
e63b2f75
L
171 break;
172
173 default:
174 rcode = CMD_RET_USAGE;
175 }
176
177 return rcode;
178}