]>
git.ipfire.org Git - people/ms/u-boot.git/blob - cmd/date.c
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
9 * RTC, Date & Time support: get and set date & time
17 DECLARE_GLOBAL_DATA_PTR
;
19 static const char * const weekdays
[] = {
20 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
23 #ifdef CONFIG_NEEDS_MANUAL_RELOC
24 #define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
29 int mk_date (const char *, struct rtc_time
*);
31 static struct rtc_time default_tm
= { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
33 static int do_date(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
37 int old_bus __maybe_unused
;
39 /* switch to correct I2C bus */
43 rcode
= uclass_get_device(UCLASS_RTC
, 0, &dev
);
45 printf("Cannot find RTC: err=%d\n", rcode
);
46 return CMD_RET_FAILURE
;
48 #elif defined(CONFIG_SYS_I2C)
49 old_bus
= i2c_get_bus_num();
50 i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM
);
52 old_bus
= I2C_GET_BUS();
53 I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM
);
57 case 2: /* set date & time */
58 if (strcmp(argv
[1],"reset") == 0) {
59 puts ("Reset RTC...\n");
61 rcode
= dm_rtc_reset(dev
);
63 rcode
= dm_rtc_set(dev
, &default_tm
);
66 rcode
= rtc_set(&default_tm
);
69 puts("## Failed to set date after RTC reset\n");
71 /* initialize tm with current time */
73 rcode
= dm_rtc_get(dev
, &tm
);
78 /* insert new date & time */
79 if (mk_date(argv
[1], &tm
) != 0) {
80 puts ("## Bad date format\n");
83 /* and write to RTC */
85 rcode
= dm_rtc_set(dev
, &tm
);
90 printf("## Set date failed: err=%d\n",
94 puts("## Get date failed\n");
98 case 1: /* get date & time */
100 rcode
= dm_rtc_get(dev
, &tm
);
102 rcode
= rtc_get(&tm
);
105 puts("## Get date failed\n");
109 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
110 tm
.tm_year
, tm
.tm_mon
, tm
.tm_mday
,
111 (tm
.tm_wday
<0 || tm
.tm_wday
>6) ?
112 "unknown " : RELOC(weekdays
[tm
.tm_wday
]),
113 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
117 rcode
= CMD_RET_USAGE
;
120 /* switch back to original I2C bus */
121 #ifdef CONFIG_SYS_I2C
122 i2c_set_bus_num(old_bus
);
123 #elif !defined(CONFIG_DM_RTC)
124 I2C_SET_BUS(old_bus
);
127 return rcode
? CMD_RET_FAILURE
: 0;
131 * simple conversion of two-digit string with error checking
133 static int cnvrt2 (const char *str
, int *valp
)
137 if ((*str
< '0') || (*str
> '9'))
144 if ((*str
< '0') || (*str
> '9'))
147 *valp
= 10 * val
+ (*str
- '0');
153 * Convert date string: MMDDhhmm[[CC]YY][.ss]
155 * Some basic checking for valid values is done, but this will not catch
156 * all possible error conditions.
158 int mk_date (const char *datestr
, struct rtc_time
*tmp
)
163 ptr
= strchr (datestr
,'.');
164 len
= strlen (datestr
);
171 if ((len
- (ptr
- datestr
)) != 2)
174 len
= strlen (datestr
);
176 if (cnvrt2 (ptr
, &sec
))
184 if (len
== 12) { /* MMDDhhmmCCYY */
187 if (cnvrt2 (datestr
+ 8, ¢ury
) ||
188 cnvrt2 (datestr
+10, &year
) ) {
191 tmp
->tm_year
= 100 * century
+ year
;
192 } else if (len
== 10) { /* MMDDhhmmYY */
195 century
= tmp
->tm_year
/ 100;
196 if (cnvrt2 (datestr
+ 8, &year
))
198 tmp
->tm_year
= 100 * century
+ year
;
202 case 8: /* MMDDhhmm */
204 case 10: /* MMDDhhmmYY */
206 case 12: /* MMDDhhmmCCYY */
207 if (cnvrt2 (datestr
+0, &val
) ||
212 if (cnvrt2 (datestr
+2, &val
) ||
213 val
> ((tmp
->tm_mon
==2) ? 29 : 31)) {
218 if (cnvrt2 (datestr
+4, &val
) ||
224 if (cnvrt2 (datestr
+6, &val
) ||
230 /* calculate day of week */
231 rtc_calc_weekday(tmp
);
241 /***************************************************/
245 "get/set/reset date & time",
246 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
247 " - without arguments: print date & time\n"
248 " - with numeric argument: set the system date & time\n"
249 " - with 'reset' argument: reset the RTC"