[PATCH[ RTC: Add rtc_year_days() to calculate tm_yday
RTC: Add exported function rtc_year_days() to calculate the tm_yday value.
drivers/rtc/rtc-lib.c +19 -0
@@ -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. */
include/linux/rtc.h +1 -0
@@ -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);
Quoted 2 lines · Andrew Victor
RTC: Add exported function
rtc_year_days()to calculate thetm_ydayvalue.
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".
Signature · 3 lines
Russell King Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/ maintainer of: 2.6 Serial core
Followup to: <[email protected]> By author: Russell King <[email protected]> In newsgroup: linux.dev.kernel
Quoted 8 lines · Andrew Victor
Quoted 2 lines
RTC: Add exported function
rtc_year_days()to calculate thetm_ydayvalue.Is there a good reason for this? I ask the question because the x86
/dev/rtcdriver says:
Quoted 8 lines
* Only the values that we read from the RTC are set. We leave
*tm_wday,tm_ydayandtm_isdstuntouched. Note that while the
* RTC hasRTC_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
hi Russell,
Quoted 6 lines
Quoted 2 lines
RTC: Add exported function
rtc_year_days()to calculate thetm_ydayvalue.Is there a good reason for this? I ask the question because the x86
/dev/rtcdriver says:
Quoted 7 lines
* Only the values that we read from the RTC are set. We leave
*tm_wday,tm_ydayandtm_isdstuntouched. Note that while the
* RTC hasRTC_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