-
Notifications
You must be signed in to change notification settings - Fork 1
/
timefuncs.c
executable file
·84 lines (76 loc) · 2.41 KB
/
timefuncs.c
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
/* ======================================================================
* Copyright (c) 2000 Theo Schlossnagle
* All rights reserved.
* The following code was written by Theo Schlossnagle <[email protected]>
* This code was written to facilitate clustered logging via Spread.
* More information on Spread can be found at http://www.spread.org/
* Please refer to the LICENSE file before using this software.
* ======================================================================
*/
#include "sld_config.h"
#include "timefuncs.h"
#define MAXTIMESTRLEN 128
static char *apmonthnames[12] =
{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
#ifndef MIN
#define MIN(a,b) (((a)<(b))?(a):(b))
#endif
static char *strnchr(char *string, char chr, int len) {
char *cp=string;
while(len>0) {
if(*cp == chr) return cp;
cp++; len--;
}
return NULL;
}
void force_local_time(char *string, int *length, const int buffsize,
int style, char *format) {
char *cp, *cpend, *newcpend;
char timebuff[MAXTIMESTRLEN];
int timestrlen, newtimestrlen;
int timz;
struct tm *t;
time_t secs;
struct timeval now;
struct timezone tz;
cp = strnchr(string, '[', *length);
if(!cp) return;
cpend = strnchr(cp, ']', *length-(cp-string));
if(!cpend) return;
cpend++;
timestrlen = cpend-cp;
gettimeofday(&now, &tz);
timz = tz.tz_minuteswest;
secs = now.tv_sec;
t = localtime(&secs);
if(style == NO_REWRITE_TIMES)
return;
else if(style == REWRITE_TIMES_IN_CLF) {
char sign = (timz > 0 ? '-' : '+');
timz = (timz < 0)?(-timz):(timz);
snprintf(timebuff, sizeof(timebuff), "[%02d/%s/%d:%02d:%02d:%02d %c%.2d%.2d]",
t->tm_mday, apmonthnames[t->tm_mon], t->tm_year+1900,
t->tm_hour, t->tm_min, t->tm_sec,
sign, timz / 60, timz % 60);
} else if((style == REWRITE_TIMES_FORMAT) && format) {
strftime(timebuff, sizeof(timebuff), format, t);
}
/* No measure and squeeze it in there */
newtimestrlen = strlen(timebuff);
newcpend = cp+newtimestrlen;
if(newcpend != cpend) {
/* Ugh different size... this is going to me slower */
int chunk = MIN(MIN(string+buffsize-cpend, string+buffsize-newcpend),
string+*length-cpend);
if(chunk>0)
memmove(newcpend, cpend, chunk);
*length += newcpend-cpend;
if(*length >= buffsize) {
newtimestrlen -= *length-buffsize;
*length = buffsize;
}
}
memcpy(cp, timebuff, newtimestrlen);
return;
}