[filesystem.c] Add a cast to aviod a warning with VisualStudio 2017.
[metze/wireshark/wip.git] / wsutil / time_util.c
1 /* time_util.c
2  *
3  * Wireshark - Network traffic analyzer
4  * By Gerald Combs <gerald@wireshark.org>
5  * Copyright 1998 Gerald Combs
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21
22
23 #include "config.h"
24
25 #include <glib.h>
26
27 #include "time_util.h"
28
29 #ifndef _WIN32
30 #include <sys/time.h>
31 #include <sys/resource.h>
32 #else
33 #include <windows.h>
34 #endif
35 #include "ws_printf.h" /* ws_g_warning */
36
37 /* converts a broken down date representation, relative to UTC,
38  * to a timestamp; it uses timegm() if it's available.
39  * Copied from Glib source gtimer.c
40  */
41 time_t
42 mktime_utc(struct tm *tm)
43 {
44 #ifndef HAVE_TIMEGM
45         time_t retval;
46
47         static const int days_before[] =
48                 {
49                         0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
50                 };
51
52         int yr;
53
54         if (tm->tm_mon < 0 || tm->tm_mon > 11)
55                 return (time_t) -1;
56
57         retval = (tm->tm_year - 70) * 365;
58
59         /* count number of leap years */
60         yr  = tm->tm_year + 1900;
61         if (tm->tm_mon + 1 < 3 && (yr % 4) == 0 && ((yr % 100) != 0 || (yr % 400) == 0))
62                 yr--;
63         retval += (((yr / 4) - (yr / 100) + (yr / 400)) - 477); /* 477 = ((1970 / 4) - (1970 / 100) + (1970 / 400)) */
64
65         retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
66
67         retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
68
69         return retval;
70 #else
71         return timegm(tm);
72 #endif /* !HAVE_TIMEGM */
73 }
74
75 void get_resource_usage(double *user_time, double *sys_time) {
76 #ifndef _WIN32
77         struct rusage ru;
78
79         getrusage(RUSAGE_SELF, &ru);
80
81         *user_time = ru.ru_utime.tv_sec + (ru.ru_utime.tv_usec / 1000000.0);
82         *sys_time = ru.ru_stime.tv_sec + (ru.ru_stime.tv_usec / 1000000.0);
83 #else /* _WIN32 */
84         HANDLE h_proc = GetCurrentProcess();
85         FILETIME cft, eft, kft, uft;
86         ULARGE_INTEGER uli_time;
87
88         GetProcessTimes(h_proc, &cft, &eft, &kft, &uft);
89
90         uli_time.LowPart = uft.dwLowDateTime;
91         uli_time.HighPart = uft.dwHighDateTime;
92         *user_time = uli_time.QuadPart / 10000000.0;
93         uli_time.LowPart = kft.dwLowDateTime;
94         uli_time.HighPart = kft.dwHighDateTime;
95         *sys_time = uli_time.QuadPart / 1000000000.0;
96 #endif /* _WIN32 */
97 }
98
99 static double last_user_time = 0.0;
100 static double last_sys_time = 0.0;
101
102 void log_resource_usage(gboolean reset_delta, const char *format, ...) {
103         va_list ap;
104         GString *log_str = g_string_new("");
105         double user_time;
106         double sys_time;
107
108         get_resource_usage(&user_time, &sys_time);
109
110         if (reset_delta || last_user_time == 0.0) {
111                 last_user_time = user_time;
112                 last_sys_time = sys_time;
113         }
114
115         g_string_append_printf(log_str, "user %.3f +%.3f sys %.3f +%.3f ",
116                 user_time, user_time - last_user_time,
117                 sys_time, sys_time - last_sys_time);
118
119         va_start(ap, format);
120         g_string_append_vprintf(log_str, format, ap);
121         va_end(ap);
122
123         ws_g_warning("%s", log_str->str);
124         g_string_free(log_str, TRUE);
125
126 }
127
128 /*
129  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
130  *
131  * Local variables:
132  * c-basic-offset: 8
133  * tab-width: 8
134  * indent-tabs-mode: t
135  * End:
136  *
137  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
138  * :indentSize=8:tabSize=8:noTabs=false:
139  */