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. Advertisements

  2. michelle

    Jack Klein Guest

    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.
    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.
    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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.
Loading...