diff options
| author | alex <alex@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2007-07-19 10:36:05 +0000 |
|---|---|---|
| committer | alex <alex@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2007-07-19 10:36:05 +0000 |
| commit | a9bb4776d6000ced0fdbcbee731a13d5ebf1922c (patch) | |
| tree | bb55302f74ab87da31abcc755de006abef89ee9d /src/libs | |
| parent | b333c78f6f1754b469019f5e25f0a700d0c47474 (diff) | |
New function time_diff() (Alexei)
git-svn-id: svn://svn.zabbix.com/trunk@4461 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src/libs')
| -rw-r--r-- | src/libs/zbxcommon/Makefile.am | 1 | ||||
| -rw-r--r-- | src/libs/zbxcommon/time.c | 72 |
2 files changed, 73 insertions, 0 deletions
diff --git a/src/libs/zbxcommon/Makefile.am b/src/libs/zbxcommon/Makefile.am index 65d2c90d..e64cb608 100644 --- a/src/libs/zbxcommon/Makefile.am +++ b/src/libs/zbxcommon/Makefile.am @@ -8,6 +8,7 @@ libzbxcommon_a_SOURCES = \ xml.c \ comms.c \ str.c \ + time.c \ zbxgetopt.c \ alias.c diff --git a/src/libs/zbxcommon/time.c b/src/libs/zbxcommon/time.c new file mode 100644 index 00000000..cd8ade35 --- /dev/null +++ b/src/libs/zbxcommon/time.c @@ -0,0 +1,72 @@ +/* +** ZABBIX +** Copyright (C) 2000-2005 SIA Zabbix +** +** 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., 675 Mass Ave, Cambridge, MA 02139, USA. +**/ + +#include "common.h" + +#include <sys/time.h> +#include <time.h> + +/****************************************************************************** + * * + * Function: time_diff * + * * + * Purpose: calculate time difference in seconds * + * * + * Author: Alexei Vladishev * + * * + * Comments: * + * * + ******************************************************************************/ +double time_diff(struct timeval *from, struct timeval *to) +{ + double msec; + double diff; + + /* from<=to */ + if( (from->tv_sec < to->tv_sec) || (from->tv_sec == to->tv_sec && from->tv_usec <= to->tv_usec)) + { + msec = (double)(to->tv_usec-from->tv_usec)/1000000; + + if(msec >= 0) + { + diff = to->tv_sec - from->tv_sec + msec; + } + else + { + diff = to->tv_sec - from->tv_sec - (msec + 1); + } + } + /* from>to */ + else + { + msec = (double)(from->tv_usec-to->tv_usec)/1000000; + + if(msec >= 0) + { + diff = from->tv_sec - to->tv_sec + msec; + } + else + { + diff = from->tv_sec - to->tv_sec - (msec + 1); + } + diff = 0.0 - diff; + } + + return diff; +} |
