Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I have this simple piece of code in main to do some things every 2 seconds: Code: time_t t_send, ttemp; t_send = 0; while(1){ ttemp = time(NULL); if(ttemp - ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2010
    Location
    Madrid
    Posts
    32

    time(NULL) returning a wrong value?


    Hi, I have this simple piece of code in main to do some things every 2 seconds:

    Code:
    time_t t_send, ttemp;
    t_send = 0;
    
    while(1){
        ttemp = time(NULL);
        if(ttemp - t_send > 1){
           t_send = ttemp;
           // ... more code, mainly send data through a socket
        }
        // ... more code
        usleep(50000);
    }
    It works as it is expected to work until I realize that it stopped sending data. Then I attach gdb to the process and see this:

    Code:
    (gdb) p ttemp
    $1 = 1381825757
    (gdb) p t_send
    $2 = 1381829765
    (gdb) p ttemp - t_send
    $3 = -4008
    How can t_send be greater than ttemp?? Does it mean that time(NULL) returned a wrong value before? Am I missing something? is ttemp or t_send corrupted?

    does anyone figure out what's gong on?

  2. #2
    Just Joined!
    Join Date
    Jun 2011
    Location
    Belgium
    Posts
    84
    Could it be that another variable was badly initialized and thus overwrites 't_send'?

    if not:
    At what point you perform this gdb prints? The only possible point is before t_send is initialized to zero. But I suppose you've already checked this.
    Try
    Code:
    time_t t_send = 0, ttemp;
    ...

  3. #3
    Just Joined!
    Join Date
    May 2010
    Location
    Madrid
    Posts
    32
    Thanks for the answer kdg1955.

    Quote Originally Posted by kdg1955 View Post
    The only possible point is before t_send is initialized to zero.
    Why? I perform the gdb prints in the line
    Code:
     if(ttemp - t_send > 1){
    to check why it does not enter in the if stament.

    The first time state_last is zero so it is initialized correctly.. : /

  4. #4
    Just Joined!
    Join Date
    Jun 2011
    Location
    Belgium
    Posts
    84
    I can hardly imagine that 'time(NULL)' gives a wrong return value. Therefore, my first thought was a overwritten 't_send' somewhere in your code.
    Is it already wrong with 't_send' in your first while loop? I do not think so.

    Another hint: check that after 'usleep(50000)' the variable 't_send' is always unchanged?

  5. #5
    Just Joined!
    Join Date
    Jun 2011
    Location
    Belgium
    Posts
    84
    another possibility: you have a sheduled program that adjust your PC clock with a ntp server.

  6. #6
    Just Joined!
    Join Date
    May 2010
    Location
    Madrid
    Posts
    32
    Quote Originally Posted by kdg1955 View Post
    another possibility: you have a sheduled program that adjust your PC clock with a ntp server.
    yes, I have a cronjob that does that.. but how can it affect to time(NULL)?

  7. #7
    Just Joined!
    Join Date
    Jun 2011
    Location
    Belgium
    Posts
    84
    Quote Originally Posted by occam25 View Post
    yes, I have a cronjob that does that.. but how can it affect to time(NULL)?
    As far as I know:
    time(NULL) returns the cpu time of your computer and your cronjob adjusts the cpu time of your computer. ie: when your cronjob adjust the CPU time, your call to 'time(NULL)' may return a smaller value.

    I'm not sure this is your problem, it's just a train of thought.

    FYI: time(NULL) may also return -1, but this is not applicable to your problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •