cmd_date.c 3.9 KB

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