1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * RTC, Date & Time support: get and set date & time
14 #include <asm/global_data.h>
16 DECLARE_GLOBAL_DATA_PTR
;
18 static const char * const weekdays
[] = {
19 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
22 int mk_date (const char *, struct rtc_time
*);
24 static struct rtc_time default_tm
= { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
26 static int do_date(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
31 int old_bus __maybe_unused
;
33 /* switch to correct I2C bus */
36 rcode
= uclass_get_device_by_seq(UCLASS_RTC
, 0, &dev
);
38 rcode
= uclass_get_device(UCLASS_RTC
, 0, &dev
);
40 printf("Cannot find RTC: err=%d\n", rcode
);
41 return CMD_RET_FAILURE
;
46 case 2: /* set date & time */
47 if (strcmp(argv
[1],"reset") == 0) {
48 puts ("Reset RTC...\n");
49 rcode
= dm_rtc_reset(dev
);
51 rcode
= dm_rtc_set(dev
, &default_tm
);
53 puts("## Failed to set date after RTC reset\n");
55 /* initialize tm with current time */
56 rcode
= dm_rtc_get(dev
, &tm
);
58 /* insert new date & time */
59 if (mk_date(argv
[1], &tm
) != 0) {
60 puts ("## Bad date format\n");
63 /* and write to RTC */
64 rcode
= dm_rtc_set(dev
, &tm
);
66 printf("## Set date failed: err=%d\n",
70 puts("## Get date failed\n");
74 case 1: /* get date & time */
75 rcode
= dm_rtc_get(dev
, &tm
);
77 puts("## Get date failed\n");
81 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
82 tm
.tm_year
, tm
.tm_mon
, tm
.tm_mday
,
83 (tm
.tm_wday
<0 || tm
.tm_wday
>6) ?
84 "unknown " : weekdays
[tm
.tm_wday
],
85 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
89 rcode
= CMD_RET_USAGE
;
92 return rcode
? CMD_RET_FAILURE
: 0;
96 * simple conversion of two-digit string with error checking
98 static int cnvrt2 (const char *str
, int *valp
)
102 if ((*str
< '0') || (*str
> '9'))
109 if ((*str
< '0') || (*str
> '9'))
112 *valp
= 10 * val
+ (*str
- '0');
118 * Convert date string: MMDDhhmm[[CC]YY][.ss]
120 * Some basic checking for valid values is done, but this will not catch
121 * all possible error conditions.
123 int mk_date (const char *datestr
, struct rtc_time
*tmp
)
128 ptr
= strchr(datestr
, '.');
129 len
= strlen(datestr
);
136 if ((len
- (ptr
- datestr
)) != 2)
141 if (cnvrt2 (ptr
, &sec
))
149 if (len
== 12) { /* MMDDhhmmCCYY */
152 if (cnvrt2 (datestr
+ 8, ¢ury
) ||
153 cnvrt2 (datestr
+10, &year
) ) {
156 tmp
->tm_year
= 100 * century
+ year
;
157 } else if (len
== 10) { /* MMDDhhmmYY */
160 century
= tmp
->tm_year
/ 100;
161 if (cnvrt2 (datestr
+ 8, &year
))
163 tmp
->tm_year
= 100 * century
+ year
;
167 case 8: /* MMDDhhmm */
169 case 10: /* MMDDhhmmYY */
171 case 12: /* MMDDhhmmCCYY */
172 if (cnvrt2 (datestr
+0, &val
) ||
177 if (cnvrt2 (datestr
+2, &val
) ||
178 val
> ((tmp
->tm_mon
==2) ? 29 : 31)) {
183 if (cnvrt2 (datestr
+4, &val
) ||
189 if (cnvrt2 (datestr
+6, &val
) ||
195 /* calculate day of week */
196 rtc_calc_weekday(tmp
);
206 /***************************************************/
210 "get/set/reset date & time",
211 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
212 " - without arguments: print date & time\n"
213 " - with numeric argument: set the system date & time\n"
214 " - with 'reset' argument: reset the RTC"