There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer audio black screen boot bsod computer connection crash css dell display driver drivers email error ethernet excel explorer firefox firefox 3 game hard drive internet internet explorer itunes laptop lcd linux malware network networking nvidia outlook outlook 2003 outlook express partition printer problem router slow software sound trojan usb video virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Solved: GetSystemTime() - Visual C++


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
mobarati's Avatar
Junior Member with 13 posts.
 
Join Date: Mar 2005
Location: Esfahan, Iran
Experience: Advanced
28-Dec-2006, 02:39 PM #1
Solved: GetSystemTime() - Visual C++
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's Avatar
Computer Specs
Distinguished Member with 6,673 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
28-Dec-2006, 03:15 PM #2
Hi mobarati,

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

Also :
Code:
SYSTEMTIME SystemTime;
GetSystemTime(&SystemTime);

Last edited by Chicon : 28-Dec-2006 03:26 PM.
IMM's Avatar
IMM IMM is offline IMM is authorized to help remove malware.
Distinguished Member with 3,160 posts.
 
Join Date: Feb 2002
28-Dec-2006, 08:12 PM #3
Quote:
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's Avatar
Junior Member with 13 posts.
 
Join Date: Mar 2005
Location: Esfahan, Iran
Experience: Advanced
29-Dec-2006, 06:44 AM #4
Yes. Some people call it the DOS box too.
IMM's Avatar
IMM IMM is offline IMM is authorized to help remove malware.
Distinguished Member with 3,160 posts.
 
Join Date: Feb 2002
29-Dec-2006, 03:01 PM #5
Quote:
Originally Posted by mobarati
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's Avatar
Junior Member with 13 posts.
 
Join Date: Mar 2005
Location: Esfahan, Iran
Experience: Advanced
29-Dec-2006, 04:26 PM #6
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's Avatar
IMM IMM is offline IMM is authorized to help remove malware.
Distinguished Member with 3,160 posts.
 
Join Date: Feb 2002
29-Dec-2006, 07:00 PM #7
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 ?

Last edited by IMM : 29-Dec-2006 07:15 PM.
IMM's Avatar
IMM IMM is offline IMM is authorized to help remove malware.
Distinguished Member with 3,160 posts.
 
Join Date: Feb 2002
29-Dec-2006, 09:42 PM #8
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.

Last edited by IMM : 30-Dec-2006 12:35 AM.
mobarati's Avatar
Junior Member with 13 posts.
 
Join Date: Mar 2005
Location: Esfahan, Iran
Experience: Advanced
30-Dec-2006, 02:07 AM #9
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.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:12 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.