Thread: time problem

  1. #1
    ---
    Join Date
    May 2004
    Posts
    1,379

    time problem

    I have this function...
    Code:
    tm* get_time_stamp(void){
      time_t timer = time(NULL);
      return localtime(&timer);
    }
    But evey time it is called throughout the program I get the same time. I have heard of this happening before but I don't know how to fix it.

    Second question:
    how can I use printf to print trailing spaces with a string up to a certain number. I want print 20 character no matter how long the string is. I know how to print a maximum of character but how to print a minimum?
    Last edited by sand_man; 09-12-2005 at 10:25 PM.

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    The problem is that localtime returns a pointer to static data. This means that if you call it a second time, the contents will just be overwritten, and the first pointer will point to the new value, just as the second pointer does.

    What you need to do is make a copy of the struct tm on each call. I would do something like this:
    Code:
    #include <stdio.h>
    #include <time.h>
    
    struct tm *get_time_stamp(struct tm *t){
        time_t timer = time(NULL);
        struct tm *l;
        l = localtime(&timer);
        if (!l)
            return NULL;
        *t = *l;
        return t;
    }
    
    int main(void)
    {
        struct tm start_time;
        struct tm end_time;
        long i;
    
        get_time_stamp(&start_time);
    
        /* do something for a while */
        for (i = 0; i < 1000000000L; i++)
            ;
    
        get_time_stamp(&end_time);
    
    
        printf("Start: %02d:%02d:%02d\n", start_time.tm_hour, start_time.tm_min, start_time.tm_sec);
        printf("  End: %02d:%02d:%02d\n", end_time.tm_hour, end_time.tm_min, end_time.tm_sec);
        return 0;
    }
    My output:
    Code:
    Start: 12:34:28
      End: 12:34:30
    You can check the return of get_time_stamp for NULL in case of error.

    And to your second question:

    printf("%20s", the_string); or printf("%-20s", the_string);
    Last edited by cwr; 09-12-2005 at 10:35 PM.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    Thanks, I've got it half working.
    Last edited by sand_man; 09-12-2005 at 10:41 PM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    also, your program can execute the same lines quite a few times during ther same second, so if time() is called twice during the same second it will return the same time. you have to insert a delay(), sleep() or execute some other code that will consume some noticeable number of seconds between calls to time() function. cwr illustrated one way of doing that, but you wouldn't want to use that technique in an actual program because the time will vary from one computer to another. you should use the system's sleep() in *nix or Sleep() in Windows for consistant delays on different computers.

    localtime() returns a statically allocated pointer to tm structure. That means the function will NEVER return a NULL pointer, and I see no point in making that check. If it does return NULL, your program has really hosed things up!
    Last edited by Ancient Dragon; 09-12-2005 at 10:54 PM.

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Ancient Dragon
    also, your program can execute the same lines quite a few times during ther same second, so if time() is called twice during the same second it will return the same time. you have to insert a delay(), sleep() or execute some other code that will consume some noticeable number of seconds between calls to time() function. cwr illustrated one way of doing that, but you wouldn't want to use that technique in an actual program because the time will vary from one computer to another. you should use the system's sleep() in *nix or Sleep() in Windows for consistant delays on different computers.
    I didn't use sleep() because I was sticking to standard C, and a for loop was the easiest way I could think of to do a >1 second delay portably. If you show me any current processor that can do that loop in under 1 second, I'll be very surprised. I wasn't after a consistent delay, just something in place to make time pass for a while.

    localtime() returns a statically allocated pointer to tm structure. That means the function will NEVER return a NULL pointer, and I see no point in making that check. If it does return NULL, your program has really hosed things up!
    From the C standard:

    "The localtime function returns a pointer to the broken-down time, or a null pointer if the specified time cannot be converted to localtime."

    That's why I made the check.

    You might say that because the specified time came from time(), the time will never be unable to be converted, but, also from the C standard:

    "The time function returns the implementation's best approximation to the current calendar time. The value (time_t)-1 is returned if the calendar time is not available."

    Never say "NEVER". :P

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by Ancient Dragon
    also, your program can execute the same lines quite a few times during ther same second, so if time() is called twice during the same second it will return the same time. you have to insert a delay(), sleep() or execute some other code that will consume some noticeable number of seconds between calls to time() function. cwr illustrated one way of doing that, but you wouldn't want to use that technique in an actual program because the time will vary from one computer to another. you should use the system's sleep() in *nix or Sleep() in Windows for consistant delays on different computers.
    That is not the case because the program waits for user input before calling the function

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    cwr: I apologize if my post sounded a little too critical -- we often post code that is over-simplified to illustrate a point. If localtime() ever returns NULL that would mean the system's clock has probably stopped, which means the whole computer is about to crash and die. And programs shouldn't be calling localtime() on systems that do not support it, which some embedded systems may not.


    sand_man: I didn't see any user input in cwr's program.

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    Ancient Dragon: I was talking about mine.

    Now I just don't know what is wrong.
    I am calling the function like this
    Code:
    get_time_stamp(&rec[*record_no]->time);
    Where *record_no is incremented every time a new record is input.
    But when I go to print the time to the screen the first element will print but the second element seg faults.
    Code:
    printf("%s",asctime(&records[0]->time));
    printf("%s",asctime(&records[1]->time));
    Here is my struct
    Code:
    typedef struct{
      long unsigned product_code;
      char description[18];
      float wholesale_price;
      float markup;
      float discount;
      int stock_lvl;
      struct tm time;
    }RECORD;
    
    RECORD *records[100];
    Last edited by sand_man; 09-13-2005 at 06:13 AM.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    The array "records" is an uninitialized array of pointers to structures of type RECORD. You have to allocate the space before using it.
    Code:
    records[o] = malloc(sizeof(RECORD);
    get_time_stamp(&records[*record_no]->time);
    printf("%s",asctime(&records[0]->time));
    (*record_no)++; // incremnet record number
    records[1] = malloc(sizeof(RECORD);
    get_time_stamp(&records[*record_no]->time);
    (*record_no)++; // incremnet record number
    printf("%s",asctime(&records[1]->time));
    Last edited by Ancient Dragon; 09-13-2005 at 07:10 AM.

  10. #10
    ---
    Join Date
    May 2004
    Posts
    1,379
    No they are allocated I just forgot to show that part
    But I fixed it now thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer problem... i think
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 11-30-2005, 03:45 PM
  2. another problem with catstr() this time
    By the_winky_files in forum C Programming
    Replies: 19
    Last Post: 09-22-2005, 04:20 PM
  3. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 01:21 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Problem with time on the board, or is it just me?
    By biosninja in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 10-23-2002, 05:45 AM