Question about Function: int gettimeofday

This is a discussion on Question about Function: int gettimeofday within the Linux Programming forums, part of the Platform Specific Boards category; I'm making a log file for my program and I need to inset a time and date stamp. I've had ...

  1. #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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    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
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #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

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351
    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.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    and what about freeing the time_str string?
    or better snprintf directly into the provided buffer to avoid unneeded copy...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351
    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.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  9. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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