These code are mainly from kernel source code of rtc interface. It reads hardware clock and use it as initial wall time when system boots.

Original source code is at drivers/rtc/hctosys.c.

   int err = -ENODEV;
   struct rtc_time tm;
   struct timespec tv = {
      .tv_nsec = NSEC_PER_SEC >> 1,
   struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);

   if (rtc == NULL) {
      printk(KERN_INFO "%s: unable to open rtc device (%s)\n",
      goto err_open;

   err = rtc_read_time(rtc, &tm);
   if (err) {
      printk(KERN_INFO "get_timestamp_vars: unable to read the hardware clock\n");
      goto err_read;

   err = rtc_valid_tm(&tm);
   if (err) {
      printk(KERN_INFO "get_timestamp_vars: invalid date/time\n");
      goto err_invalid;

   rtc_tm_to_time(&tm, &tv.tv_sec);

   printk(KERN_INFO "get_timestamp_vars: RTC time: %d-%02d-%02d %02d:%02d:%02d UTC (%u)\n",
      tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
      tm.tm_hour, tm.tm_min, tm.tm_sec,
      (unsigned int) tv.tv_sec);

   //Do other job here

-- ChenLi - 07 Aug 2013
Topic revision: r2 - 08 Feb 2014, HarlanStenn

This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Network Time Foundation Community Wiki? Send feedback