Trouble with time.h

This is a discussion on Trouble with time.h within the C Programming forums, part of the General Programming Boards category; How come the two dates printed out by this program are the same? Code: #include <stdio.h> #include <stdlib.h> #include <time.h> ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    54

    Trouble with time.h

    How come the two dates printed out by this program are the same?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
    	time_t t, t2;
    	struct tm* current_time, *other_time;
    	time(&t);
    	t2 = t + (11 * 24 * 60 * 60);
    	current_time=gmtime(&t);
    	other_time=gmtime(&t2);
    	printf("%d %d %d\n", current_time->tm_mon, current_time->tm_mday, current_time->tm_year+1900);
    	printf("%d %d %d\n", other_time->tm_mon, other_time->tm_mday, other_time->tm_year+1900);
    	return 0;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    Quote Originally Posted by Boxknife View Post
    How come the two dates printed out by this program are the same?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
        time_t t, t2;
        struct tm* current_time, *other_time;
        time(&t);
        t2 = t + (11 * 24 * 60 * 60);
        current_time=gmtime(&t);
        other_time=gmtime(&t2);
        printf("%d %d %d\n", current_time->tm_mon, current_time->tm_mday, current_time->tm_year+1900);
        printf("%d %d %d\n", other_time->tm_mon, other_time->tm_mday, other_time->tm_year+1900);
        return 0;
    }
    "gmtime" returns an internally allocated structure which gets overwritten on each successive call. In other words, you need to copy the data before you call the function again.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    54
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-03-2007, 02:02 PM
  2. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 03:10 PM
  3. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 11:58 AM
  4. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM
  5. header file trouble - PLEASE HELP!!!
    By thewinout in forum C++ Programming
    Replies: 0
    Last Post: 03-23-2003, 12:01 AM

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