hwclock.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* hwclock.c - get and set the hwclock
  2. *
  3. * Copyright 2014 Bilal Qureshi <bilal.jmi@gmail.com>
  4. *
  5. * No standard, but see Documentation/rtc.txt in the linux kernel source..
  6. *
  7. USE_HWCLOCK(NEWTOY(hwclock, ">0(fast)f(rtc):u(utc)l(localtime)t(systz)s(hctosys)r(show)w(systohc)[-ul][!rtsw]", TOYFLAG_SBIN))
  8. config HWCLOCK
  9. bool "hwclock"
  10. default y
  11. help
  12. usage: hwclock [-rswtluf]
  13. Get/set the hardware clock.
  14. -f FILE Use specified device file instead of /dev/rtc0 (--rtc)
  15. -l Hardware clock uses localtime (--localtime)
  16. -r Show hardware clock time (--show)
  17. -s Set system time from hardware clock (--hctosys)
  18. -t Set the system time based on the current timezone (--systz)
  19. -u Hardware clock uses UTC (--utc)
  20. -w Set hardware clock from system time (--systohc)
  21. */
  22. #define FOR_hwclock
  23. #include "toys.h"
  24. #include <linux/rtc.h>
  25. GLOBALS(
  26. char *f;
  27. )
  28. void hwclock_main()
  29. {
  30. struct timezone tzone;
  31. struct timeval timeval;
  32. struct tm tm;
  33. int fd = -1, utc;
  34. if (FLAG(u)) utc = 1;
  35. else if (FLAG(l)) utc = 0;
  36. else utc = !readfile("/etc/adjtime", toybuf, sizeof(toybuf)) ||
  37. !!strstr(toybuf, "UTC");
  38. if (!FLAG(t)) {
  39. if (!TT.f) TT.f = "/dev/rtc0";
  40. fd = xopen(TT.f, O_WRONLY*FLAG(w));
  41. // Get current time in seconds from rtc device. todo: get subsecond time
  42. if (!FLAG(w)) {
  43. xioctl(fd, RTC_RD_TIME, &tm);
  44. timeval.tv_sec = xmktime(&tm, utc);
  45. timeval.tv_usec = 0; // todo: fixit
  46. }
  47. }
  48. if (FLAG(w) || FLAG(t)) {
  49. if (gettimeofday(&timeval, 0)) perror_exit("gettimeofday failed");
  50. if (!(utc ? gmtime_r : localtime_r)(&timeval.tv_sec, &tm))
  51. error_exit(utc ? "gmtime_r failed" : "localtime_r failed");
  52. }
  53. if (FLAG(w)) {
  54. /* The value of tm_isdst is positive if daylight saving time is in effect,
  55. * zero if it is not and negative if the information is not available.
  56. * todo: so why isn't this negative...? */
  57. tm.tm_isdst = 0;
  58. xioctl(fd, RTC_SET_TIME, &tm);
  59. } else if (FLAG(s)) {
  60. tzone.tz_minuteswest = timezone / 60 - 60 * daylight;
  61. } else if (FLAG(t)) {
  62. // Adjust seconds for timezone and daylight saving time
  63. // extern long timezone is defined in header sys/time.h
  64. tzone.tz_minuteswest = timezone / 60;
  65. if (tm.tm_isdst) tzone.tz_minuteswest -= 60;
  66. if (!utc) timeval.tv_sec += tzone.tz_minuteswest * 60;
  67. } else {
  68. strftime(toybuf, sizeof(toybuf), "%F %T%z", &tm);
  69. xputs(toybuf);
  70. }
  71. if (FLAG(t) || FLAG(s)) {
  72. tzone.tz_dsttime = 0;
  73. if (settimeofday(&timeval, &tzone)) perror_exit("settimeofday failed");
  74. }
  75. xclose(fd);
  76. }