title: Linux RTC application code waits at read() & current year is displayed incorrectly
body: <p>I wrote a simple RTC Test application, compiled &amp; ran. But I am unable to decode two things from the output I got:</p>
  
  <p>Output:</p>
  
  <pre><code>====== RTC Test  ====
  Open &amp; release
  opened
  get RTC time
  Current RTC date/time is 9-5-114, 18:17:55
  read RTC time
  </code></pre>
  
  <p>Issue #1) The year is incorrect as 114 &amp; not 2014 or 14 as expected.</p>
  
  <p>Issue #2) The code waits at read()</p>
  
  <p>Why is this happening? And how to resolve this?
  Here is the code:-</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;linux/rtc.h&gt;
  #include &lt;sys/ioctl.h&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  
  
  int main()
  {
          int fd, ret;
          struct rtc_time rtc_tm;
          unsigned long data;
  
          printf("====== RTC Test  ====\n");
  
          printf("Open &amp; release\n");
  //open function is used to establish the connection between the RTC device with the file descriptor
          fd = open("/dev/rtc0", O_RDWR, 0);      //open for reading &amp; writing
          if (fd == -1)
          {
                  perror("/dev/rtc0");
                  exit(errno);
          }
          else
          {
                  printf("opened\n");
          }
  
  //ioctl() to configure the RTC device
          printf("get RTC time\n");
          ret = ioctl(fd, RTC_RD_TIME, &amp;rtc_tm);  //ioctl command RTC_RD_TIME is used to read the current timer
          if (ret == -1)
          {
                  perror("rtc ioctl RTC_RD_TIME error");
          }
          printf("Current RTC date/time is %d-%d-%d, %02d:%02d:%02d\n",
                  rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year,
  rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
  
          /* Set the RTC time/date */
         /* rtc_tm.tm_mday = 31;
          rtc_tm.tm_mon = 4;      // for example Sep 8
          rtc_tm.tm_year = 104;
          rtc_tm.tm_hour = 2;
          rtc_tm.tm_min = 30;
          rtc_tm.tm_sec = 0;
          */
  
  // call the read function to wait the Alarm interrupt
          printf("read RTC time\n");
          ret = read(fd, &amp;data, sizeof(unsigned long));
          if (ret == -1)
          {
                  perror("rtc read error");
          }
          printf("rtc read\n");
  
  // set RTC time
          printf("set RTC time\n");
          ret == ioctl(fd, RTC_SET_TIME, &amp;rtc_tm);
          if (ret == -1)
          {
                  perror("rtc ioctl RTC_SET_TIME error");
          }
          printf("Set Current RTC date/time to %d-%d-%d, %02d:%02d:%02d\n",
                  rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year,
                  rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
          printf("Get RTC time\n");
          ret = ioctl(fd, RTC_RD_TIME, &amp;rtc_tm);
          if (ret == -1)
          {
                  perror("rtc ioctl RTC_RD_TIME error");
          }
          printf("Current RTC date/time is %d-%d-%d, %02d:%02d:%02d\n",
                  rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year,
                  rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
  
          rtc_tm.tm_sec += 50;
          if (rtc_tm.tm_sec &gt;= 60)
          {
                  rtc_tm.tm_sec %= 60;
                  rtc_tm.tm_min++;
          }
          if (rtc_tm.tm_min == 60)
          {
                  rtc_tm.tm_min = 0;
                  rtc_tm.tm_hour++;
          }
          if (rtc_tm.tm_hour == 24)
          {
                  rtc_tm.tm_hour = 0;
          }
          printf("RTC tests done");
          close(fd);
  
          return 0;
  }
  </code></pre>
  
  <p>As far as issue #2 is concerned, it could be since the device running the RTC is waiting for an interrupt. Please confirm whether my guess is correct, because if yes, then I need not worry about it.</p>