mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "pang.xunlei" <pang.xunlei@linaro.org>
To: linux-kernel@vger.kernel.org
Cc: rtc-linux@googlegroups.com, xen-devel@lists.xenproject.org,
	John Stultz <john.stultz@linaro.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Alessandro Zummo <a.zummo@towertech.it>,
	Stefano Stabellini <stefano.stabellini@eu.citrix.com>,
	"pang.xunlei" <pang.xunlei@linaro.org>
Subject: [RFC PATCH v2 02/11] time: Add mktime64() safe version(using time64_t)
Date: Thu, 30 Oct 2014 19:15:36 +0800	[thread overview]
Message-ID: <1414667745-7703-3-git-send-email-pang.xunlei@linaro.org> (raw)
In-Reply-To: <1414667745-7703-1-git-send-email-pang.xunlei@linaro.org>

As part of addressing 2038 saftey for in-kernel uses, this patch
adds safe mktime64() using time64_t. After this patch, mktime()
should be replaced by mktime64() one by one. Eventually, mktime()
will be removed from the kernel when it has no users.

Signed-off-by: pang.xunlei <pang.xunlei@linaro.org>
---
 include/linux/time.h |    4 ++++
 kernel/time/time.c   |   39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/include/linux/time.h b/include/linux/time.h
index 8c42cf8..5c2c077 100644
--- a/include/linux/time.h
+++ b/include/linux/time.h
@@ -39,6 +39,10 @@ static inline int timeval_compare(const struct timeval *lhs, const struct timeva
 	return lhs->tv_usec - rhs->tv_usec;
 }
 
+extern time64_t mktime64(const unsigned int year, const unsigned int mon,
+                const unsigned int day, const unsigned int hour,
+                const unsigned int min, const unsigned int sec);
+
 extern unsigned long mktime(const unsigned int year, const unsigned int mon,
 			    const unsigned int day, const unsigned int hour,
 			    const unsigned int min, const unsigned int sec);
diff --git a/kernel/time/time.c b/kernel/time/time.c
index a9ae20f..f44efdb 100644
--- a/kernel/time/time.c
+++ b/kernel/time/time.c
@@ -304,6 +304,44 @@ struct timespec timespec_trunc(struct timespec t, unsigned gran)
 }
 EXPORT_SYMBOL(timespec_trunc);
 
+/* 
+ * Converts Gregorian date to seconds since 1970-01-01 00:00:00.
+ * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
+ * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
+ *
+ * [For the Julian calendar (which was used in Russia before 1917,
+ * Britain & colonies before 1752, anywhere else before 1582,
+ * and is still in use by some communities) leave out the
+ * -year/100+year/400 terms, and add 10.]
+ *
+ * This algorithm was first published by Gauss (I think).
+ *
+ * Safe version 2038 safety on 32-bit systems.
+ */
+time64_t
+mktime64(const unsigned int year0, const unsigned int mon0,
+       const unsigned int day, const unsigned int hour,
+       const unsigned int min, const unsigned int sec)
+{
+	time64_t ret;
+	unsigned int mon = mon0, year = year0;
+
+	 /* 1..12 -> 11,12,1..10 */
+	if (0 >= (int) (mon -= 2)) {
+		mon += 12;  /* Puts Feb last since it has leap day */
+		year -= 1;
+	}
+
+	ret = (year/4 - year/100 + year/400 + 367*mon/12 + day) + year*365 - 719499;
+	ret = ret*24 + hour; /* now have hours */
+	ret = ret*60 + min; /* now have minutes */
+	ret = ret*60 + sec; /* finally seconds */
+
+	return ret;
+}
+
+EXPORT_SYMBOL(mktime64);
+
 /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
  * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
  * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
@@ -318,6 +356,7 @@ EXPORT_SYMBOL(timespec_trunc);
  * WARNING: this function will overflow on 2106-02-07 06:28:16 on
  * machines where long is 32-bit! (However, as time_t is signed, we
  * will already get problems at other places on 2038-01-19 03:14:08)
+ * TODO: [2038 safety] should be replaced by mktime64().
  */
 unsigned long
 mktime(const unsigned int year0, const unsigned int mon0,
-- 
1.7.9.5


  parent reply	other threads:[~2014-10-30 11:17 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-30 11:15 [RFC PATCH v2 00/11] Convert do_settimeofday() to use timespec64 pang.xunlei
2014-10-30 11:15 ` [RFC PATCH v2 01/11] time: Add do_settimeofday64() safe version(using timespec64) pang.xunlei
2014-10-30 13:24   ` Thomas Gleixner
2014-10-30 11:15 ` pang.xunlei [this message]
2014-10-30 13:43   ` [RFC PATCH v2 02/11] time: Add mktime64() safe version(using time64_t) Thomas Gleixner
2014-10-30 11:15 ` [RFC PATCH v2 03/11] time: Add rtc_tm_to_time64() " pang.xunlei
2014-10-30 13:55   ` Thomas Gleixner
2014-10-30 16:30     ` pang.xunlei
2014-10-30 20:49       ` Thomas Gleixner
2014-10-30 11:15 ` [RFC PATCH v2 04/11] time: Add rtc_time_to_tm64() " pang.xunlei
2014-10-30 14:07   ` Thomas Gleixner
2014-10-30 11:15 ` [RFC PATCH v2 05/11] time: Convert all users of do_settimeofday() to use do_settimeofday64() pang.xunlei
2014-10-30 14:49   ` Thomas Gleixner
2014-10-30 15:27     ` John Stultz
2014-10-30 16:06       ` Thomas Gleixner
2014-10-30 11:15 ` [RFC PATCH v2 06/11] time: Remove do_settimeofday() pang.xunlei
2014-10-30 11:15 ` [RFC PATCH v2 07/11] time: Convert alarm_set_rtc() to use timespec64 pang.xunlei
2014-10-30 14:52   ` Thomas Gleixner
2014-10-30 11:15 ` [RFC PATCH v2 08/11] time: Convert xen_read_wallclock() " pang.xunlei
     [not found]   ` <54522159.8070204@citrix.com>
2014-10-30 14:58     ` [Xen-devel] " Thomas Gleixner
2014-10-30 11:15 ` [RFC PATCH v2 09/11] time: Convert pvclock_read_wallclock() " pang.xunlei
2014-10-30 14:57   ` Thomas Gleixner
2014-10-30 11:15 ` [RFC PATCH v2 10/11] time: Convert x86_platform.set_wallclock()to " pang.xunlei
2014-10-30 11:15 ` [RFC PATCH v2 11/11] time: Convert x86_platform.get_wallclock()to " pang.xunlei

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=1414667745-7703-3-git-send-email-pang.xunlei@linaro.org \
    --to=pang.xunlei@linaro.org \
    --cc=a.zummo@towertech.it \
    --cc=john.stultz@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rtc-linux@googlegroups.com \
    --cc=stefano.stabellini@eu.citrix.com \
    --cc=tglx@linutronix.de \
    --cc=xen-devel@lists.xenproject.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®