* [PATCH[ RTC: Add rtc_year_days() to calculate tm_yday
@ 2006-06-07 18:26 Andrew Victor
2006-06-07 19:33 ` Russell King
0 siblings, 1 reply; 4+ messages in thread
From: Andrew Victor @ 2006-06-07 18:26 UTC (permalink / raw)
To: linux-kernel; +Cc: alessandro.zummo, akpm
RTC: Add exported function rtc_year_days() to calculate the tm_yday
value.
Signed-off-by: Andrew Victor <andrew@sanpeople.com>
Signed-off-by: Alessandro Zummo <a.zummo@towertech.it>
diff -urN -x CVS linux-2.6.17-rc6/drivers/rtc/rtc-lib.c linux-2.6.17-rc/drivers/rtc/rtc-lib.c
--- linux-2.6.17-rc6/drivers/rtc/rtc-lib.c Tue Jun 6 10:28:05 2006
+++ linux-2.6.17-rc/drivers/rtc/rtc-lib.c Wed Jun 7 11:27:49 2006
@@ -18,15 +18,34 @@
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
+static const unsigned short rtc_ydays[2][13] = {
+ /* Normal years */
+ { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
+ /* Leap years */
+ { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
+};
+
#define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
#define LEAP_YEAR(year) ((!(year % 4) && (year % 100)) || !(year % 400))
+/*
+ * The number of days in the month.
+ */
int rtc_month_days(unsigned int month, unsigned int year)
{
return rtc_days_in_month[month] + (LEAP_YEAR(year) && month == 1);
}
EXPORT_SYMBOL(rtc_month_days);
+/*
+ * The number of days since January 1. (0 to 365)
+ */
+int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
+{
+ return rtc_ydays[LEAP_YEAR(year)][month] + day-1;
+}
+EXPORT_SYMBOL(rtc_year_days);
+
/*
* Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
*/
diff -urN -x CVS linux-2.6.17-rc6/include/linux/rtc.h linux-2.6.17-rc/include/linux/rtc.h
--- linux-2.6.17-rc6/include/linux/rtc.h Tue Jun 6 10:28:30 2006
+++ linux-2.6.17-rc/include/linux/rtc.h Wed Jun 7 11:24:45 2006
@@ -102,6 +102,7 @@
#include <linux/interrupt.h>
extern int rtc_month_days(unsigned int month, unsigned int year);
+extern int rtc_year_days(unsigned int day, unsigned int month, unsigned int year);
extern int rtc_valid_tm(struct rtc_time *tm);
extern int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time);
extern void rtc_time_to_tm(unsigned long time, struct rtc_time *tm);
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH[ RTC: Add rtc_year_days() to calculate tm_yday
2006-06-07 18:26 [PATCH[ RTC: Add rtc_year_days() to calculate tm_yday Andrew Victor
@ 2006-06-07 19:33 ` Russell King
2006-06-07 22:48 ` H. Peter Anvin
2006-06-08 6:43 ` Andrew Victor
0 siblings, 2 replies; 4+ messages in thread
From: Russell King @ 2006-06-07 19:33 UTC (permalink / raw)
To: Andrew Victor; +Cc: linux-kernel, alessandro.zummo, akpm
On Wed, Jun 07, 2006 at 08:26:09PM +0200, Andrew Victor wrote:
> RTC: Add exported function rtc_year_days() to calculate the tm_yday
> value.
Is there a good reason for this? I ask the question because the x86
/dev/rtc driver says:
* Only the values that we read from the RTC are set. We leave
* tm_wday, tm_yday and tm_isdst untouched. Note that while the
* RTC has RTC_DAY_OF_WEEK, we should usually ignore it, as it is
* only updated by the RTC when initially set to a non-zero value.
So it seems the established modus operandi for RTC interfaces is "don't
trust wday, yday and isdst".
--
Russell King
Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/
maintainer of: 2.6 Serial core
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH[ RTC: Add rtc_year_days() to calculate tm_yday
2006-06-07 19:33 ` Russell King
@ 2006-06-07 22:48 ` H. Peter Anvin
2006-06-08 6:43 ` Andrew Victor
1 sibling, 0 replies; 4+ messages in thread
From: H. Peter Anvin @ 2006-06-07 22:48 UTC (permalink / raw)
To: linux-kernel
Followup to: <20060607193311.GH13165@flint.arm.linux.org.uk>
By author: Russell King <rmk+lkml@arm.linux.org.uk>
In newsgroup: linux.dev.kernel
>
> On Wed, Jun 07, 2006 at 08:26:09PM +0200, Andrew Victor wrote:
> > RTC: Add exported function rtc_year_days() to calculate the tm_yday
> > value.
>
> Is there a good reason for this? I ask the question because the x86
> /dev/rtc driver says:
>
> * Only the values that we read from the RTC are set. We leave
> * tm_wday, tm_yday and tm_isdst untouched. Note that while the
> * RTC has RTC_DAY_OF_WEEK, we should usually ignore it, as it is
> * only updated by the RTC when initially set to a non-zero value.
>
> So it seems the established modus operandi for RTC interfaces is "don't
> trust wday, yday and isdst".
>
"Be conservative in what you send, liberal in what you accept."
- Mr. Protocol
In this case it seems a good idea to set these fields correctly, but
not rely upon them, unless there are known cases where that causes
trouble.
-hpa
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH[ RTC: Add rtc_year_days() to calculate tm_yday
2006-06-07 19:33 ` Russell King
2006-06-07 22:48 ` H. Peter Anvin
@ 2006-06-08 6:43 ` Andrew Victor
1 sibling, 0 replies; 4+ messages in thread
From: Andrew Victor @ 2006-06-08 6:43 UTC (permalink / raw)
To: Russell King; +Cc: linux-kernel, alessandro.zummo, akpm
hi Russell,
> > RTC: Add exported function rtc_year_days() to calculate the tm_yday
> > value.
>
> Is there a good reason for this? I ask the question because the x86
> /dev/rtc driver says:
>
> * Only the values that we read from the RTC are set. We leave
> * tm_wday, tm_yday and tm_isdst untouched. Note that while the
> * RTC has RTC_DAY_OF_WEEK, we should usually ignore it, as it is
> * only updated by the RTC when initially set to a non-zero value.
>
> So it seems the established modus operandi for RTC interfaces is "don't
> trust wday, yday and isdst".
wday and yday are already being calculated by rtc_time_to_tm() in
drivers/rtc/rtc-lib.c, and this function is used by about half of the
RTC class drivers.
Since yday can also be easily calculated from dd/mm/yyyy, I don't see
why those drivers who don't use rtc_time_to_tm() can't also return a
valid yday value.
Regards,
Andrew Victor
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2006-06-08 6:49 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-06-07 18:26 [PATCH[ RTC: Add rtc_year_days() to calculate tm_yday Andrew Victor
2006-06-07 19:33 ` Russell King
2006-06-07 22:48 ` H. Peter Anvin
2006-06-08 6:43 ` Andrew Victor
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox