cmd_date.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. * RTC, Date & Time support: get and set date & time
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <rtc.h>
  29. #if (CONFIG_COMMANDS & CFG_CMD_DATE)
  30. const char *weekdays[] = {
  31. "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
  32. };
  33. int mk_date (char *, struct rtc_time *);
  34. int do_date (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  35. {
  36. struct rtc_time tm;
  37. int rcode = 0;
  38. switch (argc) {
  39. case 2: /* set date & time */
  40. if (strcmp(argv[1],"reset") == 0) {
  41. printf ("Reset RTC...\n");
  42. rtc_reset ();
  43. } else {
  44. /* initialize tm with current time */
  45. rtc_get (&tm);
  46. /* insert new date & time */
  47. if (mk_date (argv[1], &tm) != 0) {
  48. printf ("## Bad date format\n");
  49. return 1;
  50. }
  51. /* and write to RTC */
  52. rtc_set (&tm);
  53. }
  54. /* FALL TROUGH */
  55. case 1: /* get date & time */
  56. rtc_get (&tm);
  57. printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
  58. tm.tm_year, tm.tm_mon, tm.tm_mday,
  59. (tm.tm_wday<0 || tm.tm_wday>6) ?
  60. "unknown " : weekdays[tm.tm_wday],
  61. tm.tm_hour, tm.tm_min, tm.tm_sec);
  62. return 0;
  63. default:
  64. printf ("Usage:\n%s\n", cmdtp->usage);
  65. rcode = 1;
  66. }
  67. return rcode;
  68. }
  69. /*
  70. * simple conversion of two-digit string with error checking
  71. */
  72. static int cnvrt2 (char *str, int *valp)
  73. {
  74. int val;
  75. if ((*str < '0') || (*str > '9'))
  76. return (-1);
  77. val = *str - '0';
  78. ++str;
  79. if ((*str < '0') || (*str > '9'))
  80. return (-1);
  81. *valp = 10 * val + (*str - '0');
  82. return (0);
  83. }
  84. /*
  85. * Convert date string: MMDDhhmm[[CC]YY][.ss]
  86. *
  87. * Some basic checking for valid values is done, but this will not catch
  88. * all possible error conditions.
  89. */
  90. int mk_date (char *datestr, struct rtc_time *tmp)
  91. {
  92. int len, val;
  93. char *ptr;
  94. ptr = strchr (datestr,'.');
  95. len = strlen (datestr);
  96. /* Set seconds */
  97. if (ptr) {
  98. int sec;
  99. *ptr++ = '\0';
  100. if ((len - (ptr - datestr)) != 2)
  101. return (-1);
  102. len = strlen (datestr);
  103. if (cnvrt2 (ptr, &sec))
  104. return (-1);
  105. tmp->tm_sec = sec;
  106. } else {
  107. tmp->tm_sec = 0;
  108. }
  109. if (len == 12) { /* MMDDhhmmCCYY */
  110. int year, century;
  111. if (cnvrt2 (datestr+ 8, &century) ||
  112. cnvrt2 (datestr+10, &year) ) {
  113. return (-1);
  114. }
  115. tmp->tm_year = 100 * century + year;
  116. } else if (len == 10) { /* MMDDhhmmYY */
  117. int year, century;
  118. century = tmp->tm_year / 100;
  119. if (cnvrt2 (datestr+ 8, &year))
  120. return (-1);
  121. tmp->tm_year = 100 * century + year;
  122. }
  123. switch (len) {
  124. case 8: /* MMDDhhmm */
  125. /* fall thru */
  126. case 10: /* MMDDhhmmYY */
  127. /* fall thru */
  128. case 12: /* MMDDhhmmCCYY */
  129. if (cnvrt2 (datestr+0, &val) ||
  130. val > 12) {
  131. break;
  132. }
  133. tmp->tm_mon = val;
  134. if (cnvrt2 (datestr+2, &val) ||
  135. val > ((tmp->tm_mon==2) ? 29 : 31)) {
  136. break;
  137. }
  138. tmp->tm_mday = val;
  139. if (cnvrt2 (datestr+4, &val) ||
  140. val > 23) {
  141. break;
  142. }
  143. tmp->tm_hour = val;
  144. if (cnvrt2 (datestr+6, &val) ||
  145. val > 59) {
  146. break;
  147. }
  148. tmp->tm_min = val;
  149. /* calculate day of week */
  150. GregorianDay (tmp);
  151. return (0);
  152. default:
  153. break;
  154. }
  155. return (-1);
  156. }
  157. #endif /* CFG_CMD_DATE */