1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: debugtime.c 770 2007-10-24 00:23:09Z hubert@u.washington.edu $";
#endif
/*
* ========================================================================
* Copyright 2013-2021 Eduardo Chappa
* Copyright 2006 University of Washington
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* ========================================================================
*/
#include <stdio.h>
#include <system.h>
#include "debugtime.h"
#ifdef DEBUG
/*
* Returns a pointer to static string for a timestamp.
*
* If timestamp is set .subseconds are added if available.
* If include_date is set the date is appended.
*/
char *
debug_time(int include_date, int include_subseconds, int signal_in_progress)
{
time_t t;
struct tm *tm_now;
#if HAVE_GETTIMEOFDAY
struct timeval tp;
struct timezone tzp;
#else
struct _timeb timebuffer;
#endif
static char timestring[23];
char subsecond[8];
char datestr[7];
if(signal_in_progress)
return _("Time Unavailable");
timestring[0] = '\0';
#if HAVE_GETTIMEOFDAY
if(gettimeofday(&tp, &tzp) == 0){
t = (time_t)tp.tv_sec;
if(include_date){
tm_now = localtime(&t);
snprintf(datestr, sizeof(datestr), " %d/%d", tm_now->tm_mon+1, tm_now->tm_mday);
}
else
datestr[0] = '\0';
if(include_subseconds)
snprintf(subsecond, sizeof(subsecond), ".%06ld", tp.tv_usec);
else
subsecond[0] = '\0';
snprintf(timestring, sizeof(timestring), "%.8s%.7s%.6s", ctime(&t)+11, subsecond, datestr);
}
#else /* !HAVE_GETTIMEOFDAY */
/* Should be _WINDOWS */
t = time((time_t *)0);
if(include_date){
tm_now = localtime(&t);
snprintf(datestr, sizeof(datestr), " %d/%d", tm_now->tm_mon+1, tm_now->tm_mday);
}
else
datestr[0] = '\0';
if(include_subseconds){
_ftime(&timebuffer);
snprintf(subsecond, sizeof(subsecond), ".%03ld", timebuffer.millitm);
}
else
subsecond[0] = '\0';
snprintf(timestring, sizeof(timestring), "%.8s%.7s%.6s", ctime(&t)+11, subsecond, datestr);
#endif /* HAVE_GETTIMEOFDAY */
return(timestring);
}
#endif /* DEBUG */
/*
* Fills in the passed in structure with the current time.
*
* Returns 0 if ok
* -1 if can't do it
*/
int
get_time(TIMEVAL_S *our_time_val)
{
#if HAVE_GETTIMEOFDAY
struct timeval tp;
struct timezone tzp;
if(gettimeofday(&tp, &tzp) == 0){
our_time_val->sec = tp.tv_sec;
our_time_val->usec = tp.tv_usec;
return 0;
}
#else /* !HAVE_GETTIMEOFDAY */
#ifdef _WINDOWS
struct _timeb timebuffer;
_ftime(&timebuffer);
our_time_val->sec = (long)timebuffer.time;
our_time_val->usec = 1000L * (long)timebuffer.millitm;
return 0;
#endif
#endif
return -1;
}
/*
* Returns the difference between the two values, in microseconds.
* Value returned is first - second.
*/
long
time_diff(TIMEVAL_S *first, TIMEVAL_S *second)
{
return(1000000L*(first->sec - second->sec) + (first->usec - second->usec));
}
|