Thread: problems with time

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    problems with time

    I am trying to learn how to use the time.h and am running into problems keeping the times straight. In my program , "grad" always become the same that "timeinfo" is, and I cant figure out why. There seems to be no reason why the values would change. How do I make sure my structs keep their value?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your code
    Code:
    #include <stdio.h>
    #include <time.h>
    
    
    int main()
    {
    	time_t now;
    	time_t timeGrad;
    	struct tm *timeinfo;
    	struct tm *grad;
    
    	/* current time */
    	time(&now);
    	timeinfo = localtime(&now);
    
    	/* graduation time  */
    	time(&timeGrad);
    	grad = localtime(&timeGrad);
    
    	grad->tm_sec = 0;
    	grad->tm_min = 0;
    	grad->tm_hour = 0;
    	grad->tm_mday = 24;
    	grad->tm_mon = 4;
    	grad->tm_year = 113;
    //	printf("Grad date is %d-%d-%d\n", grad->tm_mon+1, grad->tm_mday, grad->tm_year+1900);
    
    
    	/*print out unformatted*/
    	printf("The current time is %s", ctime(&now));
    
    	/*Format and print time, mm-dd-yyyy*/
    	printf("The date is %d-%d-%d\n", timeinfo->tm_mon+1, timeinfo->tm_mday, timeinfo->tm_year+1900);
    	printf("Grad date is %d-%d-%d\n", grad->tm_mon+1, grad->tm_mday, grad->tm_year+1900);
    
    
    
    	return 0;
    
    }
    localtime() returns a pointer to some hidden local storage. So as you modify it, everything that points to it ALSO changes.

    If you want to make unique copies, do this
    struct tm timeinfo = *localtime(&now);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    1
    There is a thread safe version of the api, Use this one:

    localtime_r(const time_t *timep, struct tm *result)

    You pass in a pointer to the tm struct you want to use. Then it doesn't get overwritten.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NtSetSystemTime() failed
    By medp7060 in forum C++ Programming
    Replies: 11
    Last Post: 04-02-2010, 02:58 AM
  2. Determine the closest departure time
    By Kyeong in forum C Programming
    Replies: 9
    Last Post: 10-07-2008, 08:06 PM
  3. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  4. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM
  5. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM

Tags for this Thread