summaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
authorKevin P. Fleming <kpfleming@digium.com>2005-07-15 23:00:47 +0000
committerKevin P. Fleming <kpfleming@digium.com>2005-07-15 23:00:47 +0000
commit22b0f5d30691e94b18e152ec44ed90a23f55b266 (patch)
tree896015cada90b49858e1bf6503d9583704a56179 /utils.c
parent60cd1fa57deeb80b7a564fd81194797b503d4846 (diff)
add a library of timeval manipulation functions, and change a large number of usses to use the new functions (bug #4504)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@6146 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'utils.c')
-rwxr-xr-xutils.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/utils.c b/utils.c
index 1ab8c690d..2043e6286 100755
--- a/utils.c
+++ b/utils.c
@@ -493,6 +493,55 @@ int ast_false(const char *s)
return 0;
}
+#define ONE_MILLION 1000000
+/*
+ * put timeval in a valid range. usec is 0..999999
+ * negative values are not allowed and truncated.
+ */
+static struct timeval tvfix(struct timeval a)
+{
+ if (a.tv_usec >= ONE_MILLION) {
+ ast_log(LOG_ERROR, "warning too large timestamp %ld.%ld\n",
+ a.tv_sec, a.tv_usec);
+ a.tv_sec += a.tv_usec % ONE_MILLION;
+ a.tv_usec %= ONE_MILLION;
+ } else if (a.tv_usec < 0) {
+ ast_log(LOG_ERROR, "warning negative timestamp %ld.%ld\n",
+ a.tv_sec, a.tv_usec);
+ a.tv_usec = 0;
+ }
+ return a;
+}
+
+struct timeval ast_tvadd(struct timeval a, struct timeval b)
+{
+ /* consistency checks to guarantee usec in 0..999999 */
+ a = tvfix(a);
+ b = tvfix(b);
+ a.tv_sec += b.tv_sec;
+ a.tv_usec += b.tv_usec;
+ if (a.tv_usec >= ONE_MILLION) {
+ a.tv_sec++;
+ a.tv_usec -= ONE_MILLION;
+ }
+ return a;
+}
+
+struct timeval ast_tvsub(struct timeval a, struct timeval b)
+{
+ /* consistency checks to guarantee usec in 0..999999 */
+ a = tvfix(a);
+ b = tvfix(b);
+ a.tv_sec -= b.tv_sec;
+ a.tv_usec -= b.tv_usec;
+ if (a.tv_usec < 0) {
+ a.tv_sec-- ;
+ a.tv_usec += ONE_MILLION;
+ }
+ return a;
+}
+#undef ONE_MILLION
+
#ifndef HAVE_STRCASESTR
static char *upper(const char *orig, char *buf, int bufsize)
{