2 * Routines for manipulating nstime_t structures
4 * Copyright (c) 2005 MX Telecom Ltd. <richardv@mxtelecom.com>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 /* this is #defined so that we can clearly see that we have the right number of
30 zeros, rather than as a guard against the number of nanoseconds in a second
32 #define NS_PER_S 1000000000
34 /* set the given nstime_t to zero */
35 void nstime_set_zero(nstime_t *nstime)
41 /* is the given nstime_t currently zero? */
42 gboolean nstime_is_zero(nstime_t *nstime)
44 if(nstime->secs == 0 && nstime->nsecs == 0) {
51 /* set the given nstime_t to (0,maxint) to mark it as "unset"
52 * That way we can find the first frame even when a timestamp
53 * is zero (fix for bug 1056)
55 void nstime_set_unset(nstime_t *nstime)
58 nstime->nsecs = G_MAXINT;
61 /* is the given nstime_t currently (0,maxint)? */
62 gboolean nstime_is_unset(nstime_t *nstime)
64 if(nstime->secs == 0 && nstime->nsecs == G_MAXINT) {
72 /** funcion: nstime_copy
76 void nstime_copy(nstime_t *a, const nstime_t *b)
83 * function: nstime_delta
87 void nstime_delta(nstime_t *delta, const nstime_t *b, const nstime_t *a )
89 if (b->secs == a->secs) {
90 /* The seconds part of b is the same as the seconds part of a, so if
91 the nanoseconds part of the first time is less than the nanoseconds
92 part of a, b is before a. The nanoseconds part of the delta should
93 just be the difference between the nanoseconds part of b and the
94 nanoseconds part of a; don't adjust the seconds part of the delta,
95 as it's OK if the nanoseconds part is negative, and an overflow
98 delta->nsecs = b->nsecs - a->nsecs;
99 } else if (b->secs <= a->secs) {
100 /* The seconds part of b is less than the seconds part of a, so b is
103 Both the "seconds" and "nanoseconds" value of the delta
104 should have the same sign, so if the difference between the
105 nanoseconds values would be *positive*, subtract 1,000,000,000
106 from it, and add one to the seconds value. */
107 delta->secs = b->secs - a->secs;
108 delta->nsecs = b->nsecs - a->nsecs;
109 if(delta->nsecs > 0) {
110 delta->nsecs -= NS_PER_S;
114 delta->secs = b->secs - a->secs;
115 delta->nsecs = b->nsecs - a->nsecs;
116 if(delta->nsecs < 0) {
117 delta->nsecs += NS_PER_S;
124 * function: nstime_sum
128 void nstime_sum(nstime_t *sum, const nstime_t *a, const nstime_t *b)
130 sum->secs = a->secs + b->secs;
131 sum->nsecs = a->nsecs + b->nsecs;
132 if(sum->nsecs>=NS_PER_S || (sum->nsecs>0 && sum->secs<0)){
133 sum->nsecs-=NS_PER_S;
135 } else if(sum->nsecs<=-NS_PER_S || (sum->nsecs<0 && sum->secs>0)) {
136 sum->nsecs+=NS_PER_S;
142 * function: nstime_cmp
149 int nstime_cmp (const nstime_t *a, const nstime_t *b )
151 if (a->secs == b->secs) {
152 return a->nsecs - b->nsecs;
154 return (int) (a->secs - b->secs);
159 * function: nstime_to_msec
160 * converts nstime to double, time base is milli seconds
163 double nstime_to_msec(const nstime_t *nstime)
165 return ((double)nstime->secs*1000 + (double)nstime->nsecs/1000000);
169 * function: nstime_to_sec
170 * converts nstime to double, time base is seconds
173 double nstime_to_sec(const nstime_t *nstime)
175 return ((double)nstime->secs + (double)nstime->nsecs/1000000000);
184 * indent-tabs-mode: nil
187 * ex: set shiftwidth=4 tabstop=8 expandtab:
188 * :indentSize=4:tabSize=8:noTabs=true: