wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * RTC, Date & Time support: get and set date & time |
| 26 | */ |
| 27 | #include <common.h> |
| 28 | #include <command.h> |
| 29 | #include <rtc.h> |
| 30 | |
| 31 | #if (CONFIG_COMMANDS & CFG_CMD_DATE) |
| 32 | |
| 33 | const char *weekdays[] = { |
| 34 | "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur", |
| 35 | }; |
| 36 | |
| 37 | int mk_date (char *, struct rtc_time *); |
| 38 | |
| 39 | int do_date (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 40 | { |
| 41 | struct rtc_time tm; |
| 42 | int rcode = 0; |
| 43 | |
| 44 | switch (argc) { |
| 45 | case 2: /* set date & time */ |
| 46 | if (strcmp(argv[1],"reset") == 0) { |
| 47 | printf ("Reset RTC...\n"); |
| 48 | rtc_reset (); |
| 49 | } else { |
| 50 | /* initialize tm with current time */ |
| 51 | rtc_get (&tm); |
| 52 | /* insert new date & time */ |
| 53 | if (mk_date (argv[1], &tm) != 0) { |
| 54 | printf ("## Bad date format\n"); |
| 55 | return 1; |
| 56 | } |
| 57 | /* and write to RTC */ |
| 58 | rtc_set (&tm); |
| 59 | } |
| 60 | /* FALL TROUGH */ |
| 61 | case 1: /* get date & time */ |
| 62 | rtc_get (&tm); |
| 63 | |
| 64 | printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n", |
| 65 | tm.tm_year, tm.tm_mon, tm.tm_mday, |
| 66 | (tm.tm_wday<0 || tm.tm_wday>6) ? |
| 67 | "unknown " : weekdays[tm.tm_wday], |
| 68 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 69 | |
| 70 | return 0; |
| 71 | default: |
| 72 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 73 | rcode = 1; |
| 74 | } |
| 75 | return rcode; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * simple conversion of two-digit string with error checking |
| 80 | */ |
| 81 | static int cnvrt2 (char *str, int *valp) |
| 82 | { |
| 83 | int val; |
| 84 | |
| 85 | if ((*str < '0') || (*str > '9')) |
| 86 | return (-1); |
| 87 | |
| 88 | val = *str - '0'; |
| 89 | |
| 90 | ++str; |
| 91 | |
| 92 | if ((*str < '0') || (*str > '9')) |
| 93 | return (-1); |
| 94 | |
| 95 | *valp = 10 * val + (*str - '0'); |
| 96 | |
| 97 | return (0); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Convert date string: MMDDhhmm[[CC]YY][.ss] |
| 102 | * |
| 103 | * Some basic checking for valid values is done, but this will not catch |
| 104 | * all possible error conditions. |
| 105 | */ |
| 106 | int mk_date (char *datestr, struct rtc_time *tmp) |
| 107 | { |
| 108 | int len, val; |
| 109 | char *ptr; |
| 110 | |
| 111 | ptr = strchr (datestr,'.'); |
| 112 | len = strlen (datestr); |
| 113 | |
| 114 | /* Set seconds */ |
| 115 | if (ptr) { |
| 116 | int sec; |
| 117 | |
| 118 | *ptr++ = '\0'; |
| 119 | if ((len - (ptr - datestr)) != 2) |
| 120 | return (-1); |
| 121 | |
| 122 | len = strlen (datestr); |
| 123 | |
| 124 | if (cnvrt2 (ptr, &sec)) |
| 125 | return (-1); |
| 126 | |
| 127 | tmp->tm_sec = sec; |
| 128 | } else { |
| 129 | tmp->tm_sec = 0; |
| 130 | } |
| 131 | |
| 132 | if (len == 12) { /* MMDDhhmmCCYY */ |
| 133 | int year, century; |
| 134 | |
| 135 | if (cnvrt2 (datestr+ 8, ¢ury) || |
| 136 | cnvrt2 (datestr+10, &year) ) { |
| 137 | return (-1); |
| 138 | } |
| 139 | tmp->tm_year = 100 * century + year; |
| 140 | } else if (len == 10) { /* MMDDhhmmYY */ |
| 141 | int year, century; |
| 142 | |
| 143 | century = tmp->tm_year / 100; |
| 144 | if (cnvrt2 (datestr+ 8, &year)) |
| 145 | return (-1); |
| 146 | tmp->tm_year = 100 * century + year; |
| 147 | } |
| 148 | |
| 149 | switch (len) { |
| 150 | case 8: /* MMDDhhmm */ |
| 151 | /* fall thru */ |
| 152 | case 10: /* MMDDhhmmYY */ |
| 153 | /* fall thru */ |
| 154 | case 12: /* MMDDhhmmCCYY */ |
| 155 | if (cnvrt2 (datestr+0, &val) || |
| 156 | val > 12) { |
| 157 | break; |
| 158 | } |
| 159 | tmp->tm_mon = val; |
| 160 | if (cnvrt2 (datestr+2, &val) || |
| 161 | val > ((tmp->tm_mon==2) ? 29 : 31)) { |
| 162 | break; |
| 163 | } |
| 164 | tmp->tm_mday = val; |
| 165 | |
| 166 | if (cnvrt2 (datestr+4, &val) || |
| 167 | val > 23) { |
| 168 | break; |
| 169 | } |
| 170 | tmp->tm_hour = val; |
| 171 | |
| 172 | if (cnvrt2 (datestr+6, &val) || |
| 173 | val > 59) { |
| 174 | break; |
| 175 | } |
| 176 | tmp->tm_min = val; |
| 177 | |
| 178 | /* calculate day of week */ |
| 179 | GregorianDay (tmp); |
| 180 | |
| 181 | return (0); |
| 182 | default: |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | return (-1); |
| 187 | } |
| 188 | |
| 189 | #endif /* CFG_CMD_DATE */ |