Click here to Skip to main content
Click here to Skip to main content

Get hold of system time without using inbuilt functions (Can be used with Turbo C++)

, 11 Jul 2006
Rate this:
Please Sign up or sign in to vote.
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 read 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 manipulations revoloves 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

Share

About the Author

Sreekanth Muralidharan
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 Pinmemberstarschen12-Jan-11 3:52 
I knew this code should be working with ring0, however, it's a SIMPLE guide.
GeneralMy vote of 5 Pinmemberatishax5-Oct-10 16:06 
GeneralArticle suggestion PinmemberJörgen Sigvardsson6-Oct-04 13:04 
GeneralKnowing how it really works. PinmemberRandor6-Oct-04 10:22 
GeneralRe: Knowing how it really works. PinmemberNeville Franks6-Oct-04 11:33 
GeneralRe: Knowing how it really works. PinmemberRandor6-Oct-04 12:22 
GeneralRe: Knowing how it really works. PinmemberSreekanth Muralidharan6-Oct-04 20:04 
GeneralRe: Knowing how it really works. PinmemberJörgen Sigvardsson6-Oct-04 23:06 
GeneralShould not work on any current version of windows. PinmemberJohn M. Drescher6-Oct-04 7:09 
GeneralRe: Should not work on any current version of windows. PinmemberRory Solley6-Oct-04 22:57 
GeneralRe: Should not work on any current version of windows. PinmemberJohn M. Drescher7-Oct-04 5:51 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 11 Jul 2006
Article Copyright 2004 by Sreekanth Muralidharan
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid