On Apr 26, 2005, at 11:10 AM, Adam Thornton wrote:
On Apr 26, 2005, at 10:16 AM, James Melin wrote:
Is there a way to have the date function return the time down to the
100th
of a second as a string of [hour][min][sec][hundredths] eg: 17594588 ?
Perl's Time::Hires module certainly does this. Date doesn't, but the c
library function gettimeofday() gives you down to microseconds (in
theory). It'd be easy to write something that grabbed the timeval
structure, and did the conversion of seconds-since-the-epoch into a
sane format (I think you can use ctime() or one of its friends to do
that, because the tv_sec member of the struct timeval that
gettimeofday() returns is a time_t, which is what all the ctime()
functions operate on, and tv_usec / 10000 should be the hundredths).
Like this:
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <errno.h>
int main(int argc, char **argv) {
struct timeval t;
struct timezone tz;
struct tm *tm;
int i;
time_t date;
int sec, min, hour, hundredths;
i = gettimeofday(&t,&tz);
if (i) {
return(errno);
}
date = t.tv_sec;
hundredths = t.tv_usec / 10000; /* Not rounding */
/* Rounding: hundredths = (t.tvusec + 5000)/10000; */
tm = localtime(&date);
sec = tm->tm_sec;
min = tm->tm_min;
hour = tm->tm_hour;
printf("%02d%02d%02d%02d\n",hour,min,sec,hundredths);
return(0);
}
----------------------------------------------------------------------
For LINUX-390 subscribe / signoff / archive access instructions,
send email to [EMAIL PROTECTED] with the message: INFO LINUX-390 or visit
http://www.marist.edu/htbin/wlvindex?LINUX-390