Question about Function: int gettimeofday [Archive] - C Board

PDA

View Full Version : Question about Function: int gettimeofday


sweetorangepie
03-27-2008, 06:19 AM
I'm making a log file for my program and I need to inset a time and date stamp. I've had a look through the GNU C Libary and found the gettimeofday function but I'm having a little trouble understanding how to use it. Would someone mind explaining how you would use it or give a code example. It would be much appriciated.

Many Thanks

matsp
03-27-2008, 06:28 AM
Something like this?


#include <sys/time.h>
#include <stdio.h>

int main()
{
struct timeval t;

if (gettimeofday(&t, NULL) != 0)
perror("gettimeofday");
printf("Number of seconds since Jan 1 1970: %ld\n", (long)t.tv_sec);
return 0;
}

laserlight
03-27-2008, 06:28 AM
gettimeofday is not a standard C function, but something *nix based, so I am moving this thread to the closest forum you can get to *nix here.

sweetorangepie
03-27-2008, 01:20 PM
matsp i've just tried what you suggested and it compiles but nothing happens? I'm not sure if what you suggested will do what I need, what I need is something that will get the current system date and time.

is gettimeofday the right function to use or is there something else/better?

Thanks for the help, much appreciated

Elkvis
03-27-2008, 01:24 PM
try this:


#include <glib.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int GetTimeAsString(char* str, size_t str_len)
{
time_t t;
char* time_str;
tm *my_tm;

t = time(NULL);
my_tm = localtime(&t);
time_str = g_strdup_printf("%d/%02d/%02d %02d:%02d:%02d", my_tm->tm_year + 1900,
my_tm->tm_mon + 1,
my_tm->tm_mday,
my_tm->tm_hour,
my_tm->tm_min,
my_tm->tm_sec);
if (strlen(time_str) > (len - 1)) return -1;
else
{
memset(str, 0, strlen(time_str) + 1);
strcpy(str, time_str);
return 0;
}
}


haven't tried to compile it or test it or anything, but I use a function very similar to this in some of my code (this is actually the version with all my custom stuff stripped out), so it should get you to where you want to go.

vart
03-27-2008, 01:27 PM
and what about freeing the time_str string?
or better snprintf directly into the provided buffer to avoid unneeded copy...

Elkvis
03-27-2008, 01:38 PM
and what about freeing the time_str string?

certainly something that would be necessary in production code, but for the purposes of illustration, not a deal breaker.

or better snprintf directly into the provided buffer to avoid unneeded copy...

I prefer to err on the side of caution in the case of filling a user-provided buffer with data. if there isn't enough space in which to put the string, it will return an error (-1). and the program can try again with a bigger buffer. obviously it's up to the person implementing this in his/her code to decide which way is better for him/her. my goal was to simply offer one possible solution to the problem.

matsp
03-27-2008, 04:59 PM
matsp i've just tried what you suggested and it compiles but nothing happens? I'm not sure if what you suggested will do what I need, what I need is something that will get the current system date and time.

is gettimeofday the right function to use or is there something else/better?

Thanks for the help, much appreciated

Right, I wrote a reply to this earlier, but it got lost and I had to restart my wireless router to get back to the 'net, and had some dinner in between.

I'm not sure what you expect to happen, and what you see. You should get a printout from my code - it may not "do" much, but you should get the text string and a large-ish number after it.

What is the right function for your code: I can't tell you that. gettimeofday will give you the current time (and the timezone if you supply the second parameter).

But there are other time functions, such as localtime() and there are functions to format the time into text, which I'm sure you'll need. (but you should be able to use for example strftime with the time_t tv_sec value that comes out of gettimeofday() too)

Have a look at:
http://www.hmug.org/man/3/time.php
http://www.hmug.org/man/3/ctime.php
http://www.hmug.org/man/3/strftime.php

These functions have the added advantage that they will work on most systems, whether they are Unix/Linux ones, or for example Windows.

On the other hand, gettimeofday() gives you a more precise time with it's microsecond value tv_usecs. If you need very precise time-stamps in your log, then that's the key.

--
Mats

dimis
04-06-2008, 10:09 PM
I wanted to measure time while using threads and I have some source code with which you can experiment here (http://cboard.cprogramming.com/showthread.php?t=101085). Hope this helps.

Check my second post there (third post on thread). That's the one you want.