Re: Time Conversion

From: Tim Burgess <sybernetix@dont-contact.us>
Date: Thu, 24 Jun 1999 19:47:43 +1000

Dear Jeff,

> Does any one have a quick way(preferably perl) to convert the unix time
> stamps from the squid access.log to regular(mere mortal) time???

Unless you need the extra info required in the non-httpd emulated logs, use
the options httpd_emulate or similar in the squid conf. This writes the
logs in 'mortal' time. Alternatively, if you are in a situation like mine
(i need this a while back), you can use the following c code to do it.

It may be dodgy but it does the job. The reason for the painful file access
routine is that we use it to 'tail -f' our server logs, and this enables us
to pipe it through my program as the logs are generated.

Usage is: "cat access.log | emulate stdout " for normal operation

or "cat access.log | emulate <filename>" which will write the output to a
file as well as standard output. For some reason I remember I had trouble
piping from this program to a file. oh well its just a quickfix we whipped
up in a hurry.

Cheers,

Tim

PS It only works on access.log, but you could try doing a similar thing for
store.log if you needed to. Everything you need is here.

***********C Code starts here*************88

// date.c is a test for a function which will return a meaningful date
// given a unix date as a float

#include <stdio.h>
#include <time.h>
#include <string.h>

int main(int argc, char *argv[])
{
 FILE *outptr;
 long datenum;
 time_t datevalue;
 char datetext[50];
 char clientip[15];
 char hitmiss[30];
 int httpcode;
 long filesize;
 char method[10];
 char uri[500];
 char hierarchy[15];
 char mimetype[40];

 if (argc != 2)
  fprintf(stderr,"Usage: emulate {<output filename> | stdout}\n");
 else if (strcmp(argv[1],"stdout") == 0)
 {
  while(!feof(stdin))
  {
   datenum = 0;
   scanf("%d.%*d %*d %[0123456789.] %[ABCDEFGHIJKLMNOPQRSTUVWXYZ_]%*c%d %ld
%s %s %*s %[ABCDEFGHIJKLMNOPQRSTUVWXYZ_]/%*s
%s",&datenum,&clientip,&hitmiss,&httpcode,&filesize,&method,&uri,&hierarchy,
&mimetype);

   if (datenum != 0)

    datevalue = (time_t) datenum;
    strftime(datetext,50,"%d/%m/%Y:%H:%M:%S %Z\0", localtime(&datevalue));
    printf("%s - - [%s] \"%s %s\" %d %ld %s:%s\n",
clientip,datetext,method,uri,httpcode,filesize,hitmiss,hierarchy);
   }
  }
 }
 else
 {
  while(!feof(stdin))
  {
   datenum = 0;
   scanf("%d.%*d %*d %[0123456789.] %[ABCDEFGHIJKLMNOPQRSTUVWXYZ_]%*c%d %ld
%s %s %*s %[ABCDEFGHIJKLMNOPQRSTUVWXYZ_]/%*s
%s",&datenum,&clientip,&hitmiss,&httpcode,&filesize,&method,&uri,&hierarchy,
&mimetype);

   if (datenum != 0)

    datevalue = (time_t) datenum;
    strftime(datetext,50,"%d/%m/%Y:%H:%M:%S %Z\0", localtime(&datevalue));
    if ((outptr = fopen(argv[1],"a")) != NULL)
    {
     fprintf(outptr,"%s - - [%s] \"%s %s\" %d %ld %s:%s\n",
clientip,datetext,method,uri,httpcode,filesize,hitmiss,hierarchy);
     fclose(outptr);
    }
    printf("%s - - [%s] \"%s %s\" %d %ld %s:%s\n",
clientip,datetext,method,uri,httpcode,filesize,hitmiss,hierarchy);
   }
  }
 }
 return 0;
};
Received on Thu Jun 24 1999 - 03:49:36 MDT

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 16:46:58 MST