Wireshark  4.3.0
The Wireshark network protocol analyzer
nstime.h
Go to the documentation of this file.
1 /* nstime.h
2  * Definition of data structure to hold time values with nanosecond resolution
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <[email protected]>
6  * Copyright 1998 Gerald Combs
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10 
11 #ifndef __NSTIME_H__
12 #define __NSTIME_H__
13 
14 #include <wireshark.h>
15 #include <time.h>
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif /* __cplusplus */
20 
26 typedef struct {
27  time_t secs;
28  int nsecs;
29 } nstime_t;
30 
31 /* Macros that expand to nstime_t initializers */
32 
33 /* Initialize to zero */
34 #define NSTIME_INIT_ZERO {0, 0}
35 
36 /* Initialize to unset */
37 #define NSTIME_INIT_UNSET {0, INT_MAX}
38 
39 /* Initialize to a specified number of seconds and nanoseconds */
40 #define NSTIME_INIT_SECS_NSECS(secs, nsecs) {secs, nsecs}
41 
42 /* Initialize to a specified number of seconds and microseconds */
43 #define NSTIME_INIT_SECS_USECS(secs, usecs) {secs, usecs*1000}
44 
45 /* Initialize to a specified number of seconds and milliseconds */
46 #define NSTIME_INIT_SECS_MSECS(secs, msecs) {secs, msecs*1000000}
47 
48 /* Initialize to a specified number of seconds */
49 #define NSTIME_INIT_SECS(secs) {secs, 0}
50 
51 /* Initialize to the maximum possible value */
52 #define NSTIME_INIT_MAX {sizeof(time_t) > sizeof(int) ? LONG_MAX : INT_MAX, INT_MAX}
53 
54 /* functions */
55 
57 WS_DLL_PUBLIC void nstime_set_zero(nstime_t *nstime);
58 
60 WS_DLL_PUBLIC bool nstime_is_zero(const nstime_t *nstime);
61 
66 WS_DLL_PUBLIC void nstime_set_unset(nstime_t *nstime);
67 
68 /* is the given nstime_t currently (0,maxint)? */
69 WS_DLL_PUBLIC bool nstime_is_unset(const nstime_t *nstime);
70 
75 WS_DLL_PUBLIC void nstime_copy(nstime_t *a, const nstime_t *b);
76 
84 WS_DLL_PUBLIC void nstime_delta(nstime_t *delta, const nstime_t *b, const nstime_t *a );
85 
93 WS_DLL_PUBLIC void nstime_sum(nstime_t *sum, const nstime_t *a, const nstime_t *b );
94 
96 #define nstime_add(sum, a) nstime_sum(sum, sum, a)
97 
99 #define nstime_subtract(sum, a) nstime_delta(sum, sum, a)
100 
107 WS_DLL_PUBLIC int nstime_cmp (const nstime_t *a, const nstime_t *b );
108 
109 WS_DLL_PUBLIC unsigned nstime_hash(const nstime_t *nstime);
110 
112 WS_DLL_PUBLIC double nstime_to_msec(const nstime_t *nstime);
113 
115 WS_DLL_PUBLIC double nstime_to_sec(const nstime_t *nstime);
116 
119 WS_DLL_PUBLIC bool filetime_to_nstime(nstime_t *nstime, uint64_t filetime);
120 
124 WS_DLL_PUBLIC bool nsfiletime_to_nstime(nstime_t *nstime, uint64_t nsfiletime);
125 
126 typedef enum {
127  ISO8601_DATETIME,
130 } iso8601_fmt_e;
131 
135 WS_DLL_PUBLIC const char * iso8601_to_nstime(nstime_t *nstime, const char *ptr, iso8601_fmt_e format);
136 
140 WS_DLL_PUBLIC const char * unix_epoch_to_nstime(nstime_t *nstime, const char *ptr);
141 
142 #define NSTIME_ISO8601_BUFSIZE sizeof("YYYY-MM-DDTHH:MM:SS.123456789Z")
143 
144 WS_DLL_PUBLIC size_t nstime_to_iso8601(char *buf, size_t buf_size, const nstime_t *nstime);
145 
146 /* 64 bit signed number plus nanosecond fractional part */
147 #define NSTIME_UNIX_BUFSIZE (20+10+1)
148 
149 WS_DLL_PUBLIC void nstime_to_unix(char *buf, size_t buf_size, const nstime_t *nstime);
150 
151 /*
152  * Timestamp precision values.
153  *
154  * The value is the number of digits of precision after the integral part.
155  */
156 typedef enum {
157  WS_TSPREC_SEC = 0,
158  WS_TSPREC_100_MSEC = 1,
159  WS_TSPREC_10_MSEC = 2,
160  WS_TSPREC_MSEC = 3,
161  WS_TSPREC_100_USEC = 4,
162  WS_TSPREC_10_USEC = 5,
163  WS_TSPREC_USEC = 6,
164  WS_TSPREC_100_NSEC = 7,
165  WS_TSPREC_10_NSEC = 8,
166  WS_TSPREC_NSEC = 9
167 } ws_tsprec_e;
168 
169 /*
170  * Maximum time stamp precision supported.
171  * Note that going beyond nanosecond precision would require expanding
172  * the fractional part of an nstime_t to 64 bits, and changing code
173  * that currently only handles second to nanosecond precision.
174  */
175 #define WS_TSPREC_MAX 9
176 
177 /*
178  * Total number of valid precision values.
179  */
180 #define NUM_WS_TSPREC_VALS (WS_TSPREC_MAX + 1)
181 
182 #ifdef __cplusplus
183 }
184 #endif /* __cplusplus */
185 
186 #endif /* __NSTIME_H__ */
WS_DLL_PUBLIC double nstime_to_msec(const nstime_t *nstime)
Definition: nstime.c:170
WS_DLL_PUBLIC void nstime_sum(nstime_t *sum, const nstime_t *a, const nstime_t *b)
Definition: nstime.c:117
WS_DLL_PUBLIC void nstime_set_unset(nstime_t *nstime)
Definition: nstime.c:44
WS_DLL_PUBLIC void nstime_delta(nstime_t *delta, const nstime_t *b, const nstime_t *a)
Definition: nstime.c:76
WS_DLL_PUBLIC void nstime_set_zero(nstime_t *nstime)
Definition: nstime.c:28
WS_DLL_PUBLIC bool nsfiletime_to_nstime(nstime_t *nstime, uint64_t nsfiletime)
Definition: nstime.c:263
iso8601_fmt_e
Definition: nstime.h:126
@ ISO8601_DATETIME_AUTO
Definition: nstime.h:129
@ ISO8601_DATETIME_BASIC
Definition: nstime.h:128
WS_DLL_PUBLIC int nstime_cmp(const nstime_t *a, const nstime_t *b)
Definition: nstime.c:138
WS_DLL_PUBLIC bool nstime_is_zero(const nstime_t *nstime)
Definition: nstime.c:35
WS_DLL_PUBLIC bool filetime_to_nstime(nstime_t *nstime, uint64_t filetime)
Definition: nstime.c:239
WS_DLL_PUBLIC void nstime_copy(nstime_t *a, const nstime_t *b)
Definition: nstime.c:65
WS_DLL_PUBLIC double nstime_to_sec(const nstime_t *nstime)
Definition: nstime.c:180
WS_DLL_PUBLIC const char * iso8601_to_nstime(nstime_t *nstime, const char *ptr, iso8601_fmt_e format)
Definition: nstime.c:304
WS_DLL_PUBLIC const char * unix_epoch_to_nstime(nstime_t *nstime, const char *ptr)
Definition: nstime.c:525
Definition: nstime.h:26