Getting Time Differences

This is a discussion on Getting Time Differences within the C++ Programming forums, part of the General Programming Boards category; Hey, With a program I'm working on now I get the time in one of three different ways: Fri, 31 ...

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Getting Time Differences

    Hey,

    With a program I'm working on now I get the time in one of three different ways:
    Fri, 31 Dec 1999 23:59:59 GMT
    Friday, 31-Dec-99 23:59:59 GMT
    Fri Dec 31 23:59:59 1999

    I need to calculate the difference between the time given and the current time. I know there are functions to convert a time_t structure to a string, is there any way to work backwards? I figure if I can get it down to a tm struct or a time_t, I'll be able to use the difftime(?) function to get the difference.

    So what I'm asking is, is there a function I can call that converts a string like the above into one of these structures, or will I have to write my own?

    Thanks,

    Paul

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    10
    Use COleDateTime MFC class.
    You can get the current time as follows:

    COleDateTime today = COleDateTime::GetCurrentTime();
    CString strTime;
    strTime.Format("%d %d %d", today.GetMonth(), today.GetDay(), today.GetYear());
    Something like that.

    or you can also use CTime, but I think you're much better off with COleDateTime (see MSDN).
    Mullah,
    MSVC++6.0
    (Win 2K Pro)

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Thanks Mullah, but I can already get the current time (although I bookmarked the MSDN reference for later use, thanks for that).
    My problem lies in converting a string into a time structure, and COleDateTime doesn't seem to be able to do that Any suggestions?

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    10
    for converting strings into some other things you have atoi (convert a string to an integer), atol (convert to a long), atof (convert to a double) C functions. See what you can do with these functions. You can pass CString objects to such functions and get the value from the CString and use it.
    Mullah,
    MSVC++6.0
    (Win 2K Pro)

  5. #5
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Yeah I know I can write it all myself, I'm about to write a function that uses istringstream and other standard stuff to convert the numbers and store them in a struct tm, but I had hoped there might be a function like this:

    struct tm GetTime(string, string);

    And I could say
    struct tm MyTm;
    MyTm = GetTime("%h, %m, %s", oldTime);

    Where the first string is the format of the string, and the second is the string to be converted. I hoped someone might have done this before, but oh well, looks like I'll have to do it myself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Representing floats with color?
    By DrSnuggles in forum C++ Programming
    Replies: 113
    Last Post: 12-30-2008, 09:11 AM
  2. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 08:51 PM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  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, 07:17 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM

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