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