setdate.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Real Time Clock Driver Test
  2. * by: Benjamin Gaignard (benjamin.gaignard@linaro.org)
  3. *
  4. * To build
  5. * gcc rtctest_setdate.c -o rtctest_setdate
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <stdio.h>
  18. #include <linux/rtc.h>
  19. #include <sys/ioctl.h>
  20. #include <sys/time.h>
  21. #include <sys/types.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #include <stdlib.h>
  25. #include <errno.h>
  26. static const char default_time[] = "00:00:00";
  27. int main(int argc, char **argv)
  28. {
  29. int fd, retval;
  30. struct rtc_time new, current;
  31. const char *rtc, *date;
  32. const char *time = default_time;
  33. switch (argc) {
  34. case 4:
  35. time = argv[3];
  36. /* FALLTHROUGH */
  37. case 3:
  38. date = argv[2];
  39. rtc = argv[1];
  40. break;
  41. default:
  42. fprintf(stderr, "usage: rtctest_setdate <rtcdev> <DD-MM-YYYY> [HH:MM:SS]\n");
  43. return 1;
  44. }
  45. fd = open(rtc, O_RDONLY);
  46. if (fd == -1) {
  47. perror(rtc);
  48. exit(errno);
  49. }
  50. sscanf(date, "%d-%d-%d", &new.tm_mday, &new.tm_mon, &new.tm_year);
  51. new.tm_mon -= 1;
  52. new.tm_year -= 1900;
  53. sscanf(time, "%d:%d:%d", &new.tm_hour, &new.tm_min, &new.tm_sec);
  54. fprintf(stderr, "Test will set RTC date/time to %d-%d-%d, %02d:%02d:%02d.\n",
  55. new.tm_mday, new.tm_mon + 1, new.tm_year + 1900,
  56. new.tm_hour, new.tm_min, new.tm_sec);
  57. /* Write the new date in RTC */
  58. retval = ioctl(fd, RTC_SET_TIME, &new);
  59. if (retval == -1) {
  60. perror("RTC_SET_TIME ioctl");
  61. close(fd);
  62. exit(errno);
  63. }
  64. /* Read back */
  65. retval = ioctl(fd, RTC_RD_TIME, &current);
  66. if (retval == -1) {
  67. perror("RTC_RD_TIME ioctl");
  68. exit(errno);
  69. }
  70. fprintf(stderr, "\n\nCurrent RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",
  71. current.tm_mday, current.tm_mon + 1, current.tm_year + 1900,
  72. current.tm_hour, current.tm_min, current.tm_sec);
  73. close(fd);
  74. return 0;
  75. }