Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
11 Jul 2006 1  
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.

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

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