Thread: Trouble with 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,708
    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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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, 03:02 PM
  2. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  3. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  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, 01:01 AM