2 * Routines for manipulating nstime_t structures
4 * Copyright (c) 2005 MX Telecom Ltd. <richardv@mxtelecom.com>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
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.
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.
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.
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
34 #define NS_PER_S 1000000000
36 /* set the given nstime_t to zero */
37 void nstime_set_zero(nstime_t *nstime)
43 /* is the given nstime_t currently zero? */
44 gboolean nstime_is_zero(nstime_t *nstime)
46 if(nstime->secs == 0 && nstime->nsecs == 0) {
53 /* set the given nstime_t to (0,maxint) to mark it as "unset"
54 * That way we can find the first frame even when a timestamp
55 * is zero (fix for bug 1056)
57 void nstime_set_unset(nstime_t *nstime)
60 nstime->nsecs = G_MAXINT;
63 /* is the given nstime_t currently (0,maxint)? */
64 gboolean nstime_is_unset(nstime_t *nstime)
66 if(nstime->secs == 0 && nstime->nsecs == G_MAXINT) {
75 * function: nstime_delta
79 void nstime_delta(nstime_t *delta, const nstime_t *b, const nstime_t *a )
81 if (b->secs == a->secs) {
82 /* The seconds part of b is the same as the seconds part of a, so if
83 the nanoseconds part of the first time is less than the nanoseconds
84 part of a, b is before a. The nanoseconds part of the delta should
85 just be the difference between the nanoseconds part of b and the
86 nanoseconds part of a; don't adjust the seconds part of the delta,
87 as it's OK if the nanoseconds part is negative, and an overflow
90 delta->nsecs = b->nsecs - a->nsecs;
91 } else if (b->secs <= a->secs) {
92 /* The seconds part of b is less than the seconds part of a, so b is
95 Both the "seconds" and "nanoseconds" value of the delta
96 should have the same sign, so if the difference between the
97 nanoseconds values would be *positive*, subtract 1,000,000,000
98 from it, and add one to the seconds value. */
99 delta->secs = b->secs - a->secs;
100 delta->nsecs = b->nsecs - a->nsecs;
101 if(delta->nsecs > 0) {
102 delta->nsecs -= NS_PER_S;
106 delta->secs = b->secs - a->secs;
107 delta->nsecs = b->nsecs - a->nsecs;
108 if(delta->nsecs < 0) {
109 delta->nsecs += NS_PER_S;
116 * function: nstime_sum
120 void nstime_sum(nstime_t *sum, const nstime_t *a, const nstime_t *b)
122 sum->secs = a->secs + b->secs;
123 sum->nsecs = a->nsecs + b->nsecs;
124 if(sum->nsecs>=NS_PER_S || (sum->nsecs>0 && sum->secs<0)){
125 sum->nsecs-=NS_PER_S;
127 } else if(sum->nsecs<=-NS_PER_S || (sum->nsecs<0 && sum->secs>0)) {
128 sum->nsecs+=NS_PER_S;
134 * function: nstime_cmp
141 int nstime_cmp (nstime_t *a, const nstime_t *b )
143 if (a->secs == b->secs) {
144 return a->nsecs - b->nsecs;
146 return (int) (a->secs - b->secs);
151 * function: nstime_to_msec
152 * converts nstime to double, time base is milli seconds
155 double nstime_to_msec(const nstime_t *time)
157 return ((double)time->secs*1000 + (double)time->nsecs/1000000);
161 * function: nstime_to_sec
162 * converts nstime to double, time base is seconds
165 double nstime_to_sec(const nstime_t *time)
167 return ((double)time->secs + (double)time->nsecs/1000000000);
171 * function: wtap_nstime_to_sec
172 * converts wtap_nstime to double, time base is seconds
175 double wtap_nstime_to_sec(const struct wtap_nstime *time)
177 return ((double)time->secs + (double)time->nsecs/1000000000);
186 * indent-tabs-mode: nil
189 * ex: set shiftwidth=4 tabstop=8 expandtab
190 * :indentSize=4:tabSize=8:noTabs=true: