Sunday 13 November 2011

How can I store time as a single integer? Are there any standards for this? in C programming

How can I store time as a single integer? Are there any standards for this?

The question of storing a time in a single byte is similar to that of storing a date in a single byte; therefore, it will be helpful for you to have read FAQ XIII.1. Nonetheless, there are differences.

The first thing you should note is that a time of day is more “deterministic” than a date of a year. You know exactly how many seconds there will be in a minute, how many minutes there will be in an hour, and how many hours there will be in a day. This uniformity makes handling times of day somewhat easier and less prone to error.

Following is a list of features that are desirable when you are choosing a method to convert a time to a number:
 It should be as efficient on space as possible.
It should be able to store different kinds of time (standard, military).
It should enable you to advance through time quickly and efficiently.

If you’ve read the preceding FAQ about dates, you might decide that a good way to handle the problem is to represent time as a bit field. That is not a bad idea, and it has several salient advantages. Look at the code for representing time as an integer:

/*
* The bit field structures for representing time
*/
typedef struct
{
unsigned int Hour : 5;
unsigned int Minute : 6;
} TimeType;
typedef union
{
TimeType time;
int Number;
} TimeConverter;
/*
* Convert time to a number, returning zero when the values are
* out of range.
*/
int TimeToNumber( int Hour , int Minute )
{
TimeConverter convert;
if ( Hour < 1 || Hour > 24 || Minute < 1 || Minute > 60 )
return 0;
convert.time.Hour = Hour;
convert.time.Minute = Minute;
return convert.Number +1 ;
}
/*
* Convert a number back into the two time
* elements that compose it.
*/
void NumberToTime( int Number , int *Hour , int *Minute )
{
TimeConverter convert;
convert.Number = Number - 1;
*Hour = convert.time.Hour;
*Minute = convert.time.Minute;
}
/*
* A main routine that tests everything to
* ensure its proper functioning.
*/
main()
{
int Number , Hour , Minute;
Hour = 13;
Minute = 13;
Number = TimeToNumber( Hour , Minute );
NumberToTime( Number , &Hour , &Minute );
printf( “The time after conversion is %d:%d.\n” , Hour , Minute );
}

Adding seconds to the time class is relatively easy. You would need to add a seconds field to the time structure  and add one extra parameter to each of the conversion functions.

Suppose, however, that you want to use this resulting number as a clock, to “tick” through the day. To carry out this task using a bit field, you would have to convert the number into a bit field, increment the seconds,and test whether the seconds value had passed 60; if it had, you would have to increment the minutes, again testing to see whether the value had passed 60, and so on. This process could be tedious!

The problem here is that the elements of the time structure do not fit evenly into bits—they are not divisible by two. It is therefore more desirable to represent time mathematically. This can be done quite simply by representing a time of day by how many seconds (or minutes) have elapsed since the start of the day. If you represent the time in this fashion, incrementing the number will move the time to the next second (or minute). Take a look at some code that represents time in this way:

#include <stdio.h>
#include <stdlib.h>
/*
* A subroutine to convert hours and minutes into an
* integer number. This does no checking for the sake
* of brevity (you’ve seen it done before!).
*/
int TimeToNumber( int Hours , int Minutes )
{
return Minutes + Hours * 60;
}
/*
* Convert an integer to hours and minutes.
*/
void NumberToTime( int Number , int *Hours , int *Minutes)
{
*Minutes = Number % 60;
*Hours = Number / 60;
}
/*
* A quickie way to show time.
*/
void ShowTime( int Number )
{
int Hours , Minutes;
NumberToTime( Number , &Hours , &Minutes );
printf( “ %02d:%02d\n” , Hours , Minutes );
}
/*
* A main loop to test the salient features of the time class.
*/
main()
{
int Number , a;
Number = TimeToNumber( 9 , 32 );
printf( “Time starts at : %d “ , Number );
ShowTime( Number );
/*
* Assure yourself that minutes are added correctly.
*/
for( a = 0 ; a < 10 ; ++ a )
{
printf( “After 32 minutes : “ );
Number += 32; /* Add 32 minutes to the time. */
ShowTime( Number );
}
}

This code provides a better representation of time. It is easy to manipulate and more compact, and it even allows for shorter code. Adding seconds is an exercise left to the reader.

This format is much like that used by the C functions timelocal() and timegm(). These functions count
seconds from some arbitrary time/date. A slight modification of the routines presented here for both time  and date should enable the programmer to utilize these functions or even his own definition of time.

Cross Reference:

XIII.1: How can I store a date in a single number? Are there any standards for this?
XIII.5: What is the best way to store the time?

No comments:

Post a Comment