From: "Lee, Chun-Yi" <joeyli.kernel@gmail.com>
To: "Rafael J. Wysocki" <rjw@rjwysocki.net>,
Alessandro Zummo <a.zummo@towertech.it>,
"H. Peter Anvin" <hpa@zytor.com>,
Matt Fleming <matt@console-pimps.org>,
Matthew Garrett <matthew.garrett@nebula.com>
Cc: Elliott@hp.com, samer.el-haj-mahmoud@hp.com,
Oliver Neukum <oneukum@suse.de>,
werner@suse.com, trenn@suse.de, JBeulich@suse.com,
linux-kernel@vger.kernel.org, rtc-linux@googlegroups.com,
x86@kernel.org,
"linux-efi@vger.kernel.org" <linux-efi@vger.kernel.org>,
linux-acpi@vger.kernel.org, "Lee, Chun-Yi" <jlee@suse.com>
Subject: [RFC PATCH 10/14] rtc: improve and move week day computing function to rtc header
Date: Thu, 19 Dec 2013 15:51:51 +0800 [thread overview]
Message-ID: <1387439515-8926-11-git-send-email-jlee@suse.com> (raw)
In-Reply-To: <1387439515-8926-1-git-send-email-jlee@suse.com>
Due to rtc-acpid and efi time used the same logic for computing
week day, so this patch moves code to rtc.h header file.
Additionally using a leap year algorithm to replace the for-loop
block in compute_wday for improve the performance. The first
version of algorithm is from Oliver Neukum.
---
drivers/rtc/rtc-acpitad.c | 13 +------------
include/linux/efi.h | 13 +------------
include/linux/rtc.h | 38 ++++++++++++++++++++++++++++++++++++++
3 files changed, 40 insertions(+), 24 deletions(-)
diff --git a/drivers/rtc/rtc-acpitad.c b/drivers/rtc/rtc-acpitad.c
index 065a033..bdf7ae1 100644
--- a/drivers/rtc/rtc-acpitad.c
+++ b/drivers/rtc/rtc-acpitad.c
@@ -32,23 +32,12 @@ compute_yday(struct acpi_time *acpit)
static int
compute_wday(struct acpi_time *acpit)
{
- int y;
- int ndays = 0;
-
if (acpit->year < 1900) {
pr_err("ACPI year < 1900, invalid date\n");
return -1;
}
- for (y = 1900; y < acpit->year; y++)
- ndays += 365 + (is_leap_year(y) ? 1 : 0);
-
- ndays += compute_yday(acpit);
-
- /*
- * 1=1/1/1900 was a Monday
- */
- return (ndays + 1) % 7;
+ return rtc_wday(acpit->day, acpit->month - 1, acpit->year);
}
static void
diff --git a/include/linux/efi.h b/include/linux/efi.h
index 3859f3e..1c78ae7 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -177,23 +177,12 @@ compute_yday(efi_time_t *eft)
static inline int
compute_wday(efi_time_t *eft)
{
- int y;
- int ndays = 0;
-
if (eft->year < 1998) {
pr_err("EFI year < 1998, invalid date\n");
return -1;
}
- for (y = EFI_RTC_EPOCH; y < eft->year; y++)
- ndays += 365 + (is_leap_year(y) ? 1 : 0);
-
- ndays += compute_yday(eft);
-
- /*
- * 4=1/1/1998 was a Thursday
- */
- return (ndays + 4) % 7;
+ return rtc_wday(eft->day, eft->month - 1, eft->year);
}
static inline void
diff --git a/include/linux/rtc.h b/include/linux/rtc.h
index e6380ec..511884f 100644
--- a/include/linux/rtc.h
+++ b/include/linux/rtc.h
@@ -196,6 +196,44 @@ static inline bool is_leap_year(unsigned int year)
return (!(year % 4) && (year % 100)) || !(year % 400);
}
+#define SINCE1900 25 /* valid from 2000 */
+
+static inline int rtc_wday(unsigned int day, unsigned int month, unsigned int year)
+{
+ int ndays, correction;
+ int base;
+
+ if (year < 1900) {
+ pr_err("rtc: year < 1900, invalid date\n");
+ return -1;
+ }
+
+ if (year >= 2000)
+ base = year - 2000;
+ else
+ base = year - 1900;
+
+ correction = 0;
+ if (base >= 0) {
+ correction += base / 4;
+ correction -= base / 100;
+ correction += base / 400;
+ if (year >= 2000)
+ correction += SINCE1900;
+
+ /* later rtc_year_days will add the leap day of current year */
+ correction -= ((is_leap_year(year)) ? 1 : 0);
+ }
+
+ ndays = (year - 1900) * 365 + correction;
+ ndays += rtc_year_days(day, month, year);
+
+ /*
+ * 1=1/1/1900 was a Monday
+ */
+ return (ndays + 1) % 7;
+}
+
#ifdef CONFIG_RTC_HCTOSYS_DEVICE
extern int rtc_hctosys_ret;
#else
--
1.6.4.2
next prev parent reply other threads:[~2013-12-19 7:54 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-12-19 7:51 [RFC PATCH 00/14] Support timezone of ACPI TAD and EFI TIME Lee, Chun-Yi
2013-12-19 7:51 ` [PATCH 01/14] rtc-efi: fix decrease day twice when computing year days Lee, Chun-Yi
2013-12-19 7:51 ` [PATCH 03/14] rtc: block registration of rtc-cmos when CMOS RTC Not Present Lee, Chun-Yi
2013-12-19 7:51 ` [RFC PATCH 04/14] ACPI: Add ACPI 5.0 Time and Alarm Device driver Lee, Chun-Yi
2013-12-19 15:22 ` H. Peter Anvin
2013-12-20 5:41 ` joeyli
2014-01-01 0:42 ` H. Peter Anvin
2014-01-06 8:58 ` joeyli
2014-01-07 5:37 ` H. Peter Anvin
2014-01-07 10:40 ` joeyli
2014-01-07 16:35 ` H. Peter Anvin
2014-01-08 14:59 ` joeyli
2014-01-08 17:56 ` H. Peter Anvin
2014-01-09 3:47 ` joeyli
2014-01-09 4:00 ` H. Peter Anvin
2014-01-09 12:16 ` Rafael J. Wysocki
2014-01-02 8:09 ` Lan Tianyu
2014-01-06 9:20 ` joeyli
2013-12-19 7:51 ` [RFC PATCH 05/14] rtc: Add RTC driver of ACPI Time and Alarm Device Lee, Chun-Yi
2013-12-19 7:51 ` [RFC PATCH 06/14] rtc-efi: register rtc-efi device when EFI enabled Lee, Chun-Yi
2013-12-19 14:09 ` Matt Fleming
2013-12-20 4:24 ` joeyli
2013-12-20 4:30 ` H. Peter Anvin
2013-12-20 10:37 ` Borislav Petkov
2013-12-20 15:14 ` Matthew Garrett
2013-12-20 21:04 ` H. Peter Anvin
2013-12-21 1:24 ` joeyli
2013-12-21 1:51 ` H. Peter Anvin
2013-12-21 15:34 ` joeyli
2013-12-20 16:48 ` H. Peter Anvin
2013-12-19 7:51 ` [RFC PATCH 07/14] rtc-efi: add GMTOFF support to rtc_efi Lee, Chun-Yi
2013-12-20 15:11 ` Matthew Garrett
2013-12-21 3:56 ` joeyli
2013-12-19 7:51 ` [RFC PATCH 08/14] rtc-efi: set uie_unsupported for indicate rtc-efi doesn't support UIE mode Lee, Chun-Yi
2013-12-19 7:51 ` [RFC PATCH 09/14] efi: move functions of access efi time to header file for sharing Lee, Chun-Yi
2013-12-19 7:51 ` Lee, Chun-Yi [this message]
2013-12-19 7:51 ` [RFC PATCH 11/14] rtc: switch to get/set rtc time to efi functions if CMOS RTC Not Present git set Lee, Chun-Yi
2013-12-19 7:51 ` [RFC PATCH 12/14] efi: adjust system time base on timezone from EFI time services Lee, Chun-Yi
2013-12-19 7:51 ` [RFC PATCH 13/14] Documentation/RTC: add document of ACPI TAD and EFI TIME driver Lee, Chun-Yi
2013-12-19 7:51 ` [TEST PATCH 14/14] acpi: add early parameter to set CMOS RTC Not Present bit for testing Lee, Chun-Yi
[not found] ` <1387448416-11672-1-git-send-email-jlee@suse.com>
2013-12-19 10:49 ` [PATCH 02/14] x86-64/efi: Use EFI to deal with platform wall clock (again) Matt Fleming
2013-12-19 13:32 ` joeyli
2013-12-20 11:29 ` One Thousand Gnomes
2013-12-23 23:25 ` H. Peter Anvin
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=1387439515-8926-11-git-send-email-jlee@suse.com \
--to=joeyli.kernel@gmail.com \
--cc=Elliott@hp.com \
--cc=JBeulich@suse.com \
--cc=a.zummo@towertech.it \
--cc=hpa@zytor.com \
--cc=jlee@suse.com \
--cc=linux-acpi@vger.kernel.org \
--cc=linux-efi@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=matt@console-pimps.org \
--cc=matthew.garrett@nebula.com \
--cc=oneukum@suse.de \
--cc=rjw@rjwysocki.net \
--cc=rtc-linux@googlegroups.com \
--cc=samer.el-haj-mahmoud@hp.com \
--cc=trenn@suse.de \
--cc=werner@suse.com \
--cc=x86@kernel.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®