| Wireshark 4.7.3 The Wireshark network protocol analyzer

Loading...
Searching...
No Matches
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 <gerald@wireshark.org>
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
24
26 typedef struct {
27 time_t secs;
28 int nsecs;
29} nstime_t;
30
31/*
32 * Compute the minimum and maximum time_t values.
33 * (Note: this does not guarantee that any particular C standard library
34 * function taking a time_t will return a valid result with these values;
35 * e.g., asctime has undefined behavior if the year exceeds 4 digits or
36 * is less than 1000, the struct tm tm_year member is defined as an int,
37 * Windows defines _MAX__TIME64_T and its localtime only allows dates
38 * through Jan 3001, etc.)
39 *
40 * This code is based on the Samba code:
41 *
42 * Unix SMB/Netbios implementation.
43 * Version 1.9.
44 * time handling functions
45 * Copyright (C) Andrew Tridgell 1992-1998
46 */
47
48#ifndef TIME_T_MIN
49#define TIME_T_MIN ((time_t) ((time_t)0 < (time_t) -1 ? (time_t) 0 \
50 : (time_t) (~0ULL << (sizeof (time_t) * CHAR_BIT - 1))))
51#endif
52#ifndef TIME_T_MAX
53#define TIME_T_MAX ((time_t) (~ (time_t) 0 - TIME_T_MIN))
54#endif
55
56/*
57 * Macros for conversion between time units.
58 *
59 * These are #defined so that we can clearly see that we have the right number
60 * of zeros, rather than as a guard against the numbers changing. ;)
61 */
62#define WS_MSECS_PER_SEC (1000)
63#define WS_USECS_PER_SEC (1000*1000)
64#define WS_NSECS_PER_SEC (1000*1000*1000)
65
66#define WS_NSECS_PER_MSEC (WS_NSECS_PER_SEC/WS_MSECS_PER_SEC)
67#define WS_NSECS_PER_USEC (WS_NSECS_PER_SEC/WS_USECS_PER_SEC)
68
69#define WS_NSECS_PER_100NSEC (100)
70#define WS_100NSECS_PER_SEC (WS_NSECS_PER_SEC/WS_NSECS_PER_100NSEC)
71
72/* Macros that expand to nstime_t initializers */
73
74/* Initialize to zero */
75#define NSTIME_INIT_ZERO {0, 0}
76
77/* Initialize to unset */
78#define NSTIME_INIT_UNSET {0, INT_MAX}
79
80/*
81 * Initialize to a specified number of seconds and nanoseconds.
82 * This does not assume that the number of nanoseconds is < 1 second.
83 */
84#define NSTIME_INIT_SECS_NSECS(secs, nsecs) {(secs) + ((nsecs) / WS_NSECS_PER_SEC), (nsecs) % WS_NSECS_PER_SEC}
85
86/*
87 * Initialize to a specified number of seconds and microseconds.
88 * This does not assume that the number of microseconds is < 1 second.
89 */
90#define NSTIME_INIT_SECS_USECS(secs, usecs) {(secs) + ((usecs) / WS_USECS_PER_SEC), ((usecs) % WS_USECS_PER_SEC) * WS_NSECS_PER_USEC}
91
92/*
93 * Initialize to a specified number of seconds and milliseconds.
94 * This does not assume that the number of milliseconds is < 1 second.
95 */
96#define NSTIME_INIT_SECS_MSECS(secs, msecs) {(secs) + ((msecs) / WS_MSECS_PER_SEC), ((msecs) % WS_MSECS_PER_SEC) * WS_NSECS_PER_MSEC}
97
98/* Initialize to a specified number of seconds */
99#define NSTIME_INIT_SECS(secs) {secs, 0}
100
101/* Initialize to the maximum possible value */
102#define NSTIME_INIT_MAX {TIME_T_MAX, INT_MAX}
103
104/* functions */
105
113WS_DLL_PUBLIC void nstime_set_zero(nstime_t *nstime);
114
123WS_DLL_PUBLIC bool nstime_is_zero(const nstime_t *nstime);
124
133WS_DLL_PUBLIC bool nstime_is_negative(const nstime_t *nstime);
134
145WS_DLL_PUBLIC void nstime_set_unset(nstime_t *nstime);
146
155WS_DLL_PUBLIC bool nstime_is_unset(const nstime_t *nstime);
156
166WS_DLL_PUBLIC void nstime_copy(nstime_t *a, const nstime_t *b);
167
182WS_DLL_PUBLIC void nstime_delta(nstime_t *delta, const nstime_t *b, const nstime_t *a);
183
197WS_DLL_PUBLIC void nstime_sum(nstime_t *sum, const nstime_t *a, const nstime_t *b);
198
208#define nstime_add(sum, a) nstime_sum(sum, sum, a)
209
219#define nstime_subtract(sum, a) nstime_delta(sum, sum, a)
220
233WS_DLL_PUBLIC int nstime_cmp(const nstime_t *a, const nstime_t *b);
234
243WS_DLL_PUBLIC unsigned nstime_hash(const nstime_t *nstime);
244
253 static inline double nstime_to_msec(const nstime_t *nstime)
254{
255 return ((double)nstime->secs*WS_MSECS_PER_SEC + (double)nstime->nsecs/WS_NSECS_PER_MSEC);
256}
257
266 static inline double nstime_to_sec(const nstime_t *nstime)
267{
268 return ((double)nstime->secs + (double)nstime->nsecs/WS_NSECS_PER_SEC);
269}
270
281WS_DLL_PUBLIC bool filetime_to_nstime(nstime_t *nstime, uint64_t filetime);
282
293WS_DLL_PUBLIC bool filetime_ns_to_nstime(nstime_t *nstime, uint64_t nsfiletime);
294
304WS_DLL_PUBLIC bool filetime_1sec_to_nstime(nstime_t *nstime, uint64_t filetime);
305
309 typedef enum {
310 ISO8601_DATETIME,
311 ISO8601_DATETIME_BASIC,
312 ISO8601_DATETIME_AUTO,
313} iso8601_fmt_e;
314
330WS_DLL_PUBLIC const char * iso8601_to_nstime(nstime_t *nstime, const char *ptr, iso8601_fmt_e format);
331
342WS_DLL_PUBLIC const char * unix_epoch_to_nstime(nstime_t *nstime, const char *ptr);
343
350#define NSTIME_ISO8601_BUFSIZE sizeof("YYYY-MM-DDTHH:MM:SS.123456789Z")
351
362WS_DLL_PUBLIC size_t nstime_to_iso8601(char *buf, size_t buf_size, const nstime_t *nstime);
363
370#define NSTIME_UNIX_BUFSIZE (20+10+1)
371
382WS_DLL_PUBLIC void nstime_to_unix(char *buf, size_t buf_size, const nstime_t *nstime);
383
392 typedef enum {
393 WS_TSPREC_SEC = 0,
394 WS_TSPREC_100_MSEC = 1,
395 WS_TSPREC_10_MSEC = 2,
396 WS_TSPREC_MSEC = 3,
397 WS_TSPREC_100_USEC = 4,
398 WS_TSPREC_10_USEC = 5,
399 WS_TSPREC_USEC = 6,
400 WS_TSPREC_100_NSEC = 7,
401 WS_TSPREC_10_NSEC = 8,
402 WS_TSPREC_NSEC = 9
403} ws_tsprec_e;
404
412#define WS_TSPREC_MAX 9
413
420#define NUM_WS_TSPREC_VALS (WS_TSPREC_MAX + 1)
421
433WS_DLL_PUBLIC void nstime_rounded(nstime_t *a, const nstime_t *b, ws_tsprec_e prec);
434
444#define nstime_round(a, prec) nstime_rounded(a, a, prec)
445
446#ifdef __cplusplus
447}
448#endif /* __cplusplus */
449
450#endif /* __NSTIME_H__ */
ws_tsprec_e
ws_tsprec_e
Timestamp precision levels.
Definition nstime.h:392
WS_TSPREC_USEC
@ WS_TSPREC_USEC
Definition nstime.h:399
WS_TSPREC_100_NSEC
@ WS_TSPREC_100_NSEC
Definition nstime.h:400
WS_TSPREC_100_MSEC
@ WS_TSPREC_100_MSEC
Definition nstime.h:394
WS_TSPREC_100_USEC
@ WS_TSPREC_100_USEC
Definition nstime.h:397
WS_TSPREC_NSEC
@ WS_TSPREC_NSEC
Definition nstime.h:402
WS_TSPREC_MSEC
@ WS_TSPREC_MSEC
Definition nstime.h:396
WS_TSPREC_10_MSEC
@ WS_TSPREC_10_MSEC
Definition nstime.h:395
WS_TSPREC_10_USEC
@ WS_TSPREC_10_USEC
Definition nstime.h:398
WS_TSPREC_SEC
@ WS_TSPREC_SEC
Definition nstime.h:393
WS_TSPREC_10_NSEC
@ WS_TSPREC_10_NSEC
Definition nstime.h:401
nstime_sum
WS_DLL_PUBLIC void nstime_sum(nstime_t *sum, const nstime_t *a, const nstime_t *b)
Calculates the sum of two time values.
Definition nstime.c:144
nstime_set_unset
WS_DLL_PUBLIC void nstime_set_unset(nstime_t *nstime)
Marks the given nstime_t as "unset".
Definition nstime.c:48
nstime_to_iso8601
WS_DLL_PUBLIC size_t nstime_to_iso8601(char *buf, size_t buf_size, const nstime_t *nstime)
Converts an nstime_t to an ISO 8601 formatted string.
Definition nstime.c:664
nstime_delta
WS_DLL_PUBLIC void nstime_delta(nstime_t *delta, const nstime_t *b, const nstime_t *a)
Calculates the time delta between two timestamps.
Definition nstime.c:80
nstime_set_zero
WS_DLL_PUBLIC void nstime_set_zero(nstime_t *nstime)
Sets the given nstime_t to zero.
Definition nstime.c:26
filetime_ns_to_nstime
WS_DLL_PUBLIC bool filetime_ns_to_nstime(nstime_t *nstime, uint64_t nsfiletime)
Converts a nanosecond-based FILETIME to nstime.
Definition nstime.c:310
nstime_is_unset
WS_DLL_PUBLIC bool nstime_is_unset(const nstime_t *nstime)
Checks whether the given nstime_t is marked as "unset".
Definition nstime.c:55
iso8601_fmt_e
iso8601_fmt_e
Selects the ISO 8601 datetime string format used for parsing or formatting timestamps.
Definition nstime.h:309
ISO8601_DATETIME
@ ISO8601_DATETIME
Definition nstime.h:310
ISO8601_DATETIME_AUTO
@ ISO8601_DATETIME_AUTO
Definition nstime.h:312
ISO8601_DATETIME_BASIC
@ ISO8601_DATETIME_BASIC
Definition nstime.h:311
nstime_cmp
WS_DLL_PUBLIC int nstime_cmp(const nstime_t *a, const nstime_t *b)
Compares two time values.
Definition nstime.c:193
nstime_is_zero
WS_DLL_PUBLIC bool nstime_is_zero(const nstime_t *nstime)
Checks whether the given nstime_t is zero.
Definition nstime.c:33
filetime_to_nstime
WS_DLL_PUBLIC bool filetime_to_nstime(nstime_t *nstime, uint64_t filetime)
Converts a Windows FILETIME to nstime.
Definition nstime.c:286
nstime_copy
WS_DLL_PUBLIC void nstime_copy(nstime_t *a, const nstime_t *b)
Copies one nstime_t value to another.
Definition nstime.c:69
nstime_hash
WS_DLL_PUBLIC unsigned nstime_hash(const nstime_t *nstime)
Computes a hash value for a time value.
Definition nstime.c:230
nstime_rounded
WS_DLL_PUBLIC void nstime_rounded(nstime_t *a, const nstime_t *b, ws_tsprec_e prec)
Rounds a time value to the specified precision.
Definition nstime.c:237
nstime_to_unix
WS_DLL_PUBLIC void nstime_to_unix(char *buf, size_t buf_size, const nstime_t *nstime)
Converts an nstime_t to a Unix timestamp string.
Definition nstime.c:715
filetime_1sec_to_nstime
WS_DLL_PUBLIC bool filetime_1sec_to_nstime(nstime_t *nstime, uint64_t filetime)
Converts a second-based FILETIME to nstime.
Definition nstime.c:331
iso8601_to_nstime
WS_DLL_PUBLIC const char * iso8601_to_nstime(nstime_t *nstime, const char *ptr, iso8601_fmt_e format)
Parses an ISO 8601 formatted datetime string into an nstime_t.
Definition nstime.c:370
nstime_is_negative
WS_DLL_PUBLIC bool nstime_is_negative(const nstime_t *nstime)
Checks whether the given nstime_t is negative.
Definition nstime.c:39
unix_epoch_to_nstime
WS_DLL_PUBLIC const char * unix_epoch_to_nstime(nstime_t *nstime, const char *ptr)
Parses a Unix epoch timestamp string into an nstime_t.
Definition nstime.c:603
nstime_t
Definition nstime.h:26