Sign in to follow this  
ThreeMarks

C++ Equivalent of DateTime.Now.Ticks

Recommended Posts

ThreeMarks    100
In C# .Net, I can the do the following:

long ticks = DateTime.Now.Ticks;

How can I achieve the same result using C++? (I'm not so familiar with C++ libraries).
Thanks.

Share this post


Link to post
Share on other sites
Serapth    6671
[quote name='ThreeMarks' timestamp='1313594948' post='4850361']
In C# .Net, I can the do the following:

long ticks = DateTime.Now.Ticks;

How can I achieve the same result using C++? (I'm not so familiar with C++ libraries).
Thanks.
[/quote]


What you are looking for is clock() which returns the number of ticks since the program started. To translate "Ticks" into something meaningful, divide by the macro CLOCKS_PER_SEC. All are included in <ctime>

Share this post


Link to post
Share on other sites
ThreeMarks    100
[quote name='Serapth' timestamp='1313595343' post='4850362']
[quote name='ThreeMarks' timestamp='1313594948' post='4850361']
In C# .Net, I can the do the following:

long ticks = DateTime.Now.Ticks;

How can I achieve the same result using C++? (I'm not so familiar with C++ libraries).
Thanks.
[/quote]


What you are looking for is clock() which returns the number of ticks since the program started. To translate "Ticks" into something meaningful, divide by the macro CLOCKS_PER_SEC. All are included in <ctime>
[/quote]

Thans Serapth. I found this earlier, but I don't see how this can give me the total ticks at, say today's date. Which I belive in C# is from January /01/0001 if I'm correct.

Share this post


Link to post
Share on other sites
Serapth    6671
[quote name='ThreeMarks' timestamp='1313607464' post='4850435']
[quote name='Serapth' timestamp='1313595343' post='4850362']
[quote name='ThreeMarks' timestamp='1313594948' post='4850361']
In C# .Net, I can the do the following:

long ticks = DateTime.Now.Ticks;

How can I achieve the same result using C++? (I'm not so familiar with C++ libraries).
Thanks.
[/quote]


What you are looking for is clock() which returns the number of ticks since the program started. To translate "Ticks" into something meaningful, divide by the macro CLOCKS_PER_SEC. All are included in <ctime>
[/quote]

Thans Serapth. I found this earlier, but I don't see how this can give me the total ticks at, say today's date. Which I belive in C# is from January /01/0001 if I'm correct.
[/quote]

Well you can use std::time() to get a time_t type, which is a running total of number of seconds in unix time ( based on 1970 ) but frankly it's a horrible system, doesn't account for leap seconds and on 32bit systems will roll over in 2038. Otherwise, no.

If you are looking for microsecond precision, I don't believe it is possible, but you may be able to accomplish what you are looking for ( what are you look for anyways? ) using boost::timer

Share this post


Link to post
Share on other sites
yewbie    677
If your using windows you can use:

[url="http://msdn.microsoft.com/en-us/library/ms644904%28v=vs.85%29.aspx"]QueryPerformanceCounter[/url]

Share this post


Link to post
Share on other sites
adeyblue    549
The one to one conversion is approximately:
[source lang="cpp"]
ULONGLONG DateTimeNowTicks()
{
SYSTEMTIME localTime;
GetLocalTime(&localTime);
FILETIME tocks;
SystemTimeToFileTime(&localTime, &tocks);
ULONGLONG ticks = (ULONGLONG(tocks.dwHighDateTime) << 32) | tocks.dwLowDateTime;
// number of days from beginning to 1601 multiplied by ticks per day
return ticks + 0x701ce1722770000ULL;
}
[/source]

Depending on what you want it for, the suggestions posted by others may be more appropriate

Share this post


Link to post
Share on other sites
ThreeMarks    100
Thanks for the reponses folks. So, what I am trying to achieve is the following:

I have an Excel Addin (in C#) which makes calls to C++ dll. I'd like to prevent the dll from being loaded by the Addin after a certain date - say 3 months into the future. So what I've done is taken the future timestamp and broken it into some numbers using mulitply/add/divide etc.

In the dll I then reconstruct the timestamp (a long integer, the ticks value I plugged into a C# console app) and want to compare this value to the current date time to check for expiration. The reason I deconstruct and reconstruct the the expiration date is to at least make it difficult to figure out how it's done and prevent someone disassembling.

ThreeMarks.

Share this post


Link to post
Share on other sites
wolfscaptain    200
If gigantic companies who waste a whole lot of money on protections (instead of making actual content to their software, sadly) yet keep losing to crackers before their software even gets released, what makes you think you will win?

Bottom line - crackers always win. Instead of wasting time and money, I think people should make their software more worthwile or/and cheaper, and they will profit in the long run.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this