questions about time structure in C

Discussion in 'C Programming' started by michelle, Jul 1, 2003.

  1. michelle

    michelle Guest

    Hi, for the program I am coding now, I need to write the time as a
    String to a file, and later need to compare that recorded time with
    the current time. I have problem changing time from the string to the
    unsigned int (to compare with t->tv_sec). So what I did first is
    that:

    struct timeval* t = (struct timeval*) malloc (sizeof(struct timeval));
    gettimeofday(t, NULL);
    t->tv-sec +=sec; (Use sec(any seconds) to calculate the expired time)
    fprintf(file, "expire at %s\n", ctime(&(t->tv_sec))). (so print to a
    file about when it expries)

    And then later I need to convert this string back to unsigned int to
    compare with the current time. Does anyone know how do I convert it
    back if there is a way to do it? Thanks!

    Michelle
     
    michelle, Jul 1, 2003
    #1
    1. Advertising

  2. michelle

    Jack Klein Guest

    On 30 Jun 2003 16:32:40 -0700, (michelle)
    wrote in comp.lang.c:

    > Hi, for the program I am coding now, I need to write the time as a
    > String to a file, and later need to compare that recorded time with
    > the current time. I have problem changing time from the string to the
    > unsigned int (to compare with t->tv_sec). So what I did first is
    > that:
    >
    > struct timeval* t = (struct timeval*) malloc (sizeof(struct timeval));
    > gettimeofday(t, NULL);


    In addition to what Clint correctly said, because neither "struct
    timeval" nor "gettimeofday()" are part of C, don't ever cast the
    return value of malloc. As long as a definition of struct timeval is
    in scope and you are indeed using C, not C++, do this:

    struct timeval *t = malloc(sizeof *t);

    If your compiler complains about the assignment, include <stdlib.h>.
    If it still complains, you are compiling a C++ program and you are in
    the wrong newsgroup.

    > t->tv-sec +=sec; (Use sec(any seconds) to calculate the expired time)
    > fprintf(file, "expire at %s\n", ctime(&(t->tv_sec))). (so print to a
    > file about when it expries)


    The ctime() function accepts a pointer to a time_t type, so if the
    tv_sec member of a struct timeval is not a time_t, you have undefined
    behavior.

    > And then later I need to convert this string back to unsigned int to
    > compare with the current time. Does anyone know how do I convert it
    > back if there is a way to do it? Thanks!
    >
    > Michelle


    C does not require or guarantee that a time_t can be converted to or
    from an unsigned int without loss. Even POSIX does not require that a
    time_t be an integer type, it could be a double.

    Your program uses non-standard functions and makes non-standard
    assumptions. You really need to take it to a platform specific group.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
     
    Jack Klein, Jul 1, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. flamesrock
    Replies:
    8
    Views:
    551
    Hendrik van Rooyen
    Nov 24, 2006
  2. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    792
    Martin Ambuhl
    Oct 26, 2004
  3. Leo Nunez
    Replies:
    3
    Views:
    1,298
    Neil Kurzman
    Feb 9, 2005
  4. Replies:
    2
    Views:
    642
  5. Andreas Leitgeb
    Replies:
    1
    Views:
    517
    Andreas Leitgeb
    Apr 8, 2008
Loading...

Share This Page