making a program expire

This is a discussion on making a program expire within the C++ Programming forums, part of the General Programming Boards category; Hi all ... i was just wondering is there is a function that could be used in conjunction with a ...

  1. #1
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59

    making a program expire

    Hi all ... i was just wondering is there is a function that could be used in conjunction with a TM structure to get the system date and time? I am trying to make a program expire by using the below code in the constructor of a class. However i see no function that can fill this structure.

    Code:
    struct tm today;
    
    //getdate(&today);  /* need a getdate function here */
    
    if(today.tm_year > 1900)
    {
    	if(today.tm_mon > 6)
    	{
    	             cout << "Program Error ... Aborting!";
    	             exit(1);
    	}
    }
    Im using MSVC++ 6.0 ... thanks for any help

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Create a struct tm for the time you want the program to expire
    use time() and localtime() to get a struct tm for now
    use difftime() to work out what the difference is. When the sign of the result changes, then the time is past.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    Right on ... thanks for the reply, ill give that a shot!



  4. #4
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    Hi again ... just had another question about making a program expire. After i have a TM for the time i want the program to expire, and a TM from the time() and localtime() functions. How would i convert these TM structures into a time_t type, so i could use the difftime() function on them ? Or would it be better to just compare the two TM structures to see if the one from the computer is past the expire TM ?

    Code:
    void CTest::programExpiration(void)
    {
    	double difference = 0;
    	time_t systime;
    	struct tm expire;
    	expire.tm_mday = 10;
    	expire.tm_mon  = 6;
    	expire.tm_year = 2004;
    	time(&systime);
    	struct tm *today = localtime(&systime);
    
    	//difference = difftime(today, expire);
    }
    Any help would be appreciated ... thanks again

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How would i convert these TM structures into a time_t type, so i could use the difftime() function on them ?
    The mktime function makes a time_t out of a struct tm *.
    My best code is written with the delete key.

  6. #6
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    yes i did try the mktime() function before, and it worked for the "struct tm *today" structure, however does nother for the "struct tm expire" structure. Is there a function that can convert a "struct tm" into a time_t type ?

    :P

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there a function that can convert a "struct tm" into a time_t type ?
    No, but you could always pass the address of expire to make it a pointer.
    My best code is written with the delete key.

  8. #8
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    I tried that also, however the program crashes, and the difftime() return value is always the same no matter how i change the expire TM struct

    Code:
    void CMk3dpdek::programExpiration(void)
    {
    	double difference = 0;
    	time_t systime, today_t, expire_t;
    	struct tm expire;
    	expire.tm_mday = 9;
    	expire.tm_mon  = 6;
    	expire.tm_year = 2004;
    	time(&systime);
    	struct tm *today = localtime(&systime);
    	today_t  = mktime(today);
    	expire_t = mktime(&expire);
    	difference = difftime(today_t, expire_t);
    
    	cout << endl << "THE DIFF IS : " << difference << endl;
    	cout << endl << "TODAY TIME : " << ctime(&today_t);
    	cout << endl << "EXPIRE TIME : " << ctime(&expire_t);
    }

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You didn't normalize the values in expire before calling mktime. This is a good reason to check for return values so that you can check for errors.
    Code:
    void CMk3dpdek::programExpiration(void)
    {
    	double difference = 0;
    	time_t systime, today_t, expire_t;
    	time(&systime);
    	struct tm *today = localtime(&systime);
    	struct tm expire = *today;
    	expire.tm_mday = 14;
    	expire.tm_mon  = 6;
    	expire.tm_year = 2004 - 1900;
    	today_t  = mktime(today);
    	expire_t = mktime(&expire);
    	difference = difftime(today_t, expire_t);
    
    	cout << endl << "THE DIFF IS : " << difference << endl;
    	cout << endl << "TODAY TIME : " << ctime(&today_t);
    	cout << endl << "EXPIRE TIME : " << ctime(&expire_t);
    }
    My best code is written with the delete key.

  10. #10
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    I think i see what you mean. So by getting the localtime() first then doing ...

    Code:
    struct tm expire = *today;
    I guess that would be a "copy constructor" from one structure to another and make all the values the same.

    But ya seems to work now thank you for all the help, guess i just have to add a if statement after the difftime() function and i can test it out.

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a program run on startup
    By Poincare in forum C Programming
    Replies: 10
    Last Post: 06-21-2009, 12:50 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Making interest rate program in C
    By canadas321 in forum C Programming
    Replies: 6
    Last Post: 06-23-2005, 11:59 AM
  5. I need help with making a program which....
    By Tonyukuk in forum C# Programming
    Replies: 1
    Last Post: 04-16-2003, 10:49 PM

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