Thread: Getting difference in time

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    8

    Getting difference in time

    I can not seem to figure out how to get the difference from a future date and the present date. I use:
    Code:
    time_t current;
    time(&current);
    to get the current time but i am not sure how to set a future time. I found a post showing how the difftime (time_t time1,time_t time2) works but i am not sure how to set time_t to anything other then getting the current time. Thanks in advance.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Well you can set your time at the start of the program then set a current time to get the current time then call diftime to see the difference between the start and current time.
    Woop?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    8
    How do i set the time at the start of the program. What im trying to do is allow the user to input a date and calculate the time to that date from current time. Thanks for the reply.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    time_t t1,t2;
    time(&t1);
    // all your code
    time(&t2);
    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 2005
    Posts
    8
    I'm sorry, i still do not understand how to set a specific time. For instance how can i set, from Salem post, t2 as 5/5/2005?

    *edit*
    I'm guessing that i need to work it all out in seconds from Jan 1, 1970. Have the user input a future date and then convert it into seconds.
    Last edited by Dream Theater; 01-24-2005 at 03:36 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Oh, if you want to build an arbitrary time, then you use the mktime() function for the date you're interested in (this returns a time_t)
    Then use difftime() to work out the difference between now and then.
    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.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    8
    I can't seem to get mktime() to work. Could i get an example please. Thank you.
    *edit Or can someone point me to a resource to learn how to work with ctime
    Last edited by Dream Theater; 01-24-2005 at 09:29 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    8
    Thanks, I did search didn't come up with that thread. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 02:03 AM
  2. Pointer problem... i think
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 11-30-2005, 03:45 PM
  3. Replies: 3
    Last Post: 11-15-2003, 11:20 AM
  4. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  5. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM