From be8e1d238149c16d806b7a3434c100be3e1e1c22 Mon Sep 17 00:00:00 2001
From: "Kevin P. Fleming" <kpfleming@digium.com>
Date: Tue, 19 Jul 2005 23:28:12 +0000
Subject: [PATCH] restore proper difference calculation (bug #4746)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@6166 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 include/asterisk/time.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/include/asterisk/time.h b/include/asterisk/time.h
index a10636a697..dbdfb446ad 100755
--- a/include/asterisk/time.h
+++ b/include/asterisk/time.h
@@ -32,7 +32,13 @@ typedef typeof(tv.tv_usec) ast_suseconds_t;
 AST_INLINE_API(
 int ast_tvdiff_ms(struct timeval end, struct timeval start),
 {
-	return ((end.tv_sec - start.tv_sec) * 1000) + ((end.tv_usec - start.tv_usec) / 1000);
+	/* the offset by 1,000,000 below is intentional...
+	   it avoids differences in the way that division
+	   is handled for positive and negative numbers, by ensuring
+	   that the divisor is always positive
+	*/
+	return  ((end.tv_sec - start.tv_sec) * 1000) +
+		(((1000000 + end.tv_usec - start.tv_usec) / 1000) - 1000);
 }
 )
 
-- 
GitLab