-1

Hey everyone I am using the structure SYSTEMTIME to perform arithematic with c++ to create a countdown timer. How would I set a custom time and make it countdown? This is a very generic question but if anyone who has experience with the SYSTEMTIME structure would be able to give me some assistance that would be very much appreciated!

  1. I want to set a default time for SYSTEMTIME so like

    SYSTEMTIME tvar;
    GetLocalTime(&tvar); // instead of using this to initiate the clock i'd like to set the time by myself
    

2. Also, how can I make this clock countdown from that time?

Take note that since I am using GDI it is necessary for me to use this structure and since the code that I already worked on with my group uses this structure we already have it implemented, so there is no room for changing, can anyone give me a solution to solve those two issues that I have?

!!! EDIT !!! I'm trying to recreate this timer here http://www.picksourcecode.com/ps/ct/161097.php in countdown format with just hours and minutes for a bomb timer for the adventure game that I am making with opengl. How would I reprogram this code so that I can incorporate a countdown digital clock of this standard?

Any help is appreciated, thanks so much!

user3251225
  • 147
  • 3
  • 11
  • 1
    The members of the [structure](http://msdn.microsoft.com/en-us/library/windows/desktop/ms724950%28v=vs.85%29.aspx) are well defined, what don't you understand about it? – Retired Ninja Jun 10 '14 at 01:25
  • I just said 2 times that I don't want to set the parameter with the system or local time... I WANT TO define my own time and countdown from it. I never mentioned that I don't understand how to use the function. What I don't understand is how can I define a static time myself to start from and instead of counting up from that time, count down like a timer on a stopwatch or like those bombs in the movies. – user3251225 Jun 10 '14 at 01:55
  • @user3251225: did you try to click on the link that Retired Ninja provided? did you try to read what he wrote? it's just one sentence. – Cheers and hth. - Alf Jun 10 '14 at 02:01
  • Just bumping the question since I updated the original with a more converse dialog of what I'm trying to achieve. – user3251225 Jun 10 '14 at 02:20
  • 1
    The algorithm for how to do this is simple. Get the current time, add the timer amount to it, and save that value somewhere. Periodically, subtract the current time from the end time you saved and display the difference. As the link above mentions, you'll most likely want to convert the systemtime to a filetime in order to add to it or find the difference between two values, then convert it back. – Retired Ninja Jun 10 '14 at 05:28

2 Answers2

1

The idea is fundamentally wrong. I would recommend to use FILETIME instead. The code should look like:

typedef __int64 TDateTime;  // This is actually an alias to NT FILETIME.

inline TDateTime  CurrDateTime()
{
      TDateTime param;
      GetSystemTimeAsFileTime((FILETIME*)&param);
      return(param);
}

#define   ONE_SECOND    ((__int64)10000000)

TDateTime myFinalTime = CurrDateTime() + 30*ONE_SECOND;
while (CurrDateTime() < myFinalTime )
    Sleep(50);

In fact FILETIME is a 64 bit integer. the function GetSystemTimeAsFileTime() is cheap. It works perfectly in the code like above. I used this many times.

If you want to show the TDateTime value on the screen you need to call FileTimeToLocalFileTime(...) and then FileTimeToSystemTime(...) APIs.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
0

You can use timer for this purpose and I suggest you to use GetTickCount function. You can get milliseconds that the PC is running and get it again and get the passed time using subtraction.

Mustafa Chelik
  • 2,113
  • 2
  • 17
  • 29
  • But how would I output the numbers in win32? – user3251225 Jun 10 '14 at 20:53
  • What do you mean by output exactly? – Mustafa Chelik Jun 10 '14 at 20:55
  • Like for example, I have a win32 default class, I register the window and I create it in c++. How would I output this countdown timer to that screen using the method you stated here? – user3251225 Jun 10 '14 at 20:58
  • This is a different question. Well, you can create a textbox or label and periodically call SetWindowText to set the current time. I think it's better to do these tasks using MFC library. After some experience, you can switch to Win32. – Mustafa Chelik Jun 10 '14 at 21:03
  • GetTickCount() will work fine, although it overlaps approximately every month. On a system that may work for a long time this might be a problem. – Kirill Kobelev Jun 11 '14 at 03:56