some compilers dont like unnamed unions and structs
[obnox/wireshark/wip.git] / epan / nstime.c
1 /* nstime.c
2  * Routines for manipulating nstime_t structures
3  *
4  * Copyright (c) 2005 MX Telecom Ltd. <richardv@mxtelecom.com>
5  * 
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  *
26  */
27
28 #include <glib.h>
29 #include "nstime.h"
30
31 /* this is #defined so that we can clearly see that we have the right number of
32    zeros, rather than as a guard against the number of nanoseconds in a second
33    changing ;) */
34 #define NS_PER_S 1000000000
35
36 /* set the given nstime_t to zero */
37 void nstime_set_zero(nstime_t *nstime)
38 {
39         nstime->secs  = 0;
40         nstime->nsecs = 0;
41 }
42
43 /* is the given nstime_t currently zero? */
44 gboolean nstime_is_zero(nstime_t *nstime)
45 {
46         if(nstime->secs == 0 && nstime->nsecs == 0) {
47                 return TRUE;
48         } else {
49                 return FALSE;
50         }
51 }
52
53
54 /*
55  * function: nstime_delta
56  * delta = b - a
57  */
58
59 void nstime_delta(nstime_t *delta, const nstime_t *b, const nstime_t *a )
60 {
61     if (b->secs == a->secs) {
62         /* The seconds part of b is the same as the seconds part of a, so if
63            the nanoseconds part of the first time is less than the nanoseconds
64            part of a, b is before a.  The nanoseconds part of the delta should
65            just be the difference between the nanoseconds part of b and the
66            nanoseconds part of a; don't adjust the seconds part of the delta,
67            as it's OK if the nanoseconds part is negative, and an overflow
68            can never result. */
69         delta->secs = 0;
70         delta->nsecs = b->nsecs - a->nsecs;
71     } else if (b->secs <= a->secs) {
72         /* The seconds part of b is less than the seconds part of a, so b is
73            before a.
74
75            Both the "seconds" and "nanoseconds" value of the delta
76            should have the same sign, so if the difference between the
77            nanoseconds values would be *positive*, subtract 1,000,000,000
78            from it, and add one to the seconds value. */
79         delta->secs = b->secs - a->secs;
80         delta->nsecs = b->nsecs - a->nsecs;
81         if(delta->nsecs > 0) {
82             delta->nsecs -= NS_PER_S;
83             delta->secs ++;
84         }
85     } else {
86         delta->secs = b->secs - a->secs;
87         delta->nsecs = b->nsecs - a->nsecs;
88         if(delta->nsecs < 0) {
89             delta->nsecs += NS_PER_S;
90             delta->secs --;
91         }
92     }
93 }
94
95 /*
96  * function: nstime_sum
97  * sum = a + b
98  */
99
100 void nstime_sum(nstime_t *sum, const nstime_t *a, const nstime_t *b)
101 {
102     sum->secs = a->secs + b->secs;
103     sum->nsecs = a->nsecs + b->nsecs;
104     if(sum->nsecs>=NS_PER_S || (sum->nsecs>0 && sum->secs<0)){
105         sum->nsecs-=NS_PER_S;
106         sum->secs++;
107     } else if(sum->nsecs<=-NS_PER_S || (sum->nsecs<0 && sum->secs>0)) {
108         sum->nsecs+=NS_PER_S;
109         sum->secs--;
110     }    
111 }
112
113 /*
114  * function: nstime_to_msec
115  * converts nstime to double, time base is milli seconds
116  */
117
118 double nstime_to_msec(const nstime_t *time)
119 {
120     return ((double)time->secs*1000 + (double)time->nsecs/1000000);
121 }
122
123 /*
124  * function: nstime_to_sec
125  * converts nstime to double, time base is seconds
126  */
127
128 double nstime_to_sec(const nstime_t *time)
129 {
130     return ((double)time->secs + (double)time->nsecs/1000000000);
131 }
132