Terrible confusion with time variables

This is a discussion on Terrible confusion with time variables within the C++ Programming forums, part of the General Programming Boards category; I keep getting through the debugger and my output that according to the _timeb struct the time in seconds from ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    34

    Terrible confusion with time variables

    I keep getting through the debugger and my output that according to the _timeb struct the time in seconds from January 1, 1970 is around 8.44456*10^16. However, some simple calculation shows that it is in fact around 1.2173*10^9. This is off by 7 orders of magnitude and doesn't look anything like the correct value. Is there something that I'm missing? Can someone clear up my confusion?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Exactly what are you inspecting to see your timeb value?

    --
    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
    Jul 2008
    Posts
    34
    Quote Originally Posted by matsp View Post
    Exactly what are you inspecting to see your timeb value?

    --
    Mats
    I'm sorry, but I don't understand what you mean by "what are you inspecting to see."

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, lets put it another way: Your calculations are good, you should end up with approximately 1.1E9. So I'm trying to find out if, for example, you have got the wrong data output, so you are looking at, perhaps, the address of the value, rather than the value itself.

    By the way, I'm not entirely sure that time HAS to be stored as seconds from 1 jan 1970 - it conventionally is, but I'm not quite sure nit's guaranteed.

    --
    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.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    Nevermind, I found a different time function to use that is much more reliable/easier. Thanks anyways!

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Care to share your new insight with the rest of us?
    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.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    Well, I found this way to do it:
    Code:
    time_t rawtime; 
    struct tm* timeinfo;
    
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    
    f_Start_Time = (timeinfo->tm_hour + (timeinfo->tm_min)/60. + (timeinfo->tm_sec)/3600.);
    However, I have now been informed that I need to also implement milliseconds into my code and this can only go down to seconds. Now I'm really in need of some help. I've searched everywhere and tried a few different techniques to no avail. SYSTEMTIME crashes my program on runtime just by declaring the object. And Date objects just don't seem to want to instantiate.

    Now, I readily admit that both of those problems could possibly be solved by just declaring them correctly (most of my training has been in Java, so the transition can prove tricky at times).

    This is how I've tried SYSTEMTIME
    Code:
    #include "ATLComTime.h"
    
    SYSTEMTIME SystemTime;
    
    getSystemTime(&SystemTime);
    and for Date
    Code:
    dateObj d;
    
    d = new Date();
    Any help on either of those or new suggestions would be greatly appreciated.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I can find no documentation that suggests SYSTEMTIME is defined in ATLComTime.h (MSDN says windows.h). That may help.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    I tried including windows.h and also stdio.h but it stills crashes.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by LowlyIntern View Post
    I tried including windows.h and also stdio.h but it stills crashes.
    I would help a whole lot if you post a more complete piece of code [ideally something that is complete enough to both compile and show the actual problem].

    --
    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.

  11. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    That's actually quite difficult as there was no main function in the program as it was given to me (don't ask, nobody here knows why) and it doesn't output in a command window (it won't even let me output in the command window), but out through a port to an RS 422 card in hex. Plus, there's some 5 to 6 thousand lines of code to run the whole thing to show the problem. Basically, when I click "Apply" on my dialog box and this message comes up "OS Attitude MFC Application has encountered a problem and needs to close. We are sorry for the inconvenience."

    However, this comes up if all im doing is simply declaring: SYSTEMTIME systime; (with windows.h and stdio.h included, of course) if that helps. Also, I don't know if this has to do with anything, but I'm also using timeb.h (was in the program when it was given to me) and time.h (because it does everything but milliseconds and works for now).

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Unfortunately, it would be hard to figure out what the problem is - however, I expect that you could trap it in WinDBG in that case, and look at the call-stack to figure out what is going on.

    --
    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.

  13. #13
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    The issue has (hopefully) been resolved. Turns out SYSEMTIME doesn't like to be declared in a header file, go figure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. What is the best way to record a process execution time?
    By hanash in forum Linux Programming
    Replies: 7
    Last Post: 03-15-2006, 06:17 AM
  3. static variables confusion
    By Kleid-0 in forum C Programming
    Replies: 6
    Last Post: 01-08-2005, 05:11 PM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 05:00 PM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM

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