Add support for tab-completion when selecting by rule
[alpine.git] / pith / osdep / debugtime.c
blobbe1e3b33e9a02cf31223f128ab0d0b2b60ea2472
1 /*
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 * ========================================================================
15 #include <stdio.h>
17 #include <system.h>
18 #include "debugtime.h"
20 #ifdef DEBUG
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.
28 char *
29 debug_time(int include_date, int include_subseconds, int signal_in_progress)
31 time_t t;
32 struct tm *tm_now;
33 #if HAVE_GETTIMEOFDAY
34 struct timeval tp;
35 struct timezone tzp;
36 #else
37 struct _timeb timebuffer;
38 #endif
39 static char timestring[23];
40 char subsecond[8];
41 char datestr[7];
43 if(signal_in_progress)
44 return _("Time Unavailable");
46 timestring[0] = '\0';
48 #if HAVE_GETTIMEOFDAY
49 if(gettimeofday(&tp, &tzp) == 0){
50 t = (time_t)tp.tv_sec;
51 if(include_date){
52 tm_now = localtime(&t);
53 snprintf(datestr, sizeof(datestr), " %d/%d", tm_now->tm_mon+1, tm_now->tm_mday);
55 else
56 datestr[0] = '\0';
58 if(include_subseconds)
59 snprintf(subsecond, sizeof(subsecond), ".%06ld", tp.tv_usec);
60 else
61 subsecond[0] = '\0';
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);
68 if(include_date){
69 tm_now = localtime(&t);
70 snprintf(datestr, sizeof(datestr), " %d/%d", tm_now->tm_mon+1, tm_now->tm_mday);
72 else
73 datestr[0] = '\0';
75 if(include_subseconds){
76 _ftime(&timebuffer);
77 snprintf(subsecond, sizeof(subsecond), ".%03ld", timebuffer.millitm);
79 else
80 subsecond[0] = '\0';
82 snprintf(timestring, sizeof(timestring), "%.8s%.7s%.6s", ctime(&t)+11, subsecond, datestr);
83 #endif /* HAVE_GETTIMEOFDAY */
85 return(timestring);
87 #endif /* DEBUG */
91 * Fills in the passed in structure with the current time.
93 * Returns 0 if ok
94 * -1 if can't do it
96 int
97 get_time(TIMEVAL_S *our_time_val)
99 #if HAVE_GETTIMEOFDAY
100 struct timeval tp;
101 struct timezone tzp;
103 if(gettimeofday(&tp, &tzp) == 0){
104 our_time_val->sec = tp.tv_sec;
105 our_time_val->usec = tp.tv_usec;
106 return 0;
108 #else /* !HAVE_GETTIMEOFDAY */
109 #ifdef _WINDOWS
110 struct _timeb timebuffer;
112 _ftime(&timebuffer);
113 our_time_val->sec = (long)timebuffer.time;
114 our_time_val->usec = 1000L * (long)timebuffer.millitm;
115 return 0;
116 #endif
117 #endif
119 return -1;
124 * Returns the difference between the two values, in microseconds.
125 * Value returned is first - second.
127 long
128 time_diff(TIMEVAL_S *first, TIMEVAL_S *second)
130 return(1000000L*(first->sec - second->sec) + (first->usec - second->usec));