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++)

By , 10 Jul 2006
 

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

About the Author

Sreekanth Muralidharan
Technical Lead
India India
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberstarschen12 Jan '11 - 2:52 
GeneralMy vote of 5memberatishax5 Oct '10 - 15:06 
GeneralArticle suggestionmemberJörgen Sigvardsson6 Oct '04 - 12:04 
How about an article on the marvels of conio.h Roll eyes | :rolleyes:
 
--
Wir müssen leben bis wir sterben.
GeneralKnowing how it really works.memberRandor6 Oct '04 - 9:22 
GeneralRe: Knowing how it really works.memberNeville Franks6 Oct '04 - 10:33 
GeneralRe: Knowing how it really works.memberRandor6 Oct '04 - 11:22 
GeneralRe: Knowing how it really works.memberSreekanth Muralidharan6 Oct '04 - 19:04 
GeneralRe: Knowing how it really works.memberJörgen Sigvardsson6 Oct '04 - 22:06 
GeneralShould not work on any current version of windows.memberJohn M. Drescher6 Oct '04 - 6:09 
GeneralRe: Should not work on any current version of windows.memberRory Solley6 Oct '04 - 21:57 
GeneralRe: Should not work on any current version of windows.memberJohn M. Drescher7 Oct '04 - 4:51 

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

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