root/trunk/timefuncs.c

Revision 41, 2.4 kB (checked in by jesus, 8 years ago)

autoconf support

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /* ======================================================================
2  * Copyright (c) 2000 Theo Schlossnagle
3  * All rights reserved.
4  * The following code was written by Theo Schlossnagle <jesus@omniti.com>
5  * This code was written to facilitate clustered logging via Spread.
6  * More information on Spread can be found at http://www.spread.org/
7  * Please refer to the LICENSE file before using this software.
8  * ======================================================================
9 */
10
11 #include "sld_config.h"
12 #include "timefuncs.h"
13
14 #define MAXTIMESTRLEN 128
15
16 static char *apmonthnames[12] =
17   {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
18    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
19
20 #ifndef MIN
21 #define MIN(a,b) (((a)<(b))?(a):(b))
22 #endif
23
24 static char *strnchr(char *string, char chr, int len) {
25   char *cp=string;
26   while(len>0) {
27     if(*cp == chr) return cp;
28     cp++; len--;
29   }
30   return NULL;
31 }
32 void force_local_time(char *string, int *length, const int buffsize,
33                       int style, char *format) {
34  char *cp, *cpend, *newcpend;
35  char timebuff[MAXTIMESTRLEN];
36  int timestrlen, newtimestrlen;
37  int timz;
38  struct tm *t;
39  time_t secs;
40  struct timeval now;
41  struct timezone tz;
42
43  cp = strnchr(string, '[', *length);
44  if(!cp) return;
45  cpend = strnchr(cp, ']', *length-(cp-string));
46  if(!cpend) return;
47  cpend++;
48  timestrlen = cpend-cp;
49  gettimeofday(&now, &tz);
50  timz = tz.tz_minuteswest;
51  secs = now.tv_sec;
52  t = localtime(&secs);
53  if(style == NO_REWRITE_TIMES)
54   return;
55  else if(style == REWRITE_TIMES_IN_CLF) {
56   char sign = (timz > 0 ? '-' : '+');
57   timz = (timz < 0)?(-timz):(timz);
58   snprintf(timebuff, sizeof(timebuff), "[%02d/%s/%d:%02d:%02d:%02d %c%.2d%.2d]",
59            t->tm_mday, apmonthnames[t->tm_mon], t->tm_year+1900,
60            t->tm_hour, t->tm_min, t->tm_sec,
61            sign, timz / 60, timz % 60);
62  } else if((style == REWRITE_TIMES_FORMAT) && format) {
63   strftime(timebuff, sizeof(timebuff), format, t);
64  }
65
66  /* No measure and squeeze it in there */
67  newtimestrlen = strlen(timebuff);
68  newcpend = cp+newtimestrlen;
69  
70  if(newcpend != cpend) {
71    /* Ugh different size... this is going to me slower */
72    int chunk = MIN(MIN(string+buffsize-cpend, string+buffsize-newcpend),
73                    string+*length-cpend);
74    if(chunk>0)
75      memmove(newcpend, cpend, chunk);
76    *length += newcpend-cpend;
77    if(*length >= buffsize) {
78      newtimestrlen -= *length-buffsize;
79      *length = buffsize;
80    }
81  }
82  memcpy(cp, timebuff, newtimestrlen);
83  return;
84 }
Note: See TracBrowser for help on using the browser.