From ad41c17933e40f277d78b0f9d36b691c00bb8ca5 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Fri, 3 Sep 2021 11:09:02 +0200 Subject: score: Change TOD_LATEST_YEAR to 2099 This simplifies the implementation a bit. Declare _TOD_Days_to_date[] in . Make _TOD_Days_per_month[] and _TOD_Days_since_last_leap_year[] static. Update #4338. --- cpukit/rtems/src/clockgettod.c | 6 ++---- cpukit/rtems/src/clocktodtoseconds.c | 33 ++++++++------------------------- cpukit/rtems/src/clocktodvalidate.c | 18 ++++++++---------- 3 files changed, 18 insertions(+), 39 deletions(-) (limited to 'cpukit/rtems') diff --git a/cpukit/rtems/src/clockgettod.c b/cpukit/rtems/src/clockgettod.c index dea136d477..5058b42375 100644 --- a/cpukit/rtems/src/clockgettod.c +++ b/cpukit/rtems/src/clockgettod.c @@ -32,8 +32,6 @@ #define RTEMS_DAYS_PER_YEAR (365UL) #define RTEMS_YEAR_BASE (1970UL) -extern const uint16_t _TOD_Days_to_date[2][13]; - static bool _Leap_year( uint32_t year ) @@ -64,9 +62,9 @@ static uint32_t _Year_day_as_month( uint32_t month = 0; if ( _Leap_year( year ) ) - days_to_date = _TOD_Days_to_date[1]; - else days_to_date = _TOD_Days_to_date[0]; + else + days_to_date = _TOD_Days_to_date[1]; days_to_date += 2; diff --git a/cpukit/rtems/src/clocktodtoseconds.c b/cpukit/rtems/src/clocktodtoseconds.c index 86e89f86eb..43bf6c59c5 100644 --- a/cpukit/rtems/src/clocktodtoseconds.c +++ b/cpukit/rtems/src/clocktodtoseconds.c @@ -23,16 +23,9 @@ #include #include -#define TOD_SECONDS_AT_2100_03_01_00_00 4107542400UL - -/* - * The following array contains the number of days in all months - * up to the month indicated by the index of the second dimension. - * The first dimension should be 1 for leap years, and 0 otherwise. - */ -const uint16_t _TOD_Days_to_date[2][13] = { - { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }, - { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 } +const uint16_t _TOD_Days_to_date[ 2 ][ 13 ] = { + { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }, + { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 } }; /* @@ -48,21 +41,18 @@ Watchdog_Interval _TOD_To_seconds( const rtems_time_of_day *the_tod ) { - uint32_t time; - uint32_t year_mod_4; + uint32_t time; + size_t leap_year_index; time = the_tod->day - 1; - year_mod_4 = the_tod->year & 3; - if ( year_mod_4 == 0 ) - time += _TOD_Days_to_date[ 1 ][ the_tod->month ]; - else - time += _TOD_Days_to_date[ 0 ][ the_tod->month ]; + leap_year_index = _TOD_Get_leap_year_index( the_tod->year ); + time += _TOD_Days_to_date[ leap_year_index ][ the_tod->month ]; time += ( (the_tod->year - TOD_BASE_YEAR) / 4 ) * ( (TOD_DAYS_PER_YEAR * 4) + 1); - time += _TOD_Days_since_last_leap_year[ year_mod_4 ]; + time += _TOD_Days_since_last_leap_year[ the_tod->year % 4 ]; time *= TOD_SECONDS_PER_DAY; @@ -70,13 +60,6 @@ Watchdog_Interval _TOD_To_seconds( * TOD_SECONDS_PER_MINUTE; time += the_tod->second; - - /* The year 2100 is not a leap year */ - if ( time - >= (TOD_SECONDS_AT_2100_03_01_00_00 - TOD_SECONDS_1970_THROUGH_1988)) { - time -= TOD_SECONDS_PER_DAY; - } - time += TOD_SECONDS_1970_THROUGH_1988; return( time ); diff --git a/cpukit/rtems/src/clocktodvalidate.c b/cpukit/rtems/src/clocktodvalidate.c index 0e3b5772b5..4433374e65 100644 --- a/cpukit/rtems/src/clocktodvalidate.c +++ b/cpukit/rtems/src/clocktodvalidate.c @@ -26,13 +26,13 @@ /* * The following array contains the number of days in all months. - * The first dimension should be 1 for leap years, and 0 otherwise. + * The first dimension should be 0 for leap years, and 1 otherwise. * The second dimension should range from 1 to 12 for January to - * February, respectively. + * December, respectively. */ -const uint32_t _TOD_Days_per_month[ 2 ][ 13 ] = { - { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, - { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } +static const uint32_t _TOD_Days_per_month[ 2 ][ 13 ] = { + { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, + { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } }; rtems_status_code _TOD_Validate( @@ -40,6 +40,7 @@ rtems_status_code _TOD_Validate( TOD_Ticks_validation ticks_validation ) { + size_t leap_year_index; uint32_t days_in_month; uint32_t ticks_per_second; uint32_t ticks_mask; @@ -79,11 +80,8 @@ rtems_status_code _TOD_Validate( return RTEMS_INVALID_CLOCK; } - if (((the_tod->year % 4) == 0 && (the_tod->year % 100 != 0)) || - (the_tod->year % 400 == 0)) - days_in_month = _TOD_Days_per_month[ 1 ][ the_tod->month ]; - else - days_in_month = _TOD_Days_per_month[ 0 ][ the_tod->month ]; + leap_year_index = _TOD_Get_leap_year_index( the_tod->year ); + days_in_month = _TOD_Days_per_month[ leap_year_index ][ the_tod->month ]; if ( the_tod->day > days_in_month ) { return RTEMS_INVALID_CLOCK; -- cgit v1.2.3