26)
Telling the time
Program 24 | Telling the time | Source code - prog24.c |
All PC's have there own internal clock which
is powered by a small battery on the motherboard, this is
so that even when the power is switched off it can still
keep updating the time. That is how windows knows the
date and time ( probably in the bottom right of your
screen ) even after the power has been turned off. 'C' has a few functions for dealing with time and dates in a program. The sample program below, simply displays the date and time, and also the number of seconds that have elapsed since the 1st of January 1980. Why the 1st of Jan 1980 ?, well because I think the first PC was produced in 1982, and 1980 is a nice round year, so I think that was what must have been decided upon. It would have been pointless measuring time from 1970 or earlier, because there were no PC's about then. |
Program code - most new |
|
Description of the program code |
The first thing to
note is the inclusion of the #include<time.h>
this is because the program is using some functions and
structures that have there prototypes defined in the
time.h header file. Two variables are declared, firstly one called secs, which is a special defined data type called time_t ( it's really just an unsigned long I think ). This variable will be used to hold the number of seconds that have passed since the first of January 1980, more later. The second variable is a pointer variable to a structure called tm. The tm structure is defined in the time.h header file and is shown below, with a brief description following :
sec holds the current number of seconds in the minute (0-59), min holds the number of minutes in the hour(0-59), hour holds the number of hours in the day(0-23), mday holds the current day of the month (1-31), mon holds the current month ( 0-11 - 0 = Jan, 1 = Feb etc.. ). year holds the number of years after 1900, so for the year 2002 this will be ( 2002 - 1900 ) = 102. wday holds the day of the week ( 0 = Sun, 1 = Mon, 2 = Tue etc.. ). yday holds the current day of the year ( 0 - 365 ). isdst is all to do with daylight saving time. It isn't really important in this program above to know the layout of the tm structure, but it's interesting nonetheless. Anyway the variable called t will hold a pointer to a structure with this format. The line secs=time(NULL); makes use of the 'C' time function, which will return the number of seconds that have elapsed since Jan 1st 1980. It takes a parameter which you need to set to NULL. So at this point in the program, the secs variable holds the number of seconds that have passed since Jan 1st 1980. You could theoretically, I suppose, write a bit of snazzy code following this to work out what date and time it currently is, but it would be a lot of hard work and effort wasted because 'C' has a function specifically for doing just that, and probably more accurately. If you was to do it yourself, you'd have to take into account all sorts of horrible things like leap years etc. :( The function is the localtime function, which converts a linear time ( ie seconds passed since 1980 ) and returns a pointer to a tm structure ( shown above ). The localtime function requires a parameter, which is the address of a variable containing the number of seconds to be converted. When a pointer to a tm structure has been retrieved, it can be used as a parameter to the asctime function, which takes a pointer to a tm structure as a parameter and returns back a pointer to a string, which contains a neatly formatted date and time string. |
Summary |
Wow,
"pointered out" ! The program above showed how
the real time could be retrieved from a function and
formatted so that it can be displayed. It isn't necessary
to understand the workings of it, or even the code fully,
it's a handy little program to copy and paste from when
and if you need it. It's now time to say bye to the Miracle C compiler :(. Although it's definately not the most advanced compiler in the world, as a learning compiler it has served a brilliant purpose in getting this far into C programming. |
Tasks |
24.1) Alter the program code above to enclose it in a while loop that terminates when either the uppercase or lowercase 'x' key is pressed. The program should loop continuously updating the display and time on the screen ( don't worry about the flashing cursors though for now ). |
(c) J.C.Spooner 2001