2

I am just develop the game in C# and I was migrated C++ newly, I just want to rewrite the game into C++, and almost successfully to translate of all the code from C# to C++, but there are some problem, my game use System.DateTime.Now.Ticks in C#... and I was stuck

so.. there are something equivalent with System.DateTime.Now.Ticks in C++?

jwueller
  • 30,582
  • 4
  • 66
  • 70
CXO2
  • 628
  • 10
  • 28

4 Answers4

2

look at this tutorial on cplusplus.com to get platform independent.

Function Clock() returns the amount of tics that this process have consumed. To access this function, you need to include: #include <time.h>.

To get the same thing, only in seconds, you can use the CLOCKS_PER_SEC macro, so you can do: Clock() / CLOCKS_PER_SEC to get the number of seconds that this process took. But remember that you might loose some precision this way.


You might not need this exact functionality though... If you are looking for the exact time that has elapsed since the program has started, you (as far as I can remember) must use difftime() function. You might be loosing some precision if you need exact tics, depending on your platform.

This way you must save the current time at the beginning of your application, and subtract it from the current time during the application.

#include <stdio.h>
#include <time.h>

time_t programstart;

int main ()
{
  time(&programstart); //assign the current time 

  /*... the program ...*/

  //get seconds since start
  double secondssincestart = difftime(time(NULL), programstart);

  printf ("the program took: %f seconds\n", secondssincestart);

  return 0;
}

EDIT:

Since this post still gets some attention, it is important to note, that today's C++11 has standard, easy to use, and pretty handy library chrono.

Kupto
  • 2,802
  • 2
  • 13
  • 16
0

Use QueryUnbiasedInterruptTime. Like DateTime.Ticks, each tick of QueryUnbiasedInterruptTime is also 100-nanosecond. If you need higher resolution, you need to go for QueryPerformanceCounter.

Link: http://msdn.microsoft.com/en-us/library/windows/desktop/ee662306(v=vs.85).aspx

Chen
  • 1,654
  • 2
  • 13
  • 21
0

There isn't an equivalent class or method, but you can do this:

SYSTEMTIME systemTime;
GetLocalTime(&systemTime);

FILETIME fileTime;
SystemTimeToFileTime(&systemTime, &fileTime);

ULARGE_INTEGER largeInteger;
largeInteger.LowPart = fileTime.dwLowDateTime;
largeInteger.HighPart = fileTime.dwHighDateTime;

ULONGLONG ticks = reinterpret_cast<ULONGLONG&>(largeInteger);
user1610015
  • 6,561
  • 2
  • 15
  • 18
0

In Windows with MFC the Ticks equivalent to System.DateTime.Now.Ticks

ULONGLONG GetTicksNow()
{
    COleDateTime epoch(100, 1, 1, 00, 00, 00);
    COleDateTime currTime = COleDateTime::GetCurrentTime();
    COleDateTimeSpan span = currTime - epoch;
    CTimeSpan cSpan(span.GetDays(), span.GetHours(), span.GetMinutes(), 
                                                       span.GetSeconds());
    ULONGLONG diff = cSpan.GetTotalSeconds();
    LONG missingDays = 365 * 99 + 24;
    CTimeSpan centSpan(missingDays, 0, 0, 0);
    ULONGLONG centSeconds = centSpan.GetTotalSeconds();// *1000000000;//
    ULONGLONG totSec = (diff + centSeconds)*10000000;
    return totSec ;
}
Arun K
  • 413
  • 4
  • 15