| Wireshark 4.7.0 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
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/* Macros that expand to nstime_t initializers */
57
58/* Initialize to zero */
59#define NSTIME_INIT_ZERO {0, 0}
60
61/* Initialize to unset */
62#define NSTIME_INIT_UNSET {0, INT_MAX}
63
64/* Initialize to a specified number of seconds and nanoseconds */
65#define NSTIME_INIT_SECS_NSECS(secs, nsecs) {(secs) + ((nsecs) / 1000000000), (nsecs) % 1000000000}
66
67/* Initialize to a specified number of seconds and microseconds */
68#define NSTIME_INIT_SECS_USECS(secs, usecs) {(secs) + ((usecs) / 1000000), ((usecs) % 1000000) * 1000}
69
70/* Initialize to a specified number of seconds and milliseconds */
71#define NSTIME_INIT_SECS_MSECS(secs, msecs) {(secs) + ((msecs) / 1000), ((msecs) % 1000) * 1000000}
72
73/* Initialize to a specified number of seconds */
74#define NSTIME_INIT_SECS(secs) {secs, 0}
75
76/* Initialize to the maximum possible value */
77#define NSTIME_INIT_MAX {TIME_T_MAX, INT_MAX}
78
79/* functions */
80
88WS_DLL_PUBLIC void nstime_set_zero(nstime_t *nstime);
89
98WS_DLL_PUBLIC bool nstime_is_zero(const nstime_t *nstime);
99
108WS_DLL_PUBLIC bool nstime_is_negative(const nstime_t *nstime);
109
120WS_DLL_PUBLIC void nstime_set_unset(nstime_t *nstime);
121
130WS_DLL_PUBLIC bool nstime_is_unset(const nstime_t *nstime);
131
141WS_DLL_PUBLIC void nstime_copy(nstime_t *a, const nstime_t *b);
142
157WS_DLL_PUBLIC void nstime_delta(nstime_t *delta, const nstime_t *b, const nstime_t *a);
158
172WS_DLL_PUBLIC void nstime_sum(nstime_t *sum, const nstime_t *a, const nstime_t *b);
173
183#define nstime_add(sum, a) nstime_sum(sum, sum, a)
184
194#define nstime_subtract(sum, a) nstime_delta(sum, sum, a)
195
208WS_DLL_PUBLIC int nstime_cmp(const nstime_t *a, const nstime_t *b);
209
218WS_DLL_PUBLIC unsigned nstime_hash(const nstime_t *nstime);
219
228WS_DLL_PUBLIC double nstime_to_msec(const nstime_t *nstime);
229
238WS_DLL_PUBLIC double nstime_to_sec(const nstime_t *nstime);
239
250WS_DLL_PUBLIC bool filetime_to_nstime(nstime_t *nstime, uint64_t filetime);
251
262WS_DLL_PUBLIC bool filetime_ns_to_nstime(nstime_t *nstime, uint64_t nsfiletime);
263
273WS_DLL_PUBLIC bool filetime_1sec_to_nstime(nstime_t *nstime, uint64_t filetime);
274
275 typedef enum {
276 ISO8601_DATETIME,
277 ISO8601_DATETIME_BASIC,
278 ISO8601_DATETIME_AUTO,
279} iso8601_fmt_e;
280
292WS_DLL_PUBLIC const char * iso8601_to_nstime(nstime_t *nstime, const char *ptr, iso8601_fmt_e format);
293
304WS_DLL_PUBLIC const char * unix_epoch_to_nstime(nstime_t *nstime, const char *ptr);
305
312#define NSTIME_ISO8601_BUFSIZE sizeof("YYYY-MM-DDTHH:MM:SS.123456789Z")
313
324WS_DLL_PUBLIC size_t nstime_to_iso8601(char *buf, size_t buf_size, const nstime_t *nstime);
325
332#define NSTIME_UNIX_BUFSIZE (20+10+1)
333
344WS_DLL_PUBLIC void nstime_to_unix(char *buf, size_t buf_size, const nstime_t *nstime);
345
354 typedef enum {
355 WS_TSPREC_SEC = 0,
356 WS_TSPREC_100_MSEC = 1,
357 WS_TSPREC_10_MSEC = 2,
358 WS_TSPREC_MSEC = 3,
359 WS_TSPREC_100_USEC = 4,
360 WS_TSPREC_10_USEC = 5,
361 WS_TSPREC_USEC = 6,
362 WS_TSPREC_100_NSEC = 7,
363 WS_TSPREC_10_NSEC = 8,
364 WS_TSPREC_NSEC = 9
365} ws_tsprec_e;
366
374#define WS_TSPREC_MAX 9
375
382#define NUM_WS_TSPREC_VALS (WS_TSPREC_MAX + 1)
383
395WS_DLL_PUBLIC void nstime_rounded(nstime_t *a, const nstime_t *b, ws_tsprec_e prec);
396
406#define nstime_round(a, prec) nstime_rounded(a, a, prec)
407
408#ifdef __cplusplus
409}
410#endif /* __cplusplus */
411
412#endif /* __NSTIME_H__ */
ws_tsprec_e
ws_tsprec_e
Timestamp precision levels.
Definition nstime.h:354
WS_TSPREC_USEC
@ WS_TSPREC_USEC
Definition nstime.h:361
WS_TSPREC_100_NSEC
@ WS_TSPREC_100_NSEC
Definition nstime.h:362
WS_TSPREC_100_MSEC
@ WS_TSPREC_100_MSEC
Definition nstime.h:356
WS_TSPREC_100_USEC
@ WS_TSPREC_100_USEC
Definition nstime.h:359
WS_TSPREC_NSEC
@ WS_TSPREC_NSEC
Definition nstime.h:364
WS_TSPREC_MSEC
@ WS_TSPREC_MSEC
Definition nstime.h:358
WS_TSPREC_10_MSEC
@ WS_TSPREC_10_MSEC
Definition nstime.h:357
WS_TSPREC_10_USEC
@ WS_TSPREC_10_USEC
Definition nstime.h:360
WS_TSPREC_SEC
@ WS_TSPREC_SEC
Definition nstime.h:355
WS_TSPREC_10_NSEC
@ WS_TSPREC_10_NSEC
Definition nstime.h:363
nstime_to_msec
WS_DLL_PUBLIC double nstime_to_msec(const nstime_t *nstime)
Converts a time value to milliseconds.
Definition nstime.c:249
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:151
nstime_set_unset
WS_DLL_PUBLIC void nstime_set_unset(nstime_t *nstime)
Marks the given nstime_t as "unset".
Definition nstime.c:53
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:679
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:85
nstime_set_zero
WS_DLL_PUBLIC void nstime_set_zero(nstime_t *nstime)
Sets the given nstime_t to zero.
Definition nstime.c:31
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:337
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:60
iso8601_fmt_e
iso8601_fmt_e
Definition nstime.h:275
ISO8601_DATETIME_AUTO
@ ISO8601_DATETIME_AUTO
Definition nstime.h:278
ISO8601_DATETIME_BASIC
@ ISO8601_DATETIME_BASIC
Definition nstime.h:277
nstime_cmp
WS_DLL_PUBLIC int nstime_cmp(const nstime_t *a, const nstime_t *b)
Compares two time values.
Definition nstime.c:200
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:38
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:313
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:74
nstime_hash
WS_DLL_PUBLIC unsigned nstime_hash(const nstime_t *nstime)
Computes a hash value for a time value.
Definition nstime.c:237
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:264
nstime_to_sec
WS_DLL_PUBLIC double nstime_to_sec(const nstime_t *nstime)
Converts a time value to seconds.
Definition nstime.c:259
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:730
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:358
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:397
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:44
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:618
nstime_t
Definition nstime.h:26