[4efe1955] | 1 | /** |
---|
| 2 | * @file |
---|
[d7d7859] | 3 | * |
---|
[4efe1955] | 4 | * @brief Obtain Current Time of Day (Classic TOD) |
---|
| 5 | * @ingroup ClassicClock Clocks |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | /* |
---|
[d7d7859] | 9 | * COPYRIGHT (c) 1989-2007. |
---|
| 10 | * On-Line Applications Research Corporation (OAR). |
---|
| 11 | * |
---|
| 12 | * The license and distribution terms for this file may be |
---|
| 13 | * found in the file LICENSE in this distribution or at |
---|
[c499856] | 14 | * http://www.rtems.org/license/LICENSE. |
---|
[d7d7859] | 15 | */ |
---|
| 16 | |
---|
| 17 | #if HAVE_CONFIG_H |
---|
| 18 | #include "config.h" |
---|
| 19 | #endif |
---|
| 20 | |
---|
| 21 | #include <rtems/rtems/clock.h> |
---|
[f031df0e] | 22 | #include <rtems/score/todimpl.h> |
---|
[88c74ab] | 23 | #include <rtems/config.h> |
---|
[d7d7859] | 24 | |
---|
[eae0863] | 25 | #define RTEMS_SECS_PER_MINUTE (60UL) |
---|
| 26 | #define RTEMS_MINUTE_PER_HOUR (60UL) |
---|
| 27 | #define RTEMS_SECS_PER_HOUR (RTEMS_SECS_PER_MINUTE * RTEMS_MINUTE_PER_HOUR) |
---|
| 28 | #define RTEMS_HOURS_PER_DAY (24UL) |
---|
| 29 | #define RTEMS_SECS_PER_DAY (RTEMS_SECS_PER_HOUR * RTEMS_HOURS_PER_DAY) |
---|
| 30 | #define RTEMS_DAYS_PER_YEAR (365UL) |
---|
| 31 | #define RTEMS_YEAR_BASE (1970UL) |
---|
| 32 | |
---|
| 33 | extern const uint16_t _TOD_Days_to_date[2][13]; |
---|
| 34 | |
---|
| 35 | static bool _Leap_year( |
---|
| 36 | uint32_t year |
---|
| 37 | ) |
---|
| 38 | { |
---|
| 39 | return (((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | static uint32_t _Leap_years_before( |
---|
| 43 | uint32_t year |
---|
| 44 | ) |
---|
| 45 | { |
---|
| 46 | year -= 1; |
---|
| 47 | return (year / 4) - (year / 100) + (year / 400); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | static uint32_t _Leap_years_between( |
---|
| 51 | uint32_t from, uint32_t to |
---|
| 52 | ) |
---|
| 53 | { |
---|
| 54 | return _Leap_years_before( to ) - _Leap_years_before( from + 1 ); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | static uint32_t _Year_day_as_month( |
---|
| 58 | uint32_t year, uint32_t *day |
---|
| 59 | ) |
---|
| 60 | { |
---|
| 61 | const uint16_t* days_to_date; |
---|
| 62 | uint32_t month = 0; |
---|
| 63 | |
---|
| 64 | if ( _Leap_year( year ) ) |
---|
| 65 | days_to_date = _TOD_Days_to_date[1]; |
---|
| 66 | else |
---|
| 67 | days_to_date = _TOD_Days_to_date[0]; |
---|
| 68 | |
---|
| 69 | days_to_date += 2; |
---|
| 70 | |
---|
| 71 | while (month < 11) { |
---|
| 72 | if (*day < *days_to_date) |
---|
| 73 | break; |
---|
| 74 | ++month; |
---|
| 75 | ++days_to_date; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | *day -= *(days_to_date - 1); |
---|
| 79 | |
---|
| 80 | return month; |
---|
| 81 | } |
---|
| 82 | |
---|
[d7d7859] | 83 | rtems_status_code rtems_clock_get_tod( |
---|
| 84 | rtems_time_of_day *time_buffer |
---|
| 85 | ) |
---|
| 86 | { |
---|
| 87 | struct timeval now; |
---|
[eae0863] | 88 | uint32_t days; |
---|
| 89 | uint32_t day_secs; |
---|
| 90 | uint32_t year; |
---|
| 91 | uint32_t year_days; |
---|
| 92 | uint32_t leap_years; |
---|
[d7d7859] | 93 | |
---|
| 94 | if ( !time_buffer ) |
---|
| 95 | return RTEMS_INVALID_ADDRESS; |
---|
| 96 | |
---|
[b915762] | 97 | if ( !_TOD_Is_set() ) |
---|
[d7d7859] | 98 | return RTEMS_NOT_DEFINED; |
---|
| 99 | |
---|
| 100 | /* Obtain the current time */ |
---|
| 101 | _TOD_Get_timeval( &now ); |
---|
| 102 | |
---|
[eae0863] | 103 | /* How many days and how many seconds in the day ? */ |
---|
| 104 | days = now.tv_sec / RTEMS_SECS_PER_DAY; |
---|
| 105 | day_secs = now.tv_sec % RTEMS_SECS_PER_DAY; |
---|
| 106 | |
---|
| 107 | /* How many non-leap year years ? */ |
---|
| 108 | year = ( days / RTEMS_DAYS_PER_YEAR ) + RTEMS_YEAR_BASE; |
---|
| 109 | |
---|
| 110 | /* Determine the number of leap years. */ |
---|
| 111 | leap_years = _Leap_years_between( RTEMS_YEAR_BASE, year ); |
---|
| 112 | |
---|
| 113 | /* Adjust the remaining number of days based on the leap years. */ |
---|
| 114 | year_days = ( days - leap_years ) % RTEMS_DAYS_PER_YEAR; |
---|
| 115 | |
---|
| 116 | /* Adjust the year and days in the year if in the leap year overflow. */ |
---|
| 117 | if ( leap_years > ( days % RTEMS_DAYS_PER_YEAR ) ) { |
---|
| 118 | year -= 1; |
---|
| 119 | if ( _Leap_year( year ) ) { |
---|
| 120 | year_days += 1; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | time_buffer->year = year; |
---|
| 125 | time_buffer->month = _Year_day_as_month( year, &year_days ) + 1; |
---|
| 126 | time_buffer->day = year_days + 1; |
---|
| 127 | time_buffer->hour = day_secs / RTEMS_SECS_PER_HOUR; |
---|
| 128 | time_buffer->minute = day_secs % RTEMS_SECS_PER_HOUR; |
---|
| 129 | time_buffer->second = time_buffer->minute % RTEMS_SECS_PER_MINUTE; |
---|
| 130 | time_buffer->minute = time_buffer->minute / RTEMS_SECS_PER_MINUTE; |
---|
| 131 | time_buffer->ticks = now.tv_usec / |
---|
| 132 | rtems_configuration_get_microseconds_per_tick( ); |
---|
[05c1886] | 133 | |
---|
[d7d7859] | 134 | return RTEMS_SUCCESSFUL; |
---|
| 135 | } |
---|