All of lore.kernel.org
 help / color / mirror / Atom feed
From: Richard Cochran <richardcochran@gmail.com>
To: <linux-kernel@vger.kernel.org>
Cc: John Stultz <john.stultz@linaro.org>,
	Thomas Gleixner <tglx@linutronix.de>
Subject: [PATCH RFC V1 1/5] Add functions to convert continuous timescales to UTC.
Date: Fri, 27 Apr 2012 10:12:40 +0200	[thread overview]
Message-ID: <ab3102e23721f82df9841af9584c6c4b12501869.1335510125.git.richardcochran@gmail.com> (raw)
In-Reply-To: <cover.1335510125.git.richardcochran@gmail.com>
In-Reply-To: <cover.1335510125.git.richardcochran@gmail.com>

This patch adds a pair of inline helper functions to convert from
one timescale to the other, around a given leap second threshold.
Also, it adds an academic compile time option to support deleting
leap seconds.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
---
 kernel/time/Kconfig   |   12 ++++++
 kernel/time/utc-tai.h |   99 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 111 insertions(+), 0 deletions(-)
 create mode 100644 kernel/time/utc-tai.h

diff --git a/kernel/time/Kconfig b/kernel/time/Kconfig
index a20dc8a..1569aef 100644
--- a/kernel/time/Kconfig
+++ b/kernel/time/Kconfig
@@ -26,6 +26,18 @@ config HIGH_RES_TIMERS
 	  hardware is not capable then this option only increases
 	  the size of the kernel image.
 
+config DELETE_LEAP_SECONDS
+	bool "Support deletion of leap seconds"
+	default y
+	help
+	  This option enables support for leap second deletion via the
+	  STA_DEL control bit in the NTP adjtimex system call.
+
+	  Historically, leap seconds have only ever been inserted,
+	  never deleted. Saying no here results in a slightly smaller
+	  kernel image. If you believe that Earth's rotational speed
+	  might one day increase, then say yes here.
+
 config GENERIC_CLOCKEVENTS_BUILD
 	bool
 	default y
diff --git a/kernel/time/utc-tai.h b/kernel/time/utc-tai.h
new file mode 100644
index 0000000..ffc3ae9
--- /dev/null
+++ b/kernel/time/utc-tai.h
@@ -0,0 +1,99 @@
+/*
+ * linux/kernel/time/utc-tai.h
+ *
+ * Converts between TAI and UTC by applying the offset correction.
+ *
+ * Copyright (C) 2012 Richard Cochran <richardcochran@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+#ifndef __LINUX_KERNEL_TIME_UTC_TAI_H
+#define __LINUX_KERNEL_TIME_UTC_TAI_H
+
+#include <linux/types.h>
+
+static inline time_t __tai_to_utc(time_t tai, time_t next, int offset)
+{
+	time_t utc = tai - offset;
+
+	if (unlikely(tai >= next))
+		utc--;
+
+	return utc;
+}
+
+static inline time_t __utc_to_tai(time_t utc, time_t next, int offset)
+{
+	time_t leapsec = next - offset;
+	time_t result = utc + offset;
+
+	if (unlikely(utc >= leapsec))
+		result++;
+
+	return result;
+}
+
+#ifdef CONFIG_DELETE_LEAP_SECONDS
+
+static inline time_t tai_to_utc(time_t tai, time_t next, int offset, int insert)
+{
+	if (likely(insert))
+		return __tai_to_utc(tai, next, offset);
+
+	if (unlikely(tai > next))
+		return tai - offset + 1;
+
+	return tai - offset;
+}
+
+static inline time_t utc_to_tai(time_t utc, time_t next, int offset, int insert)
+{
+	time_t leapsec, result;
+
+	if (likely(insert))
+		return __utc_to_tai(utc, next, offset);
+
+	leapsec = next - offset + 1;
+
+	if (unlikely(utc > leapsec))
+		result = utc + offset - 1;
+	else if (utc < leapsec)
+		result = utc + offset;
+	else {
+		/*
+		 * Ouch, this time never existed.
+		 */
+		result = next;
+	}
+
+	return result;
+}
+
+#else /*CONFIG_DELETE_LEAP_SECONDS*/
+
+static inline time_t tai_to_utc(time_t tai, time_t next, int offset, int insert)
+{
+	return __tai_to_utc(tai, next, offset);
+}
+
+static inline time_t utc_to_tai(time_t utc, time_t next, int offset, int insert)
+{
+	return __utc_to_tai(utc, next, offset);
+}
+
+#endif /*!CONFIG_DELETE_LEAP_SECONDS*/
+
+#endif
-- 
1.7.2.5


  reply	other threads:[~2012-04-27  8:14 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-27  8:12 [PATCH RFC V1 0/5] Rationalize time keeping Richard Cochran
2012-04-27  8:12 ` Richard Cochran [this message]
2012-04-27  8:12 ` [PATCH RFC V1 2/5] ntp: Fix a stale comment and a few stray newlines Richard Cochran
2012-04-27 22:25   ` John Stultz
2012-04-27  8:12 ` [PATCH RFC V1 3/5] timekeeping: Fix a few minor newline issues Richard Cochran
2012-04-27 22:25   ` John Stultz
2012-04-27  8:12 ` [PATCH RFC V1 4/5] timekeeping: Offer an interface to manipulate leap seconds Richard Cochran
2012-04-27 23:08   ` John Stultz
2012-04-28  8:47     ` Richard Cochran
2012-05-05 10:17     ` Richard Cochran
2012-05-07 17:36       ` John Stultz
2012-04-27  8:12 ` [PATCH RFC V1 5/5] timekeeping: Use a continuous timescale to tell time Richard Cochran
2012-05-28 16:49   ` Richard Cochran
2012-04-27 22:49 ` [PATCH RFC V1 0/5] Rationalize time keeping John Stultz
2012-04-28  8:04   ` Richard Cochran
2012-04-30 20:56     ` John Stultz
2012-05-01  7:17       ` Richard Cochran
2012-05-01  8:01         ` John Stultz
2012-05-01 18:43           ` Richard Cochran
2012-05-03  7:02       ` Richard Cochran
2012-05-03 15:48         ` John Stultz
2012-05-03 18:21   ` Richard Cochran
2012-05-03 18:44     ` John Stultz
2012-05-03 19:28       ` Richard Cochran
2012-05-03 19:42         ` John Stultz
2012-05-03 19:57 ` John Stultz
2012-05-05  7:34   ` Richard Cochran
2012-05-05 19:25     ` John Stultz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ab3102e23721f82df9841af9584c6c4b12501869.1335510125.git.richardcochran@gmail.com \
    --to=richardcochran@gmail.com \
    --cc=john.stultz@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.