2 * ========================================================================
3 * Copyright 2013-2022 Eduardo Chappa
4 * Copyright 2006 University of Washington
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
18 #include "debugtime.h"
23 * Returns a pointer to static string for a timestamp.
25 * If timestamp is set .subseconds are added if available.
26 * If include_date is set the date is appended.
29 debug_time(int include_date
, int include_subseconds
, int signal_in_progress
)
37 struct _timeb timebuffer
;
39 static char timestring
[23];
43 if(signal_in_progress
)
44 return _("Time Unavailable");
49 if(gettimeofday(&tp
, &tzp
) == 0){
50 t
= (time_t)tp
.tv_sec
;
52 tm_now
= localtime(&t
);
53 snprintf(datestr
, sizeof(datestr
), " %d/%d", tm_now
->tm_mon
+1, tm_now
->tm_mday
);
58 if(include_subseconds
)
59 snprintf(subsecond
, sizeof(subsecond
), ".%06ld", tp
.tv_usec
);
63 snprintf(timestring
, sizeof(timestring
), "%.8s%.7s%.6s", ctime(&t
)+11, subsecond
, datestr
);
65 #else /* !HAVE_GETTIMEOFDAY */
66 /* Should be _WINDOWS */
67 t
= time((time_t *)0);
69 tm_now
= localtime(&t
);
70 snprintf(datestr
, sizeof(datestr
), " %d/%d", tm_now
->tm_mon
+1, tm_now
->tm_mday
);
75 if(include_subseconds
){
77 snprintf(subsecond
, sizeof(subsecond
), ".%03ld", timebuffer
.millitm
);
82 snprintf(timestring
, sizeof(timestring
), "%.8s%.7s%.6s", ctime(&t
)+11, subsecond
, datestr
);
83 #endif /* HAVE_GETTIMEOFDAY */
91 * Fills in the passed in structure with the current time.
97 get_time(TIMEVAL_S
*our_time_val
)
103 if(gettimeofday(&tp
, &tzp
) == 0){
104 our_time_val
->sec
= tp
.tv_sec
;
105 our_time_val
->usec
= tp
.tv_usec
;
108 #else /* !HAVE_GETTIMEOFDAY */
110 struct _timeb timebuffer
;
113 our_time_val
->sec
= (long)timebuffer
.time
;
114 our_time_val
->usec
= 1000L * (long)timebuffer
.millitm
;
124 * Returns the difference between the two values, in microseconds.
125 * Value returned is first - second.
128 time_diff(TIMEVAL_S
*first
, TIMEVAL_S
*second
)
130 return(1000000L*(first
->sec
- second
->sec
) + (first
->usec
- second
->usec
));