Date & Time Management System using C
Introduction
It’s my first post here. 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=PST8PDTwhere 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 hrsMST : 7 hrsCST : 6 hrsEST : 5 hrsdaylight : A flag that is set to zero if daylight saving time is not in effect otherwise 1tzname[] : Contains the three-character daylight saving time descriptive string or a null stringfunction tzset() sets these values
Terminology & Data Types
TicksWhenever 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_tThe 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 tmThis 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
#include
return 0;}biostime() - Reads or set the BIOS timer (interrupt 0x1A)
Specification#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
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
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
ctime() - converts date and time to a string
Specification#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
References
C Encyclopedia, Compiler Help and various websites (credits to google)