11-19-2007
You can use the gettimeofday() and the timeval structure to achieve what you want.
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
int
main(void)
{
char buf[30];
struct timeval tv;
time_t ct;
int ms;
gettimeofday(&tv, NULL);
ct = tv.tv_sec;
strftime(buf, 30, "%m/%d/%y %T.", localtime(&ct));
ms = (tv.tv_usec % 1000000) / 1000;
printf("Timestamp: %s%d\n", buf, ms);
}
10 More Discussions You Might Find Interesting
1. Programming
Hi,
Please see this:
When i make a declaration as:
char *i, j, *k;
and then do
sprintf( k, "print.sh %s", i );
the program works fine.
But when i change the declaration to:
char *i, *k;
and then do
sprintf( k, "print.sh %s", i );
I get a segmentation fault at the 'sprintf'... (16 Replies)
Discussion started by: karthikb23
16 Replies
2. UNIX for Advanced & Expert Users
hello...
thats a great forum btw :)
my problem is that I need a list of the Base Level Filesets (BLF) which are needed by a specific program.
Is there any command/tool which shows me that?
during the installation I can choose "Preview only" so that I can see what BLF´s are missing etc but... (4 Replies)
Discussion started by: cypher82
4 Replies
3. UNIX for Dummies Questions & Answers
Hi,
i want to write a script that executes a program (exec?) .
this program then requires a filename as input.
how do i give it this input in the script so the program will be complete run and close by the script.
e.g.
exec prog.exe
program then asks for filename
"enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies
4. UNIX for Dummies Questions & Answers
Hi guys ,
This is the first time i m running java application inside linux.
i have installed jdk-6u20-linux-i586-rpm.bin jre-6u20-linux-i586-rpm.bin in my linux machine.
and set JAVA_HOME and JRE_HOME variables respectively.
# echo $JAVA_HOME
/usr/java/jdk1.6.0_20/
# echo $JRE_HOME... (6 Replies)
Discussion started by: pinga123
6 Replies
5. Debian
Hi everyone, I have an issue with a project of mine. I have to run a program on a terminal which requires to be logged in as su to have it run it. Given that I'm having problem to use expect to give the password I'd like to change the privilege of that program from SU to normal user ( I have the SU... (13 Replies)
Discussion started by: gaisselick87
13 Replies
6. Homework & Coursework Questions
Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies
7. Programming
Hello all ,
i need some help asap
i have a program that keeps killing the machine
when i did google searches and 2 days later i ran strace
it seems the programm keeps making a system call to gettimeofday
to i guess increment a counter ?
gettimeofday({1347986584, 464904}, NULL) = 0... (6 Replies)
Discussion started by: NetworkLearning
6 Replies
8. Homework & Coursework Questions
Hello,
I would like to build some sample C++ application on Solaris SunOS 5.8 Generic Virtual sun4v sparc.
so I would like to know what are the compilation utilities and runtime utilities I need to get in my machine
and will any one explain me the detaied life cycle of program like
what... (1 Reply)
Discussion started by: Revathi R
1 Replies
9. Programming
Hi ,
seq can be 0...128
int windex = seq / 8;
int bindex = seq % 8;
unsigned char bitvalue = '\x01' << (7-bindex) ;
bpv.bitmapvalue = bitvalue;
This is the part of a program to convert decimal to bitmap value of hexadecimal.
I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies
10. Shell Programming and Scripting
I created a program, so a kid can practice there math on it. It dispenses varies math problems and the kid must input an answer. I also want it to grade the work they have done, but I can't find the best place for it to print out the grade.
I have:
if ( $response =~ m/^/ ) {
$user_wants_to_quit... (1 Reply)
Discussion started by: germany1517
1 Replies
LEARN ABOUT NETBSD
timespec
TIMEVAL(3) BSD Library Functions Manual TIMEVAL(3)
NAME
timeval, timespec, itimerval, itimerspec, bintime -- time structures
SYNOPSIS
#include <sys/time.h>
void
TIMEVAL_TO_TIMESPEC(struct timeval *tv, struct timespec *ts);
void
TIMESPEC_TO_TIMEVAL(struct timeval *tv, struct timespec *ts);
DESCRIPTION
The <sys/time.h> header, included by <time.h>, defines various structures related to time and timers.
1. The following structure is used by gettimeofday(2), among others:
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
The tv_sec member represents the elapsed time, in whole seconds. The tv_usec member captures rest of the elapsed time, represented as
the number of microseconds.
2. The following structure is used by nanosleep(2), among others:
struct timespec {
time_t tv_sec;
long tv_nsec;
};
The tv_sec member is again the elapsed time in whole seconds. The tv_nsec member represents the rest of the elapsed time in nanosec-
onds.
A microsecond is equal to one millionth of a second, 1000 nanoseconds, or 1/1000 milliseconds. To ease the conversions, the macros
TIMEVAL_TO_TIMESPEC() and TIMESPEC_TO_TIMEVAL() can be used to convert between struct timeval and struct timespec.
3. The following structure is used by setitimer(2), among others:
struct itimerval {
struct timeval it_interval;
struct timeval it_value;
};
4. The following structure is used by timer_settime(2), among others:
struct itimerspec {
struct timespec it_interval;
struct timespec it_value;
};
Both struct itimerval and struct itimerspec are used to specify when a timer expires. Generally, it_interval specifies the period
between successive timer expirations. A value zero implies that the alarm will fire only once. If it_value is non-zero, it indicates
the time left to the next timer expiration. A value zero implies that the timer is disabled.
5. The following structure is used by bintime(9), among others:
struct bintime {
time_t sec;
uint64_t frac;
};
The sec member specifies the time in seconds and frac represents a 64-bit fraction of seconds. The struct bintime is meant to be used
in the kernel only. It is further described in timecounter(9).
EXAMPLES
It can be stressed that the traditional UNIX timeval and timespec structures represent elapsed time, measured by the system clock (see
hz(9)). The following sketch implements a function suitable for use in a context where the timespec structure is required for a conditional
timeout:
static void
example(struct timespec *spec, time_t minutes)
{
struct timeval elapsed;
(void)gettimeofday(&elapsed, NULL);
_DIAGASSERT(spec != NULL);
TIMEVAL_TO_TIMESPEC(&elapsed, spec);
/* Add the offset for timeout in minutes. */
spec->tv_sec = spec->tv_sec + minutes * 60;
}
A better alternative would use the more precise clock_gettime(2).
SEE ALSO
timeradd(3), tm(3), bintime_add(9)
BSD
April 12, 2011 BSD