- I've yet to encounter a programmable device that
didn't have some sort of internal clock.
Computer has a clock and thanks to the Internet
modern computers keep accurate track of the time.
When your code needs to access that information
you dip into the C language library's
assortment of time functions.
In this movie you'll see how time
functions work in the C language.
You'll learn how to check the time, read time values,
and how to gather information about the current date.
This code uses the direct value
generated by the time function, as shown by the
second argument in the printf function at line six.
You'll see that the placeholder is ld,
which is a long integer value or a very large integer.
That's the type of value returned from the time function.
The time function itself requires an argument.
Here I'm using the NULL pointer
constant to keep the function happy.
Also keeping the compiler happy is the inclusion
of the time header file at line two.
Build and run this code.
So this value is the current time.
It's actually what's referred to as the Unix Epoch time
or the number of seconds that have
elapsed since January 1st, 1970.
To be relevant to a human being the number
is going to need some work.
Oh and by the way, this value has been used in
previous movies as the seed number for the srand function.
It's the same deal, in fact if you run the program
again you'll see a different value generated.
Normally the value returned from the time function is saved.
It's a long integer value, but the variable type
is known as time_t, which is spelled time underscore t.
Insert a new line in the code to declare
a variable now as a time_t type.
To assign a value to the now variable you must
specify it as an argument in the time function.
Yes that's backwards, but several functions
operate that way, including scanf.
And like scanf you must prefix
the variable with an ampersand.
And change the printf statement so that the variable
now appears as the second argument.
Save these changes, build and run.
And the time is still shown as a big number,
but it also shows that you have
time left to complete this movie, so moving along.
The C library comes with a function that
translates time_t values into strings.
It's called the ctime function.
Edit the printf function in this code so that the
second argument in this function is now ctime(&now).
Change the ld placeholder to %s.
That's because the ctime function generates a string.
Save, build and run.
What you see is the current date and time,
and unless you're doing this movie
right now you'll see a different time.
If you want to fetch specific time values,
then you need to use the localtime function.
This function returns the address of a structure
that holds individual time values, such as the day,
month, week, hour, day of the year, and other information.
Open exercise file time2.
The localtime function at line 10 fills a structure
with interesting info about the current time.
The structure is declared at line seven.
It's a pointer variable, which you'll
learn about in a later movie.
The local function uses the current
time value obtained at line nine.
This fills the structure right_now,
which is the name of the structure.
Some of that information available in the structure
is displayed in the printf statement starting at line 11.
I've split up the arguments on separate
lines to make them more readable,
but all this stuff is really a single statement.
The C language doesn't let any
extra spaces in here bother it.
Four values are accessed from the right_now structure,
the month, the day of the month, the hour, and minutes.
These are all members of the right_now structure.
You see that the period structure
member operator isn't used here.
This is the member operator used
for structure pointer variables.
Build and run the code.
Now you may notice that the month value is off.
That's because C starts counting at zero
and the first month of the year, January, is month zero.
You can fix this code by adding one to the
value returned from the right_now structure.
Other adjustments can be made as well.
For example, if the current time is less than 10
minutes after the hour, then it appears as a single digit.
Fix that by adding 02 to the %d placeholder.
This argument increases the output width of the integer
to two places, plus it'll prefix a zero
when the output is only one digit long.
Save this change, build and run.
Of course the current time doesn't show that
because it's 18 minutes after the hour,
but if it was eight the time would show up as 11:08
and the month is displayed correctly.