In C++, what's the easiest way to get the system time?

Discussion in 'Computer Programming, Emulation, and Game Modding' started by jamieyello, Feb 15, 2016.

  1. jamieyello
    OP

    jamieyello Professional Dumbass

    Member
    376
    255
    Mar 11, 2013
    United States
    I am writing a SmileBASIC interpreter, SmileBASIC being a modified version of basic that ran on the official licensed games Petit Computer (DSi) and SmileBASIC (3ds). I need a way to get time in order to recreate the WAIT (number) function, which takes in a number and stops for that amount of time in 1/60ths seconds. It doesn't actually wait, though, it throttles more like, so I need a way to measure when 1/60th of a second has passed, and not add the time it took to execute other running code to that.

    I did this with <chrono>, but every time I make a specific completely unrelated string array larger than a couple of strings it returns a constant -2 million something, it's just not working correctly. Is there a good alternative, or does anyone know why it's acting this way?

    Code:
    ...
    #include <chrono>
    ...
    
    //Outside my main loop
    auto SBStartTime = Clock::now();
    
    //This function returns how long the program has been running in 1/60ths of seconds
    long long int getTime(){
         auto t2 = Clock::now();
         return std::chrono::duration_cast<std::chrono::nanoseconds>(t2-SBStartTime).count() / 16666666.6667;
    }
    
     
    Last edited by jamieyello, Feb 15, 2016
  2. Joe88

    Joe88 [λ]

    Member
    11,646
    2,927
    Jan 6, 2008
    United States
    NYC
  3. jamieyello
    OP

    jamieyello Professional Dumbass

    Member
    376
    255
    Mar 11, 2013
    United States
    I'm pretty new to C++, is there a way to get nanoseconds from that? (Better yet 1/60th seconds so I don't have to bother with conversion)

    I actually think I did look at that and couldn't figure out how to get more accurate time than seconds.
     
    Last edited by jamieyello, Feb 15, 2016
  4. jamieyello
    OP

    jamieyello Professional Dumbass

    Member
    376
    255
    Mar 11, 2013
    United States
    I basically fixed this by moving the place where I declared the starting time (
    autoSBStartTime=Clock::now();) to after all other arrays were set. I think they were conflicting somehow. Weird.

    Edit Hey my code got a winky face