Saturday, August 23, 2008
Thursday, August 21, 2008
Saturday, August 2, 2008
Introduction
I will discuss on Date & Time Management Functions of C. Though if someone is only interested about getting the time and date, then it can be done by some trivial function but the goal of this detail discussion is understanding the bios as well as decreasing the abstraction. I had to sum up this data when I was working on an algorithm. Hopefully some of you will find this useful.
Here you will find explanations of each run-time library functions that supports the capture, conversion and display of time, from which all date and date measures are derived. The examples provided here demonstrate the proper use of these functions and detail the relationship that exist between the global variable daylight, timezone and tzname[], and environment string TZ.
Computer Time
All system date and time measures available under DOS are derived from either the 8253 timer (oscillator) chip, or a real-time (battery-supported) if one is installed and operating. The 8253 timer oscillates at 1,193,180 Hz with a built-in divisor of 65,536 and emits a BIOS interrupt 0x08 18.2 times per second, or about every 0.055 seconds.
Universal Date & Time
To maintain compatibility with UNIX, several DOS run-time library functions and data structures are available that permit the underlying DOS clock device driver measures of time to support universal (GMT) time as well as standard and daylight saving time.
The non-Standard DOS-specific global variables (timezone, daylight and tzname[]) and environment string (TZ=) are used to implement universal GMT based time and to provide a complement of UNIX-compatible functions with within the DOS. These global variables assume default values based on an assumed TZ=string setting of
TZ=PST8PDT
where
PST : A three character standard time zone abbreviation –
PST – Pacific Standard Time
MST – Mountain Standard Time
CST – Central Standard Time
EST – Eastern Standard Time
8 : The number of whole hours, measured from the prime meridian; a positive value of 0 to 12 if west longitude, and 0 to –12 if east longitude.
PDT : A three-character abbreviation used to designate dayligyt saving time is in effect for this time zone, it stands for Pacific Daylight Time it is also can be of 4 types PDT, MDT, CDT, EDT
timezone : the number of seconds difference between the local time zone and the GMT is as follows :
PST : 8 hrs
MST : 7 hrs
CST : 6 hrs
EST : 5 hrs
daylight : A flag that is set to zero if daylight saving time is not in effect otherwise 1
tzname[] : Contains the three-character daylight saving time descriptive string or a null string
function tzset() sets these values
Terminology & Data Types
Ticks
Whenever we speak of ticks, we are referring to the count of interrupts that are generated by the 8285 timer chip or real-time clock, and accumulated by the BIOS. For example if you have a tick count of 66,733, using the BIOS constant, the measure of time would be calculated as follows
1-hour : 66733-(1*((1,193,180/65,536)*60*60))=1190
Standard C defines two data-object types, clock_t and time_t, for representing data in
clock_t
The standard C clock_t type is defined as
typedef long clock_t;
and represents the elapsed process time in seconds divided by the object-like macro CLK_TCK or CLOCKS_PER_SEC which is set to 1000 for DOS in
time_t
It is defined as
typedef long time_t;
A time_t type represents the number of elapsed seconds since midnight relative to prime meridian (GMT) January 1, 1970, for compatibility with UNIX, whereas all DOS dates are originated at January 1, 1980.
struct tm
This is a special structure used in date time management functions. Template of this structure:
Code:
struct tm {
int tm_sec; //seconds after the minute
int tm_min; //minutes after the hour
int tm_hour; //hours since midnight
int tm_mday; //day of the month
int tm_mon; //months since January
int tm_year; //years since 1900
int tm_wday; //days since Sunday
int tm_yday; //days since January 1
int tm_isdst; //daylight saving time flag
};
Microsoft C defines the dosdate_t and dostime_t data structure in
The structure template is given by :
Code:
struct dosdate_t {
unsigned char hour;
unsigned char minute;
unsigned char second;
unsigned char hsecond;
};
Code:
struct dostime_t{
unsigned char day;
unsigned char month;
unsigned int year;
unsigned char dayofweek;
};
Code:
struct timeb {
time_t time; //GMT in seconds
unsigned short millitm; //millisecond fraction
short timezone; //GMT local time in minute
short dstflag; //=1 if DST in effect
};
Run-Time Library Functions For Date & Time Management
Here is a list of library functions that are used for Date and Time Management in C :
asctime() biostime() _bios_timeofday() ctime() difftime()
_dos_getdate() _dos_gettime() _dos_settime() _ dos_setdate()
dostounix() ftime() getdate() gettime() gmtime()localtime() mktime() stime() _strdate() _strtime()strftime() time() TDate(class) TTime(class) tzset() unixtodos()
Here are the example of few -
asctime() - convert the standard tm structure to a string
Spicification
#include
char *asctime(const struct tm *pointer)
Arguments
pointer : pointer to the Standard C date and time structure, tm
Return Value
A pointer to a static character string that is always 26 characters and in the format
Sun Jul 06 09:30:50 1980\n\0
Example
Code: C
#include
#include
#include
int main()
{
struct tm t;
char str[80];
/* Sample loading of tm structure */
t.tm_sec=1; //seconds
t.tm_min=30; //minutes
t.tm_hour=9; //hour
t.tm_mday=22; //day of the month
t.tm_mon=11; //month
t.tm_year=56; //year does not include century
t.tm_wday=4; //day of the week
t.tm_yday=0; //does not show in asctime
t.tm_isdst=0; //is Daylight SavTime, does not show in asctime
/* converts structure to null terminates string */
strcpy(str, asctime(&t));
printf("%s\n",str);
return 0;
}
biostime() - Reads or set the BIOS timer (interrupt 0x1A)
Specification
#include
long biostime (int cmd, long newtime);
Arguments & Return Value
If cmd equals 0, biostime returns the current value of the timer. If cmd=1, the timer is set to the long value in newtime.
Example
Code: c
#include
#include
#include
#include
int main(void)
{
long bios_time;
clrscr();
printf("The number of clock ticks since midnight : \n");
printf("The number of seconds since midnight : \n");
printf("The number of minutes since midnight : \n");
printf("The number of hours since midnight : \n");
printf("Press any key to stop....");
while(!kbhit())
{
bios_time=biostime(0,0L);
gotoxy(50,1);
printf("%lu",bios_time);
gotoxy(50,2);
printf("%.4f",bios_time/_BIOS_CLK_TCK);
gotoxy(50,3);
printf("%.4f",bios_time/_BIOS_CLK_TCK/60);
gotoxy(50,4);
printf("%.4f",bios_time/_BIOS_CLK_TCK/3600);
}
return 0;
}
clock() - Determines processor time
Specification
#include
clock_t clock(void);
Return Value
Clock can be used to determine the time interval between two events. On success clock returns the processor time elapsed since the beginning of the program invocation. On error clock returns –1.
Example
Code: c
#include
#include
#include
#include
void main(void)
{
long int bios_start,bios_end;
float elap_bios,elap_clock;
clock_t clock_start,clock_end;
_bios_timeofday(_TIME_GETCLOCK,&bios_start);
clock_start=clock();
printf("\n_bios = %ld\t\tclcok = %ld",bios_start,clock_start);
clock_end=clock();
_bios_timeofday(_TIME_GETCLOCK,&bios_end);
printf("\n_bios = %ld\t\tclock = %ld",bios_end,clock_end);
elap_bios=(bios_end-bios_start)/18.2F;
elap_clock=(clock_end-clock_start)/(float) CLK_TCK;
printf("\nelap_bios = %f sec\nelap_clock = %f sec",elap_bios,elap_clock);
exit(0);
}
_bios_timeofday() - Get/set the system timer clock ticks
Specification
#include
unsigned _bios_timeofday(unsigned service, long *ticks)
Arguments
service : use either the _TIME_GETCLOCK or _TIME_SETCLOCK object like macros found in
ticks : pointer to a long that is used either to return the clock tick count or to serve as the value to set the clock tick count.
Return Value
When the _TIME_GETCLOCK service is used, a value of 0 is returned if midnight has been passed since the last get or set time was performed; otherwise a value pf 1 is returned.
When _TIME_SETCLOCK service is used, there is no return value.
Example See previous example
ctime() - converts date and time to a string
Specification
#include
char *ctime(const time_t *timer);
Arguments
timer : pointer to a time value in time_t format that was acquired using function time().
Return Value
ctime returns a pointer to the character string containing the date and time.
Example
Code: c
#include
#include
int main(void)
{
time_t t;
time(&t);
printf("Todays date and time is : %s",ctime(&t));
return 0;
}
time() - get system time in time_t format
Specification
#include
time_t time(time_t *timer);
Arguments
timer : pointer to a time_t type variable that is assigned the number of seconds elapsed since 00:00:00 GMT January 1, 1970.If timer is null no value is stored.
Return Value
The elapsed time in seconds identical to that placed in timer.
Example
See previous example
References
C Encyclopedia, Compiler Help and various websites (credits to google)