Commit f1451490 authored by Ulrich Huber's avatar Ulrich Huber
Browse files

[add] timespec handling

parent 051e3dd6
......@@ -6,14 +6,30 @@
#define TOKI_TIME_H
typedef signed long time_t;
#define TIME_MAX ((s64)~((u64)1 << 63))
/* Parameters used to convert the timespec values: */
#define MSEC_PER_SEC 1000L
#define USEC_PER_MSEC 1000L
#define NSEC_PER_USEC 1000L
#define NSEC_PER_MSEC 1000000L
#define USEC_PER_SEC 1000000L
#define NSEC_PER_SEC 1000000000L
#define FSEC_PER_SEC 1000000000000000LL
struct timespec {
long tv_sec; /* seconds */
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
struct timespec timespec_add(struct timespec lhs, struct timespec rhs);
struct timespec timespec_sub(struct timespec lhs, struct timespec rhs);
struct timespec timespec_div(struct timespec ts, long div);
void normalize_timespec(struct timespec *ts);
struct timeval {
long tv_sec; /* seconds */
time_t tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
......
......@@ -5,3 +5,4 @@ add_subdirectory(math)
add_subdirectory(printf)
add_subdirectory(stdlib)
add_subdirectory(string)
add_subdirectory(time)
cmake_minimum_required(VERSION 3.7 FATAL_ERROR)
target_sources(
toki-libc-repl
#
PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/timespec.c"
)
BSD with Attribution License
Copyright (c) 2015-2020 fortiss GmbH - Research Institute of the
Free State of Bavaria, Guerickestr. 25, 80805 Munich, Germany
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.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
4. Redistributions of any form whatsoever must retain the following
acknowledgment: 'This product includes software developed by the
fortiss GmbH -- Research Institute of the Free State of Bavaria,
Munich, Germany (https://www.fortiss.org/).'
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
HOLDER 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.
SPDX-License-Identifier: BSD-3-Clause-Attribution
#include <sys/time.h>
/**
* Normalize the timespec structure to always positive nanoseconds
* in the range of [0, NSEC_PER_SEC).
*/
void normalize_timespec(struct timespec *ts)
{
// Take care of seconds stored in nsecs
while (ts->tv_nsec >= NSEC_PER_SEC) {
ts->tv_nsec -= NSEC_PER_SEC;
++ts->tv_sec;
}
// Take care of negative nsecs
while (ts->tv_nsec < 0) {
ts->tv_nsec += NSEC_PER_SEC;
--ts->tv_sec;
}
}
/**
* Add rhs to lhs (result = lhs + rhs) and normalize timespec
*
* @param lhs The first timespec to add
* @param rhs The second timespec to add
*
* @return The result of the equation `lhs + rhs`
*/
inline struct timespec timespec_add(struct timespec lhs, struct timespec rhs)
{
struct timespec result;
result.tv_sec = lhs.tv_sec + rhs.tv_sec;
result.tv_nsec = lhs.tv_nsec + rhs.tv_nsec;
normalize_timespec(&result);
return result;
}
/**
* Subtract rhs from lhs (result = lhs - rhs) and normalize timespec.
*
* @param lhs The timespec to subtract from
* @param rhs The timespec to subtract
*
* @return The result of the equation `lhs - rhs`
*/
inline struct timespec timespec_sub(struct timespec lhs, struct timespec rhs)
{
struct timespec result;
result.tv_sec = lhs.tv_sec - rhs.tv_sec;
result.tv_nsec = lhs.tv_nsec - rhs.tv_nsec;
normalize_timespec(&result);
return result;
}
/**
* Divide a timespec by a constant and normalize it.
* @param ts The timespec to divide
* @param div The divisor
* @return The result of `ts / div`
*/
inline struct timespec timespec_div(struct timespec ts, long div)
{
struct timespec result;
result.tv_sec = ts.tv_sec / div;
result.tv_nsec = ts.tv_nsec / div;
result.tv_nsec += (ts.tv_sec % div) * (NSEC_PER_SEC / div);
normalize_timespec(&result);
return result;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment