home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
kermit.columbia.edu
/
kermit.columbia.edu.tar
/
kermit.columbia.edu
/
sredird
/
telnetcpcd-1.09.tar.gz
/
telnetcpcd-1.09.tar
/
timestamp.c
< prev
next >
Wrap
C/C++ Source or Header
|
2003-08-12
|
2KB
|
76 lines
/*
timestamp.c
Copyright (c) 2002,2003 Thomas J Pinkl <tom@pinkl.com>
Return a timestamp string for the current date and time.
Version 1.00 04/02/1998
*/
#include "telnetcpcd.h"
#define STRSIZE 32
char *timestamp(void)
{
static char string[STRSIZE];
static time_t last = 0L;
time_t seconds;
struct tm *timeptr;
seconds = time((time_t *) NULL);
if (seconds == last) /* time hasn't changed since last */
return(string);
last = seconds; /* save for next time */
/*
our string has the format: MMM DD HH:MM:SS
this format is consistent with that of syslog
*/
timeptr = localtime(&seconds);
strftime(string,STRSIZE,"%b %d %T",timeptr);
return(string);
}
/*
this version includes milliseconds
*/
char *timestamp_ms(void)
{
static char string[STRSIZE];
static struct timeval last = {0L, 0L};
struct timeval now;
struct tm *timeptr;
char *p;
/*
gettimeofday() returns seconds and microseconds since the epoch.
we convert microseconds (1,000,000) to milliseconds (1,000) by
dividing by 1000 because we're not interested in the extra
precision. and doing so allows us to cache the returned string.
*/
gettimeofday(&now,NULL);
if ((now.tv_sec == last.tv_sec) && ((now.tv_usec / 1000) == (last.tv_usec / 1000)))
return(string); /* time hasn't changed since last */
last.tv_sec = now.tv_sec; /* save for next time */
last.tv_usec = now.tv_usec;
/*
our string has the format: MMM DD HH:MM:SS
this format is consistent with that of syslog
*/
timeptr = localtime(&(now.tv_sec));
strftime(string,STRSIZE,"%b %d %T",timeptr);
/* now append milliseconds to the string */
p = string + strlen(string); /* points to the null */
sprintf(p,".%03d",(int) (now.tv_usec / 1000));
return(string);
}