# Solved: GetSystemTime() - Visual C++



## 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.

thanks


----------



## Chicon

Hi mobarati,

You may be interested by this article : Date and Time in C++.

Also :


Code:


[SIZE=2]SYSTEMTIME SystemTime;
GetSystemTime(&SystemTime);[/SIZE]


----------



## IMM

> 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

Yes. Some people call it the DOS box too.


----------



## IMM

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.


----------



## mobarati

i think you miss understood me. let me make it clear. i am using Visual C++ under windows xp. now when you compile a programme a box pops up to run the ".exe" file. some people call that box the console box/screen, some call it the dos box, i don't know. it makes no difference what you call it. anyhow, all i want is to have the time on that screen. i dont want it to show the time at which the programme was ran. i want to show the time and update it every second. i asked my professor at uni and he said it needed an intrrupt, so that every second the time will jump a second and i get a clock on the screen. but he wasn't sure and he is looking to find the function that does this for me. but i am just posting this on here to see if i can find out the answer before he does, so i can get this thing done! 

thats all. sorry for any confusion.
and thanks for your concern


----------



## IMM

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:


Code:


INT 1A - TIME - GET REAL-TIME CLOCK TIME (AT,XT286,PS)
	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 ?


----------



## IMM

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.


Code:


#include <windows.h>
#include <stdio.h>
#include <conio.h>

HANDLE  hConsole;
CONSOLE_SCREEN_BUFFER_INFO csbi;
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(
           "CONOUT$", GENERIC_WRITE | GENERIC_READ,FILE_SHARE_READ | FILE_SHARE_WRITE,
            0L, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0L) ) == (HANDLE) -1 ) {
    printf("\nError: Unable to open console.\n");
    return( -1 );
  }
  printf("\n");
  
  GetConsoleScreenBufferInfo( hConsole, &csbi );
  wAttributesOld = csbi.wAttributes;
  len = strlen(timstr);
  FillConsoleOutputAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_BLUE,
                             len, Home, &dRet);
  
  while (!kbhit()) {
    GetLocalTime(&localtim);
    ClearScreen();
    sprintf(timstr, "%02d:%02d:%02d", localtim.wHour, localtim.wMinute, localtim.wSecond);
    WriteConsoleOutputCharacter(hConsole, timstr , len, Home, &dRet);
    Sleep(333);
  }
  
  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.


----------



## mobarati

Thank you, now i know that you didnt missunderstand! this is exactly what i want. 

i wanted this as a part of a bigger project, so now, having the code for it, i can manage to put this into the other project and sort things out.

thank you so much for the help.


----------

