Thread: Calculating time

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    79

    Calculating time

    ok as a second part of my program i want to make. I want it to check how many weeks has passes since the program was first run. I've been reading the c library time header file to try and get an idea how to do this.

    firstly i was going to do this

    Code:
    time_t tim;
    struct tm *currtim;
    time(&tim);
    currtim = localtime(&time);
    However i believe this only assigns the value of how many days months hours etc there has been since 1st of jan 1970 to the values in the currtim structure.

    So i thought about using

    Code:
    char timestr = ascitime(currtim);
    This sets the current date and time. However it gives it in a string format. Which means i cannot use difftime() to calculate the difference in times.

    finally i thought about using just
    Code:
      time_t start;
      time(&start);
    Then writing the value to a file. However im not sure how to write something that is a data type time_t to a file correctly, then reading it again and comparting it with endtime.

    Any ideas?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Unfortunately, time_t is an implementation dependent type, and not a defined size or format. You could store it as some sort of string, but it would not be portable to other machine / OS / compiler architectures.

    You can, however, convert it to a struct tm type, which has well-defined components, and use the "mktime" function to make it back into the integral type of time_t.

    Have a look at:
    http://www.hmug.org/man/3/ctime.php

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    Ok i understand what you mean about assigning it to a struct tm type and then using mktime() to make it into a time_t

    But the problem i have is how to assign the values of the current date and time to struct tm. any functions ive seen so far only assing how many days months years etc have passed since 1st jan 1970.

    Basically the challenge is to work how to get todays date stored in the struct tm structure.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    difftime() will take two time_t's and tell you the difference in seconds.
    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.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    I know that salem. But thats not what im stuck at. The bit im stuck at is getting the current month, year and day into the struct tm structure. If i can get them in there i understand how to use the mktime and difftime functions to get the result.

    question is how do i get the current date in the struct tm structure.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    gmtime is your friend!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    Hey Elysia. I used your advise about gmtime and have come up with this. I dont have a compiler here in work so i'm not sure if works.

    Code:
    void storeTime(FILE *p1)
    {
    	time_t tim;
    	struct tm *curtim;
    	time(&tim);
    	curtim =gmtime(&tim);
    
    	p1 = fopen("timestore.txt",w);
    
            if(p1 != NULL)
    		{
    			fprintf(p1,"%d %d %d",curtim->tm_mday,curtim->tm_mon,curtim->tm_year);
                            fclose(p1);
    		}
    	
    	
    }
    
    
    
    void readtime ()       //reads the time which was stored in the file by storetime
    {
            FILE *p2       //file pointer
            tm *newtm;     // new tm structure
            time_t timer;  //used with mktime()
            double diff;   //used to store the difference in times
    
    	p2 = fopen("timestore.txt",r);
            
            if(p2 != NULL)
    		{
    		        fscanf(p2,%d %d %d,newtm->tm_dat,newtm->tm_mon,newtm->tm_year); //scam the times from file
    			fclose(p2);
    			
    		}
            timer = mktime(newtm); // give timer a value
    
    
    	//create a time_t for current time to compare
    
    	time_t timeNow;
    	time(&timeNow);
            diff = difftime(timeNow,timer);
    	
    	printf("%d : Time difference in seconds",diff);
    
    }

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    void storeTime()
    {
    	FILE* p1;
    	time_t tim;
    	struct tm* curtim;
    	time(&tim);
    	curtim = gmtime(&tim);
    
    	p1 = fopen("timestore.txt", "w");
            if(p1 != NULL)
    	{
    		fprintf(p1,"%d %d %d", curtim->tm_mday, curtim->tm_mon, curtim->tm_year);
    		fclose(p1);
    	}
    }
    
    void readtime()       //reads the time which was stored in the file by storetime
    {
    	FILE* p2;       //file pointer
    	tm* newtm;     // new tm structure
    	time_t timer;  //used with mktime()
    	double diff;   //used to store the difference in times
    
    	p2 = fopen("timestore.txt", "r");
    	if (p2 != NULL)
    	{
    	        fscanf(p2,"%d %d %d", newtm->tm_mday, newtm->tm_mon, newtm->tm_year); //scam the times from file
    		fclose(p2);
    	}
            timer = mktime(newtm); // give timer a value
    
    	//create a time_t for current time to compare
    	time_t timeNow;
    	time(&timeNow);
            diff = difftime(timeNow, timer);
    	
    	printf("%d : Time difference in seconds",diff);
    }
    Corrections in red.
    Questionable part of the code in blue. If you fail to read the date from the file, you can't compare the dates, can you? Yet, the code will continue even if it fails.

    The code compiles. I don't know if it works.

    Oh and you don't need to indent the braces and it's a bad idea to mix spaces and tabs. Use one.
    Last edited by Elysia; 02-01-2008 at 05:41 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	        fscanf(p2,"%d %d %d", &newtm->tm_mday, &newtm->tm_mon, &newtm->tm_year); //scam the times from file
    You need the ADDRESS of the data you are reading from the file.

    And are you actually scamming or scanning?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    Thanks guys for your help. I'll see if it works when i get home. only 7 hours away lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  2. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  3. Replies: 3
    Last Post: 11-15-2003, 11:20 AM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM