summaryrefslogtreecommitdiffstats
path: root/cpukit/rtems/src/clockgettod.c
diff options
context:
space:
mode:
Diffstat (limited to 'cpukit/rtems/src/clockgettod.c')
-rw-r--r--cpukit/rtems/src/clockgettod.c62
1 files changed, 42 insertions, 20 deletions
diff --git a/cpukit/rtems/src/clockgettod.c b/cpukit/rtems/src/clockgettod.c
index dea136d477..06d8edbee9 100644
--- a/cpukit/rtems/src/clockgettod.c
+++ b/cpukit/rtems/src/clockgettod.c
@@ -1,3 +1,5 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+
/**
* @file
*
@@ -11,9 +13,26 @@
* COPYRIGHT (c) 1989-2007.
* On-Line Applications Research Corporation (OAR).
*
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rtems.org/license/LICENSE.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
@@ -32,8 +51,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 +81,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;
@@ -83,7 +100,7 @@ static uint32_t _Year_day_as_month(
}
rtems_status_code rtems_clock_get_tod(
- rtems_time_of_day *time_buffer
+ rtems_time_of_day *time_of_day
)
{
struct timeval now;
@@ -93,7 +110,7 @@ rtems_status_code rtems_clock_get_tod(
uint32_t year_days;
uint32_t leap_years;
- if ( !time_buffer )
+ if ( !time_of_day )
return RTEMS_INVALID_ADDRESS;
if ( !_TOD_Is_set() )
@@ -102,9 +119,14 @@ rtems_status_code rtems_clock_get_tod(
/* Obtain the current time */
_TOD_Get_timeval( &now );
- /* How many days and how many seconds in the day ? */
- days = now.tv_sec / RTEMS_SECS_PER_DAY;
- day_secs = now.tv_sec % RTEMS_SECS_PER_DAY;
+ /*
+ * How many days and how many seconds in the day?
+ *
+ * A 32-bit integer can represent enough days for several 1000 years. When
+ * the current time is valid, the integer conversions below are well defined.
+ */
+ days = (uint32_t) ( now.tv_sec / RTEMS_SECS_PER_DAY );
+ day_secs = (uint32_t) ( now.tv_sec % RTEMS_SECS_PER_DAY );
/* How many non-leap year years ? */
year = ( days / RTEMS_DAYS_PER_YEAR ) + RTEMS_YEAR_BASE;
@@ -123,14 +145,14 @@ rtems_status_code rtems_clock_get_tod(
}
}
- time_buffer->year = year;
- time_buffer->month = _Year_day_as_month( year, &year_days ) + 1;
- time_buffer->day = year_days + 1;
- time_buffer->hour = day_secs / RTEMS_SECS_PER_HOUR;
- time_buffer->minute = day_secs % RTEMS_SECS_PER_HOUR;
- time_buffer->second = time_buffer->minute % RTEMS_SECS_PER_MINUTE;
- time_buffer->minute = time_buffer->minute / RTEMS_SECS_PER_MINUTE;
- time_buffer->ticks = now.tv_usec /
+ time_of_day->year = year;
+ time_of_day->month = _Year_day_as_month( year, &year_days ) + 1;
+ time_of_day->day = year_days + 1;
+ time_of_day->hour = day_secs / RTEMS_SECS_PER_HOUR;
+ time_of_day->minute = day_secs % RTEMS_SECS_PER_HOUR;
+ time_of_day->second = time_of_day->minute % RTEMS_SECS_PER_MINUTE;
+ time_of_day->minute = time_of_day->minute / RTEMS_SECS_PER_MINUTE;
+ time_of_day->ticks = now.tv_usec /
rtems_configuration_get_microseconds_per_tick( );
return RTEMS_SUCCESSFUL;