Tech Support Guy banner

Solved: GetSystemTime() - Visual C++

28381 Views 8 Replies 3 Participants Last post by  mobarati
i've been looking around on the net for days now, trying to find out how the GetSystemTime() function works in visual c++.

all i want to do is to have the date and time on my console screen at all times. someone told me i should be using the time intrrupt, but i have no idea how to do that either.

can anyone give me the code on how to read the date and time, from the system and then show it on the screen so it automatically gets updated by the second.

Not open for further replies.
1 - 4 of 9 Posts
all i want to do is to have the date and time on my console screen at all times.
By 'console' - do you mean what some call the DOS box ?
mobarati said:
Yes. Some people call it the DOS box too.
What is the OS?
Under DOS - this would have been done with a TSR which installs an ISR I think (it's been a long time). That still doesn't cover the issue of the screen-write and (probably) would have required the ansi driver to be installed.

With an XP console you are dealing with the ntvdm (which converts the dos stuff it sees into win32 calls - imperfectly). You can search on some of the terms I've mentioned and see if it's any help. I'll try to keep my eyes open for something.
Actually - I don't think I misunderstood - but it's actually a thorny question and there are several routes.
If you are curious about the DOS interrupt which returned the time to something like an ISR (Interrupt Service Routine) installed by a TSR (Terminate and Stay Resident) program it is/was:
	AH = 02h
	CF clear to avoid bug (see below)
Return: CF clear if successful
	    CH = hour (BCD)
	    CL = minutes (BCD)
	    DH = seconds (BCD)
	    DL = daylight savings flag (00h standard time, 01h daylight time)
	CF set on error (i.e. clock not running or in middle of update)
It's a complicated subject in some ways.

In a simpler vein (for what you want to do) - you could perhaps use a timer to poll the systemtime frequently (seperate thread would be nice :) ) and then update the screen when >= 1sec occurs
You would also probably want look at functions like GetConsole() for your writes.
Another thought would be to look at spinlocks.

If you want it to show the time and do nothing else (not be a functional console) then it is much simpler -- is that what you are after - and I did misunderstand ?
See less See more
I had a couple of minutes - and if you just want the time showing in the simplest sense that I mentioned at the bottom of the previous post -- perhaps you could work from the following.
#include <windows.h>
#include <stdio.h>
#include <conio.h>

HANDLE  hConsole;
COORD    Home = { 0, 0 };

void ClearScreen( void )
  DWORD    dRet;
  COORD    Home = { 0, 0 };
  FillConsoleOutputCharacter( hConsole, ' ', csbi.dwSize.X * csbi.dwSize.Y, Home, &dRet );

int main( void )
  WORD    ForeColor = 0;
  WORD    wAttributesOld;
  SYSTEMTIME localtim;
  DWORD dRet;
  char timstr[64] = "00:00:00\0";
  int len;
  if( ( hConsole = CreateFile(
            0L, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0L) ) == (HANDLE) -1 ) {
    printf("\nError: Unable to open console.\n");
    return( -1 );
  GetConsoleScreenBufferInfo( hConsole, &csbi );
  wAttributesOld = csbi.wAttributes;
  len = strlen(timstr);
                             len, Home, &dRet);
  while (!kbhit()) {
    sprintf(timstr, "%02d:%02d:%02d", localtim.wHour, localtim.wMinute, localtim.wSecond);
    WriteConsoleOutputCharacter(hConsole, timstr , len, Home, &dRet);
  SetConsoleTextAttribute( hConsole, wAttributesOld );
  return 0;
In this one I used LocalTime instead of SystemTime - but you can interchange them. You said VC - so if you just save that with some filename like "junk.c" - then double click on it to open it in vc - hit compile or build and let it make a default workspace - it should be fine.
See less See more
1 - 4 of 9 Posts
Not open for further replies.