Click here to Skip to main content
15,867,594 members
Articles / Desktop Programming / MFC

Get Hold of System Time without Using Inbuilt Functions (Can be Used with Turbo C++)

Rate me:
Please Sign up or sign in to vote.
1.11/5 (18 votes)
10 Jul 20062 min read 69K   625   9   11
Getting system timer without using inbuilt system calls

Introduction

You all might have used CTime or CTimeSpan to manipulate the system timer. Here is an article to show you how your system timer works. I will give you an idea of port communications behind the system clock manipulations. Those who are a little biased on hardware and software interfacing practices would find this article of great help.

About the Basic Routines

Those who have gone through conio.h header file would have found two interesting functions, _outp() and _inp(). I will explain each of these functions in detail.

  • _outp: This is a function used to send data to a port. This function sends a byte to a specified port. The syntax of the function is int _outp(unsigned short port, int databyte). The function returns with the data sent.
  • _inp: This is a function used to receive data from a port. The function reads a byte from a specified port. The function returns with the data read from the port. The syntax for the function is: _inp(unsigned port).

For getting the system clock, we should initialize the timer first. This is done by the command: _outp(0x70,0x95); Port no 0x70 is reserved for system clock. The current time information would then be stored in port 0x71(Memory address). If you send data 0x10, the content of the 0x71 address will be seconds value of timer. If you send data 0x02, the content of the 0x71 address will be minutes value of timer. If you send data 0x04, the content of the 0x71 address will be hours value of timer. That's it... The system clock manipulation revolves round these two absic functions. These functions are useful for those writing device drivers.

Explaining the Code

I am implementing the whole process in a class called CBaseTime. You can find the wrappings of the process in BaseTime.h and BaseTime.cpp.

C++
// Constructor: Indirectly initializes the clock. 
CBaseTime::CBaseTime() 
{ 
	_outp(0x70,0x95); 
} 

// Get current time CString 
CBaseTime::GetCurrentTime() 
{ 
	CString curTime; 
	_outp(0x70,0x10); 
	seconds = _inp(0x71); 
	_outp(0x70,0x02); 
	minutes = _inp(0x71);
	_outp(0x70,0x04); 
	hours = _inp(0x71); 
	curTime.Format("%x:%x:%x",hours,minutes,seconds); return curTime; 
} 

// Get current hours 
CString CBaseTime::GetCurrentHours() 
{ 
	CString retHours; 
	_outp(0x70,0x04); 
	hours = _inp(0x71); 
	retHours.Format("%x",hours); 
	return retHours; 
} 

// Get current minutes 
CString CBaseTime::GetCurrentMinutes() 
{ 
	CString retMins; 
	minutes = _inp(0x71); 
	_outp(0x70,0x02); 
	retMins.Format("%x",minutes); 
	return retMins; 
} 

// Get current seconds 
CString CBaseTime::GetCurrentSeconds() 
{ 
	CString retSecs; 
	seconds = _inp(0x71); 
	_outp(0x70,0x04); 
	retSecs.Format("%x",seconds); 
	return retSecs; 
}

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Technical Lead
India India
I am a personality from Trivandrum , a green peaceful place in Kerala, South India.
I have been an enthusiast of Windows programming when I started creating simple windows when I learnt VC++ in LCC in Trivandrum. From then on its all about Windows in my flesh and blood.
My career graph moves like this:
1. I started working as a C/C++ programmer in a company in Trivandrum where I learnt the ABCs of Software Industry. I worked with them for about 1 year. I could not contine since I was in contract for 1 year.
2. Then I joined another organization in Trivandrum who gave me challenges to love. They made me mad about VC++ and Windows.
I was mad about Embedded Systems which made me, Myself = Embedded Systems + VC++.
3. Software Engineer in a telecom company in Hyderabad, Andhra Pradesh, S.India.
4. Currently working with a telecom company in Bangalore.
I totally, so, have experience of about 4.5 years.

Comments and Discussions

 
GeneralMy vote of 5 Pin
simonchen.net12-Jan-11 2:52
simonchen.net12-Jan-11 2:52 
GeneralMy vote of 5 Pin
atishax5-Oct-10 15:06
atishax5-Oct-10 15:06 
GeneralArticle suggestion Pin
Jörgen Sigvardsson6-Oct-04 12:04
Jörgen Sigvardsson6-Oct-04 12:04 
GeneralKnowing how it really works. Pin
Randor 6-Oct-04 9:22
professional Randor 6-Oct-04 9:22 
Some people... (like myself) are never happy using the high level wrappers and want to access the information directly.

A few years ago it took me hours going through BIOS and interrupt documentation to find the information you just posted. This would have been much easier if I had your source code back then.

Thanks for sharing the information.

-Randor




GeneralRe: Knowing how it really works. Pin
Neville Franks6-Oct-04 10:33
Neville Franks6-Oct-04 10:33 
GeneralRe: Knowing how it really works. Pin
Randor 6-Oct-04 11:22
professional Randor 6-Oct-04 11:22 
GeneralRe: Knowing how it really works. Pin
Sreekanth Muralidharan6-Oct-04 19:04
Sreekanth Muralidharan6-Oct-04 19:04 
GeneralRe: Knowing how it really works. Pin
Jörgen Sigvardsson6-Oct-04 22:06
Jörgen Sigvardsson6-Oct-04 22:06 
GeneralShould not work on any current version of windows. Pin
John M. Drescher6-Oct-04 6:09
John M. Drescher6-Oct-04 6:09 
GeneralRe: Should not work on any current version of windows. Pin
Rory Solley6-Oct-04 21:57
Rory Solley6-Oct-04 21:57 
GeneralRe: Should not work on any current version of windows. Pin
John M. Drescher7-Oct-04 4:51
John M. Drescher7-Oct-04 4:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.