C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-27-2008, 06:19 AM   #1
Registered User
 
Join Date: Mar 2008
Posts: 24
Question about Function: int gettimeofday

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
sweetorangepie is offline   Reply With Quote
Old 03-27-2008, 06:28 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Something like this?

Code:
#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;
}
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 03-27-2008, 06:28 AM   #3
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,338
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 03-27-2008, 01:20 PM   #4
Registered User
 
Join Date: Mar 2008
Posts: 24
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
sweetorangepie is offline   Reply With Quote
Old 03-27-2008, 01:24 PM   #5
Registered User
 
Join Date: Oct 2006
Posts: 298
try this:

Code:
#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.
Elkvis is offline   Reply With Quote
Old 03-27-2008, 01:27 PM   #6
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,325
and what about freeing the time_str string?
or better snprintf directly into the provided buffer to avoid unneeded copy...
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-27-2008, 01:38 PM   #7
Registered User
 
Join Date: Oct 2006
Posts: 298
Quote:
Originally Posted by vart View Post
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.

Quote:
Originally Posted by vart View Post
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.
Elkvis is offline   Reply With Quote
Old 03-27-2008, 04:59 PM   #8
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by sweetorangepie View Post
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
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 04-06-2008, 10:09 PM   #9
Registered User
 
Join Date: Mar 2008
Posts: 16
I wanted to measure time while using threads and I have some source code with which you can experiment here. Hope this helps.

Check my second post there (third post on thread). That's the one you want.
dimis is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling sample DarkGDK Program Phyxashun Game Programming 6 01-27-2009 03:07 AM
getting a headache sreetvert83 C++ Programming 41 09-30-2005 05:20 AM
Binary Search Trees Part III Prelude A Brief History of Cprogramming.com 16 10-02-2004 03:00 PM
How do you search & sort an array? sketchit C Programming 30 11-03-2001 05:26 PM
Hi, could someone help me with arrays? goodn C Programming 20 10-18-2001 09:48 AM


All times are GMT -6. The time now is 09:28 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22