mausezahn: use getopt_long instead of getopt
[netsniff-ng.git] / staging / time.c
blob5ad3658fecffe7cedc07d5c2199dac5811bd4f37
1 /*
2 * Mausezahn - A fast versatile traffic generator
3 * Copyright (C) 2008-2010 Herbert Haas
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License version 2 as published by the
7 * Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 * details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, see http://www.gnu.org/licenses/gpl-2.0.html
20 #include "mz.h"
23 // Check if current system supports the nanosecond timer functions.
24 // Additionally, measure the precision.
25 // This function should be called upon program start.
26 //
27 int check_timer(void)
29 struct timespec res;
30 int r;
32 // Check if the glibc is recent enough:
33 #ifdef _POSIX_C_SOURCE
35 if (_POSIX_C_SOURCE >= 199309L) {
36 r = clock_getres(CLOCK_MONOTONIC, &res);
37 if (r!=0) perror(" mz/check_timer:");
38 if (verbose) {
39 fprintf(stderr, " This system supports a high resolution clock.\n");
40 fprintf(stderr, " The clock resolution is %li nanoseconds.\n",
41 res.tv_nsec);
44 else {
45 fprintf(stderr,
46 " WARNING: Your system does NOT support the newer high resolution clock\n"
47 " Please inform the author: herbert@perihel.at\n");
48 exit(1);
50 #endif
51 return 0;
57 // This is the replacement for gettimeofday() which would result in 'jumps' if
58 // the system clock is adjusted (e. g. via a NTP process) and finally the jitter
59 // measurement would include wrong datapoints.
60 //
61 // Furthermore the function below utilizes the newer hi-res nanosecond timers.
62 inline void getcurtime (struct mz_timestamp *t)
64 struct timespec ct;
65 clock_gettime(CLOCK_MONOTONIC, &ct);
66 t->sec = ct.tv_sec;
67 t->nsec = ct.tv_nsec;
73 //////////////////////////////////////////////////////////////////////////////////////
74 // Purpose: Calculate time deltas of two timestamps stored in struct timeval.
75 //
76 // Subtract the "struct timeval" values X and Y, storing the result in RESULT,
77 // i. e. X-Y=RESULT.
78 //
79 // RETURN VALUES:
80 //
81 // Sign: 1 = negative, 0 = positive
82 // Error: -1 due to a wrong timestamp (i. e. nsec > 999999999L)
83 //
84 inline int timestamp_subtract (struct mz_timestamp *x, struct mz_timestamp *y, struct mz_timestamp *result)
86 int32_t ndiff;
87 int sign=0, carry=0;
89 // Check for wrong timestamps
90 if ((x->nsec>999999999L) || (y->nsec>999999999L)) return -1;
92 if (y->sec > x->sec) sign=1;
93 else if ((y->sec == x->sec) && (y->nsec > x->nsec)) sign=1;
95 ndiff = x->nsec - y->nsec;
96 if ((ndiff>0) && (sign)) carry=1;
97 if ((ndiff<0) && (sign)) ndiff = y->nsec - x->nsec;
98 if ((ndiff<0) && (!sign)) {
99 ndiff = 1000000000L + ndiff;
100 carry=1;
103 if (sign)
104 result->sec = y->sec - x->sec - carry;
105 else
106 result->sec = x->sec - y->sec - carry;
108 result->nsec = ndiff;
109 return sign;
113 // Add two variables of type struct mz_timestamp: x+y=result.
115 inline void timestamp_add (struct mz_timestamp *x, struct mz_timestamp *y, struct mz_timestamp *result)
117 int carry=0;
118 u_int32_t c;
120 c = x->nsec + y->nsec;
121 if (c>999999999L) {
122 carry=1;
123 result->nsec =c-1000000000;
124 } else result->nsec =c;
126 result->sec = x->sec + y->sec + carry;
131 // Returns a human readable timestamp in the string result.
132 // Optionally a prefix can be specified, for example if the
133 // timestamp is part of a filename.
135 // Example:
136 // char myTimeStamp[128];
138 // timestamp_human(myTimeStamp, NULL);
140 // => "20080718_155521"
142 // /* or with prefix */
144 // timestamp_human(myTimeStamp, "MZ_RTP_jitter_");
146 // => "MZ_RTP_jitter_20080718_155521"
148 int timestamp_human(char* result, const char* prefix)
150 time_t curtime;
151 struct tm curtime_broken;
152 char curtime_str[32];
154 time(&curtime);
155 localtime_r (&curtime, &curtime_broken);
157 sprintf(curtime_str, "%4i%02i%02i-%02i%02i%02i",
158 curtime_broken.tm_year+1900,
159 curtime_broken.tm_mon+1,
160 curtime_broken.tm_mday,
161 curtime_broken.tm_hour,
162 curtime_broken.tm_min,
163 curtime_broken.tm_sec);
165 if (prefix==NULL)
167 strncpy(result, curtime_str, 32);
169 else
171 strncpy(result, prefix, 32);
172 strncat(result, curtime_str, 32);
175 return 0;
179 // Creates a human readable timestamp in the string result.
180 // Optionally a prefix can be specified, for example if the
181 // timestamp is part of a filename.
183 // Example:
184 // char myTimeStamp[9];
186 // timestamp_hms (myTimeStamp);
188 // => "15:55:21"
189 int timestamp_hms(char* result)
191 time_t curtime;
192 struct tm curtime_broken;
193 char curtime_str[32];
195 time(&curtime);
196 localtime_r (&curtime, &curtime_broken);
198 sprintf(curtime_str, "%02i:%02i:%02i",
199 curtime_broken.tm_hour,
200 curtime_broken.tm_min,
201 curtime_broken.tm_sec);
203 strncpy(result, curtime_str, 9);
205 return 0;