Blame view

common/cmd_date.c 4.05 KB
3863585bb   wdenk   Initial revision
1
2
3
4
  /*
   * (C) Copyright 2001
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
5
   * SPDX-License-Identifier:	GPL-2.0+
3863585bb   wdenk   Initial revision
6
7
8
9
10
11
12
13
   */
  
  /*
   * RTC, Date & Time support: get and set date & time
   */
  #include <common.h>
  #include <command.h>
  #include <rtc.h>
0dc018ece   Stefan Roese   [PATCH] I2C: Add ...
14
  #include <i2c.h>
3863585bb   wdenk   Initial revision
15

d87080b72   Wolfgang Denk   GCC-4.x fixes: cl...
16
  DECLARE_GLOBAL_DATA_PTR;
bdbc1303c   Mike Frysinger   cmd_date: constify
17
  static const char * const weekdays[] = {
3863585bb   wdenk   Initial revision
18
19
  	"Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
  };
2e5167cca   Wolfgang Denk   Replace CONFIG_RE...
20
  #ifdef CONFIG_NEEDS_MANUAL_RELOC
3e38691e8   wdenk   * Patch by Arun D...
21
  #define RELOC(a)	((typeof(a))((unsigned long)(a) + gd->reloc_off))
2e5167cca   Wolfgang Denk   Replace CONFIG_RE...
22
23
  #else
  #define RELOC(a)	a
521af04d8   Peter Tyser   Conditionally per...
24
  #endif
3e38691e8   wdenk   * Patch by Arun D...
25

bdbc1303c   Mike Frysinger   cmd_date: constify
26
  int mk_date (const char *, struct rtc_time *);
3863585bb   wdenk   Initial revision
27

088f1b199   Kim Phillips   common/cmd_*.c: s...
28
  static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585bb   wdenk   Initial revision
29
30
31
  {
  	struct rtc_time tm;
  	int rcode = 0;
0dc018ece   Stefan Roese   [PATCH] I2C: Add ...
32
33
34
  	int old_bus;
  
  	/* switch to correct I2C bus */
3f4978c71   Heiko Schocher   i2c: common chang...
35
36
37
38
  #ifdef CONFIG_SYS_I2C
  	old_bus = i2c_get_bus_num();
  	i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
  #else
0dc018ece   Stefan Roese   [PATCH] I2C: Add ...
39
  	old_bus = I2C_GET_BUS();
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
40
  	I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
3f4978c71   Heiko Schocher   i2c: common chang...
41
  #endif
3863585bb   wdenk   Initial revision
42
43
44
45
  
  	switch (argc) {
  	case 2:			/* set date & time */
  		if (strcmp(argv[1],"reset") == 0) {
4b9206ed5   wdenk   * Patches by Thom...
46
47
  			puts ("Reset RTC...
  ");
3863585bb   wdenk   Initial revision
48
49
50
  			rtc_reset ();
  		} else {
  			/* initialize tm with current time */
d1e231941   Jean-Christophe PLAGNIOL-VILLARD   rtc: allow rtc_se...
51
52
53
54
55
56
57
58
59
60
61
62
  			rcode = rtc_get (&tm);
  
  			if(!rcode) {
  				/* insert new date & time */
  				if (mk_date (argv[1], &tm) != 0) {
  					puts ("## Bad date format
  ");
  					break;
  				}
  				/* and write to RTC */
  				rcode = rtc_set (&tm);
  				if(rcode)
d52e3e017   Magnus Lilja   cmd_date: Fix spe...
63
64
  					puts("## Set date failed
  ");
d1e231941   Jean-Christophe PLAGNIOL-VILLARD   rtc: allow rtc_se...
65
  			} else {
d52e3e017   Magnus Lilja   cmd_date: Fix spe...
66
67
  				puts("## Get date failed
  ");
3863585bb   wdenk   Initial revision
68
  			}
3863585bb   wdenk   Initial revision
69
70
71
  		}
  		/* FALL TROUGH */
  	case 1:			/* get date & time */
d1e231941   Jean-Christophe PLAGNIOL-VILLARD   rtc: allow rtc_se...
72
73
74
  		rcode = rtc_get (&tm);
  
  		if (rcode) {
d52e3e017   Magnus Lilja   cmd_date: Fix spe...
75
76
  			puts("## Get date failed
  ");
d1e231941   Jean-Christophe PLAGNIOL-VILLARD   rtc: allow rtc_se...
77
78
  			break;
  		}
3863585bb   wdenk   Initial revision
79
80
81
82
83
  
  		printf ("Date: %4d-%02d-%02d (%sday)    Time: %2d:%02d:%02d
  ",
  			tm.tm_year, tm.tm_mon, tm.tm_mday,
  			(tm.tm_wday<0 || tm.tm_wday>6) ?
3e38691e8   wdenk   * Patch by Arun D...
84
  				"unknown " : RELOC(weekdays[tm.tm_wday]),
3863585bb   wdenk   Initial revision
85
  			tm.tm_hour, tm.tm_min, tm.tm_sec);
0dc018ece   Stefan Roese   [PATCH] I2C: Add ...
86
  		break;
3863585bb   wdenk   Initial revision
87
  	default:
4c12eeb8b   Simon Glass   Convert cmd_usage...
88
  		rcode = CMD_RET_USAGE;
3863585bb   wdenk   Initial revision
89
  	}
0dc018ece   Stefan Roese   [PATCH] I2C: Add ...
90
91
  
  	/* switch back to original I2C bus */
3f4978c71   Heiko Schocher   i2c: common chang...
92
93
94
  #ifdef CONFIG_SYS_I2C
  	i2c_set_bus_num(old_bus);
  #else
0dc018ece   Stefan Roese   [PATCH] I2C: Add ...
95
  	I2C_SET_BUS(old_bus);
3f4978c71   Heiko Schocher   i2c: common chang...
96
  #endif
0dc018ece   Stefan Roese   [PATCH] I2C: Add ...
97

3863585bb   wdenk   Initial revision
98
99
100
101
102
103
  	return rcode;
  }
  
  /*
   * simple conversion of two-digit string with error checking
   */
bdbc1303c   Mike Frysinger   cmd_date: constify
104
  static int cnvrt2 (const char *str, int *valp)
3863585bb   wdenk   Initial revision
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  {
  	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.
   */
bdbc1303c   Mike Frysinger   cmd_date: constify
129
  int mk_date (const char *datestr, struct rtc_time *tmp)
3863585bb   wdenk   Initial revision
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  {
  	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;
  	} else if (len == 10) {		/* MMDDhhmmYY	*/
  		int year, century;
  
  		century = tmp->tm_year / 100;
  		if (cnvrt2 (datestr+ 8, &year))
  			return (-1);
  		tmp->tm_year = 100 * century + year;
  	}
  
  	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;
  		if (cnvrt2 (datestr+2, &val) ||
  		    val > ((tmp->tm_mon==2) ? 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;
  
  		/* calculate day of week */
  		GregorianDay (tmp);
  
  		return (0);
  	default:
  		break;
  	}
  
  	return (-1);
  }
8bde7f776   wdenk   * Code cleanup:
211
  /***************************************************/
0d4983930   wdenk   Patch by Kenneth ...
212
213
  U_BOOT_CMD(
  	date,	2,	1,	do_date,
2fb2604d5   Peter Tyser   Command usage cle...
214
  	"get/set/reset date & time",
8bde7f776   wdenk   * Code cleanup:
215
216
217
218
219
220
221
  	"[MMDDhhmm[[CC]YY][.ss]]
  date reset
  "
  	"  - without arguments: print date & time
  "
  	"  - with numeric argument: set the system date & time
  "
a89c33db9   Wolfgang Denk   General help mess...
222
  	"  - with 'reset' argument: reset the RTC"
8bde7f776   wdenk   * Code cleanup:
223
  );