diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2021-09-03 11:09:02 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2021-09-06 12:24:04 +0200 |
commit | ad41c17933e40f277d78b0f9d36b691c00bb8ca5 (patch) | |
tree | eea4439c0ec3753cc129492fbc9f0e4c5d669423 /cpukit | |
parent | score: Simplify _TOD_Validate() (diff) | |
download | rtems-ad41c17933e40f277d78b0f9d36b691c00bb8ca5.tar.bz2 |
score: Change TOD_LATEST_YEAR to 2099
This simplifies the implementation a bit. Declare _TOD_Days_to_date[] in
<rtems/score/todimpl.h>. Make _TOD_Days_per_month[] and
_TOD_Days_since_last_leap_year[] static.
Update #4338.
Diffstat (limited to 'cpukit')
-rw-r--r-- | cpukit/include/rtems/score/todimpl.h | 31 | ||||
-rw-r--r-- | cpukit/rtems/src/clockgettod.c | 6 | ||||
-rw-r--r-- | cpukit/rtems/src/clocktodtoseconds.c | 33 | ||||
-rw-r--r-- | cpukit/rtems/src/clocktodvalidate.c | 18 |
4 files changed, 47 insertions, 41 deletions
diff --git a/cpukit/include/rtems/score/todimpl.h b/cpukit/include/rtems/score/todimpl.h index f85689fd9c..acfec00186 100644 --- a/cpukit/include/rtems/score/todimpl.h +++ b/cpukit/include/rtems/score/todimpl.h @@ -142,12 +142,16 @@ extern "C" { * 32 bits can accept as latest point in time 2106-Feb-7 6:28:15 * but to simplify the implementation, is was decided to only * check that the year is not greater than the year of this constant. + * The year 2099 was chosen because all years evenly divisible by 4 from 1988 + * to 2099 are leap years. In this time frame, years evenly divisible by 100 + * are no leap years unless they are evenly divisible by 400. Thus the year + * 2000 is a leap year. * - * The internal realtime clock can run centuries longer but in + * The internal CLOCK_REALTIME can run centuries longer but in * contrast to the POSIX API, the RTEMS Classic API does not * support this for efficiency reasons. */ -#define TOD_LATEST_YEAR 2105 +#define TOD_LATEST_YEAR 2099 /** * @addtogroup RTEMSScoreTOD @@ -176,6 +180,14 @@ typedef struct { extern TOD_Control _TOD; /** + * @brief This 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 0 for leap years, and 1 otherwise. + */ +extern const uint16_t _TOD_Days_to_date[ 2 ][ 13 ]; + +/** * @brief Locks the time of day mutex. */ void _TOD_Lock( void ); @@ -216,6 +228,21 @@ static inline void _TOD_Release( ISR_lock_Context *lock_context ) } /** + * @brief Maps the year to the leap year index. + * + * @param year is the year to map. + * + * @retval 0 The year is a leap year. + * + * @retval 1 The year is not a leap year. + */ +static inline size_t _TOD_Get_leap_year_index( uint32_t year ) +{ + _Assert( year % 4 != 0 || year % 100 != 0 || year % 400 == 0 ); + return ( ( year % 4 ) + 3 ) / 4; +} + +/** * @brief Checks the time point is a valid new time of day for _TOD_Set(). * * @param tod the time of day to check. 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 <rtems/rtems/clockimpl.h> #include <rtems/score/todimpl.h> -#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; |