]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/cmd_date.c
switch to avr-libc 1.8.1 time library
[z180-stamp.git] / avr / cmd_date.c
CommitLineData
61b0cfe9
L
1/*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8/*
9 * RTC, Date & Time support: get and set date & time
10 */
e63b2f75 11#include "common.h"
61b0cfe9 12#include <string.h>
e63b2f75
L
13#include "time.h"
14#include "rtc.h"
15#include "command.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
L
125
126command_ret_t do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
127{
128 struct tm t;
129 char buf[30];
130 int rcode = CMD_RET_SUCCESS;
131
132 (void) cmdtp; (void) flag;
133
134 switch (argc) {
135 case 2: /* set date & time */
136 /* initialize t with current time */
137 rcode = rtc_get (&t);
138
139 if(!rcode) {
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 */
151 rcode = rtc_set (&t);
152 if(rcode)
153 my_puts_P(PSTR("## Set date failed\n"));
154 } else {
155 my_puts_P(PSTR("## Get date failed\n"));
156 }
157 /* FALL TROUGH */
158 case 1: /* get date & time */
159 rcode = rtc_get (&t);
160
161 if (rcode) {
162 my_puts_P(PSTR("## Get date failed\n"));
163 break;
164 }
165 asctime_r(&t, buf);
166 printf_P(PSTR("%s\n"), buf);
167 break;
168
169 default:
170 rcode = CMD_RET_USAGE;
171 }
172
173 return rcode;
174}