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
18extern "C" {
19#endif /* __cplusplus */
20
26typedef 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
253static 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
266static 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
314
326WS_DLL_PUBLIC const char * iso8601_to_nstime(nstime_t *nstime, const char *ptr, iso8601_fmt_e format);
327
338WS_DLL_PUBLIC const char * unix_epoch_to_nstime(nstime_t *nstime, const char *ptr);
339
346#define NSTIME_ISO8601_BUFSIZE sizeof("YYYY-MM-DDTHH:MM:SS.123456789Z")
347
358WS_DLL_PUBLIC size_t nstime_to_iso8601(char *buf, size_t buf_size, const nstime_t *nstime);
359
366#define NSTIME_UNIX_BUFSIZE (20+10+1)
367
378WS_DLL_PUBLIC void nstime_to_unix(char *buf, size_t buf_size, const nstime_t *nstime);
379
400
408#define WS_TSPREC_MAX 9
409
416#define NUM_WS_TSPREC_VALS (WS_TSPREC_MAX + 1)
417
429WS_DLL_PUBLIC void nstime_rounded(nstime_t *a, const nstime_t *b, ws_tsprec_e prec);
430
440#define nstime_round(a, prec) nstime_rounded(a, a, prec)
441
442#ifdef __cplusplus
443}
444#endif /* __cplusplus */
445
446#endif /* __NSTIME_H__ */
ws_tsprec_e
Timestamp precision levels.
Definition nstime.h:388
@ WS_TSPREC_USEC
Definition nstime.h:395
@ WS_TSPREC_100_NSEC
Definition nstime.h:396
@ WS_TSPREC_100_MSEC
Definition nstime.h:390
@ WS_TSPREC_100_USEC
Definition nstime.h:393
@ WS_TSPREC_NSEC
Definition nstime.h:398
@ WS_TSPREC_MSEC
Definition nstime.h:392
@ WS_TSPREC_10_MSEC
Definition nstime.h:391
@ WS_TSPREC_10_USEC
Definition nstime.h:394
@ WS_TSPREC_SEC
Definition nstime.h:389
@ WS_TSPREC_10_NSEC
Definition nstime.h:397
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:146
WS_DLL_PUBLIC void nstime_set_unset(nstime_t *nstime)
Marks the given nstime_t as "unset".
Definition nstime.c:48
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:654
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
WS_DLL_PUBLIC void nstime_set_zero(nstime_t *nstime)
Sets the given nstime_t to zero.
Definition nstime.c:26
WS_DLL_PUBLIC bool filetime_ns_to_nstime(nstime_t *nstime, uint64_t nsfiletime)
Converts a nanosecond-based FILETIME to nstime.
Definition nstime.c:312
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
Selects the ISO 8601 datetime string format used for parsing or formatting timestamps.
Definition nstime.h:309
@ ISO8601_DATETIME
Definition nstime.h:310
@ ISO8601_DATETIME_AUTO
Definition nstime.h:312
@ ISO8601_DATETIME_BASIC
Definition nstime.h:311
WS_DLL_PUBLIC int nstime_cmp(const nstime_t *a, const nstime_t *b)
Compares two time values.
Definition nstime.c:195
WS_DLL_PUBLIC bool nstime_is_zero(const nstime_t *nstime)
Checks whether the given nstime_t is zero.
Definition nstime.c:33
WS_DLL_PUBLIC bool filetime_to_nstime(nstime_t *nstime, uint64_t filetime)
Converts a Windows FILETIME to nstime.
Definition nstime.c:288
WS_DLL_PUBLIC void nstime_copy(nstime_t *a, const nstime_t *b)
Copies one nstime_t value to another.
Definition nstime.c:69
WS_DLL_PUBLIC unsigned nstime_hash(const nstime_t *nstime)
Computes a hash value for a time value.
Definition nstime.c:232
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:239
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:705
WS_DLL_PUBLIC bool filetime_1sec_to_nstime(nstime_t *nstime, uint64_t filetime)
Converts a second-based FILETIME to nstime.
Definition nstime.c:333
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:372
WS_DLL_PUBLIC bool nstime_is_negative(const nstime_t *nstime)
Checks whether the given nstime_t is negative.
Definition nstime.c:39
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:593
Definition nstime.h:26