Move the functions to diff the time to the stdlib.

This commit is contained in:
Andreas Schneider 2008-05-19 17:21:12 +02:00
Родитель 476fd5d00e
Коммит c6b2b46710
6 изменённых файлов: 122 добавлений и 46 удалений

Просмотреть файл

@ -116,45 +116,3 @@ out:
return timediff;
}
struct timespec csync_tsub(struct timespec timespec1, struct timespec timespec2) {
struct timespec ret;
int xsec = 0;
int sign = 1;
if (timespec2.tv_nsec > timespec1.tv_nsec) {
xsec = (int) ((timespec2.tv_nsec - timespec1.tv_nsec) / (1E9 + 1));
timespec2.tv_nsec -= (long int) (1E9 * xsec);
timespec2.tv_sec += xsec;
}
if ((timespec1.tv_nsec - timespec2.tv_nsec) > 1E9) {
xsec = (int) ((timespec1.tv_nsec - timespec2.tv_nsec) / 1E9);
timespec2.tv_nsec += (long int) (1E9 * xsec);
timespec2.tv_sec -= xsec;
}
ret.tv_sec = timespec1.tv_sec - timespec2.tv_sec;
ret.tv_nsec = timespec1.tv_nsec - timespec2.tv_nsec;
if (timespec1.tv_sec < timespec2.tv_sec) {
sign = -1;
}
ret.tv_sec = ret.tv_sec * sign;
return ret;
}
double csync_secdiff(struct timespec clock1, struct timespec clock2) {
double ret;
struct timespec diff;
diff = csync_tsub(clock1, clock2);
ret = diff.tv_sec;
ret += (double) diff.tv_nsec / (double) 1E9;
return ret;
}

Просмотреть файл

@ -29,8 +29,4 @@
time_t csync_timediff(CSYNC *ctx);
struct timespec csync_tsub(struct timespec timespec_1,struct timespec timespec_2);
double csync_secdiff(struct timespec clock_1, struct timespec clock_2);
#endif /* _CSYNC_TIME_H */

Просмотреть файл

@ -24,6 +24,7 @@ set(cstdlib_SRCS
c_path.c
c_rbtree.c
c_string.c
c_time.c
)
include_directories(

Просмотреть файл

@ -27,3 +27,4 @@
#include "c_path.h"
#include "c_rbtree.h"
#include "c_string.h"
#include "c_time.h"

65
src/std/c_time.c Normal file
Просмотреть файл

@ -0,0 +1,65 @@
/*
* c_time - time functions
*
* Copyright (c) 2008 by Andreas Schneider <mail@cynapses.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* vim: ts=2 sw=2 et cindent
*/
#include "c_time.h"
struct timespec c_tspecdiff(struct timespec time1, struct timespec time0) {
struct timespec ret;
int xsec = 0;
int sign = 1;
if (time0.tv_nsec > time1.tv_nsec) {
xsec = (int) ((time0.tv_nsec - time1.tv_nsec) / (1E9 + 1));
time0.tv_nsec -= (long int) (1E9 * xsec);
time0.tv_sec += xsec;
}
if ((time1.tv_nsec - time0.tv_nsec) > 1E9) {
xsec = (int) ((time1.tv_nsec - time0.tv_nsec) / 1E9);
time0.tv_nsec += (long int) (1E9 * xsec);
time0.tv_sec -= xsec;
}
ret.tv_sec = time1.tv_sec - time0.tv_sec;
ret.tv_nsec = time1.tv_nsec - time0.tv_nsec;
if (time1.tv_sec < time0.tv_sec) {
sign = -1;
}
ret.tv_sec = ret.tv_sec * sign;
return ret;
}
double c_secdiff(struct timespec clock1, struct timespec clock0) {
double ret;
struct timespec diff;
diff = c_tspecdiff(clock1, clock0);
ret = diff.tv_sec;
ret += (double) diff.tv_nsec / (double) 1E9;
return ret;
}

55
src/std/c_time.h Normal file
Просмотреть файл

@ -0,0 +1,55 @@
/*
* c_time - time functions
*
* Copyright (c) 2008 by Andreas Schneider <mail@cynapses.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* vim: ft=c.doxygen ts=2 sw=2 et cindent
*/
#ifndef _C_TIME_H
#define _C_TIME_H
#include <time.h>
/**
* @brief Calculate time difference
*
* The c_tspecdiff function returns the time elapsed between time time1 and time
* time0 represented as timespec.
*
* @param time1 The time.
* @param time0 The time.
*
* @return time elapsed between time1 and time0.
*/
struct timespec c_tspecdiff(struct timespec time1, struct timespec time0);
/**
* @brief Calculate time difference.
*
* The function returns the time elapsed between time clock1 and time
* clock0 represented as double (in seconds and milliseconds).
*
* @param clock1 The time.
* @param clock0 The time.
*
* @return time elapsed between clock1 and clock0 in seconds and
* milliseconds.
*/
double c_secdiff(struct timespec clock1, struct timespec clock0);
#endif /* _C_TIME_H */