1 | /* |
---|
2 | * Time of Day (TOD) Handler -- Validate Classic TOD |
---|
3 | * |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-2007. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.com/license/LICENSE. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #if HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <rtems/system.h> |
---|
20 | #include <rtems/config.h> |
---|
21 | #include <rtems/rtems/clock.h> |
---|
22 | |
---|
23 | /* |
---|
24 | * The following array contains the number of days in all months. |
---|
25 | * The first dimension should be 1 for leap years, and 0 otherwise. |
---|
26 | * The second dimension should range from 1 to 12 for January to |
---|
27 | * February, respectively. |
---|
28 | */ |
---|
29 | const uint32_t _TOD_Days_per_month[ 2 ][ 13 ] = { |
---|
30 | { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, |
---|
31 | { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } |
---|
32 | }; |
---|
33 | |
---|
34 | /*PAGE |
---|
35 | * |
---|
36 | * _TOD_Validate |
---|
37 | * |
---|
38 | * This kernel routine checks the validity of a date and time structure. |
---|
39 | * |
---|
40 | * Input parameters: |
---|
41 | * the_tod - pointer to a time and date structure |
---|
42 | * |
---|
43 | * Output parameters: |
---|
44 | * true - if the date, time, and tick are valid |
---|
45 | * false - if the the_tod is invalid |
---|
46 | * |
---|
47 | * NOTE: This routine only works for leap-years through 2099. |
---|
48 | */ |
---|
49 | |
---|
50 | bool _TOD_Validate( |
---|
51 | rtems_time_of_day *the_tod |
---|
52 | ) |
---|
53 | { |
---|
54 | uint32_t days_in_month; |
---|
55 | uint32_t ticks_per_second; |
---|
56 | |
---|
57 | ticks_per_second = TOD_MICROSECONDS_PER_SECOND / |
---|
58 | rtems_configuration_get_microseconds_per_tick(); |
---|
59 | if ((!the_tod) || |
---|
60 | (the_tod->ticks >= ticks_per_second) || |
---|
61 | (the_tod->second >= TOD_SECONDS_PER_MINUTE) || |
---|
62 | (the_tod->minute >= TOD_MINUTES_PER_HOUR) || |
---|
63 | (the_tod->hour >= TOD_HOURS_PER_DAY) || |
---|
64 | (the_tod->month == 0) || |
---|
65 | (the_tod->month > TOD_MONTHS_PER_YEAR) || |
---|
66 | (the_tod->year < TOD_BASE_YEAR) || |
---|
67 | (the_tod->day == 0) ) |
---|
68 | return false; |
---|
69 | |
---|
70 | if ( (the_tod->year % 4) == 0 ) |
---|
71 | days_in_month = _TOD_Days_per_month[ 1 ][ the_tod->month ]; |
---|
72 | else |
---|
73 | days_in_month = _TOD_Days_per_month[ 0 ][ the_tod->month ]; |
---|
74 | |
---|
75 | if ( the_tod->day > days_in_month ) |
---|
76 | return false; |
---|
77 | |
---|
78 | return true; |
---|
79 | } |
---|