aboutsummaryrefslogtreecommitdiff
path: root/toys/other/hwclock.c
blob: ba28cc3dcfd19647db63298e1e04fa35cd9e542f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* hwclock.c - get and set the hwclock
 *
 * Copyright 2014 Bilal Qureshi <bilal.jmi@gmail.com>
 *
 * No standard, but see Documentation/rtc.txt in the linux kernel source..
 *
USE_HWCLOCK(NEWTOY(hwclock, ">0(fast)f(rtc):u(utc)l(localtime)t(systz)s(hctosys)r(show)w(systohc)[-ul][!rtsw]", TOYFLAG_SBIN))

config HWCLOCK
  bool "hwclock"
  default y
  help
    usage: hwclock [-rswtluf]

    Get/set the hardware clock.

    -f FILE	Use specified device file instead of /dev/rtc0 (--rtc)
    -l	Hardware clock uses localtime (--localtime)
    -r	Show hardware clock time (--show)
    -s	Set system time from hardware clock (--hctosys)
    -t	Set the system time based on the current timezone (--systz)
    -u	Hardware clock uses UTC (--utc)
    -w	Set hardware clock from system time (--systohc)
*/

#define FOR_hwclock
#include "toys.h"
#include <linux/rtc.h>

GLOBALS(
  char *f;
)

void hwclock_main()
{
  struct timezone tzone;
  struct timeval timeval;
  struct tm tm;
  int fd = -1, utc;

  if (FLAG(u)) utc = 1;
  else if (FLAG(l)) utc = 0;
  else {
    xreadfile("/etc/adjtime", toybuf, sizeof(toybuf));
    utc = !!strstr(toybuf, "UTC");
  }

  if (!FLAG(t)) {
    if (!TT.f) TT.f = "/dev/rtc0";
    fd = xopen(TT.f, O_WRONLY*FLAG(w));

    // Get current time in seconds from rtc device. todo: get subsecond time
    if (!FLAG(w)) {
      xioctl(fd, RTC_RD_TIME, &tm);
      timeval.tv_sec = xmktime(&tm, utc);
      timeval.tv_usec = 0; // todo: fixit
    }
  }

  if (FLAG(w) || FLAG(t)) {
    if (gettimeofday(&timeval, 0)) perror_exit("gettimeofday failed");
    if (!(utc ? gmtime_r : localtime_r)(&timeval.tv_sec, &tm))
      error_exit(utc ? "gmtime_r failed" : "localtime_r failed");
  }

  if (FLAG(w)) {
    /* The value of tm_isdst is positive if daylight saving time is in effect,
     * zero if it is not and negative if the information is not available. 
     * todo: so why isn't this negative...? */
    tm.tm_isdst = 0;
    xioctl(fd, RTC_SET_TIME, &tm);
  } else if (FLAG(s)) {
    tzone.tz_minuteswest = timezone / 60 - 60 * daylight;
  } else if (FLAG(t)) {
    // Adjust seconds for timezone and daylight saving time
    // extern long timezone is defined in header sys/time.h
    tzone.tz_minuteswest = timezone / 60;
    if (tm.tm_isdst) tzone.tz_minuteswest -= 60;
    if (!utc) timeval.tv_sec += tzone.tz_minuteswest * 60;
  } else {
    strftime(toybuf, sizeof(toybuf), "%F %T%z", &tm);
    xputs(toybuf);
  }
  if (FLAG(t) || FLAG(s)) {
    tzone.tz_dsttime = 0;
    if (settimeofday(&timeval, &tzone)) perror_exit("settimeofday failed");
  }

  if (fd != -1) close(fd);
}