Date & Time Management System using C

dssr's Avatar author of Date & Time Management System using C
This is an article on Date & Time Management System using C in C.
Rated 4.67 By 3 users

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=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 <time.h> and one data structure, tm.


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.h>


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 <dos.h>, the timeb data structure in <sys/timeb.h>.

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<time.h>
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<stdio.h>
#include<time.h>
#include<string.h>
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<bios.h>
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<stdio.h>
#include<bios.h>
#include<time.h>
#include<conio.h>

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<time.h>
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<stdio.h>
#include<time.h>
#include<bios.h>
#include<stdlib.h>

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<bios.h>
unsigned _bios_timeofday(unsigned service, long *ticks)
Arguments
service : use either the _TIME_GETCLOCK or _TIME_SETCLOCK object like macros found in <bios.h>
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<time.h>
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<stdio.h>
#include<time.h>

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.h>
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)
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Really nice information.
dssr's Avatar, Join Date: Feb 2008
Go4Expert Member
thanks mod
Considering the size of the article I have not given details of every functions but if you ask for some specific function or all of them I can post them too.
i would love to answer any queries or in-depth analysis of some portion
XXxxImmortalxxXX's Avatar
Invasive contributor
Very VERy good information keep up the good work
XXxxImmortalxxXX's Avatar
Invasive contributor
i got a question something i just thought of when ever you download a game and they have time limits on it does the time limit go according to your time on the pc or is it a script inside the game countding down thats what im thinking but if its on ur pc then couldnt you manipulate your time so that your time limit wouldnt never expire? is it possible?
GMail's Avatar
Go4Expert Member
Informative
madlex's Avatar, Join Date: Jun 2008
Go4Expert Member
*
Quote:
and represents the elapsed process time in seconds multiplied by the object-like macro CLK_TCK or CLOCKS_PER_SEC which is set to 1000 for DOS in <time.h>
It is not multiplied, it is divided by..

Actually, for the old DOS enviroment the clock_t is used with the clock() function, and it is divided by the macro TICKS_PER_SEC, not CLOCKS_PER_SEC or CLK_TCK.

The form of TICKS_PER_SEC was:
#define TICKS_PER_SEC 18.2f

As you well said, this value represents the number of interupts that the timer emits per second.The clock() function is exactly the function that gets this number.

If you check it out, you'll see that this macro "works" only for Win 95 and over, nor for DOS.
*
Quote:
The 8253 timer oscillates at 1,193,180 Hz with a built-in divisor of 65,636
The "divisor" is 65536 (not 65636).It is not a coincidence that this number represents 2^16+1, the maximum number of values a WORD can store.
DeepSeas's Avatar, Join Date: Feb 2008
Go4Expert Member
nice first post
dssr's Avatar, Join Date: Feb 2008
Go4Expert Member
Quote:
Originally Posted by madlex
*

It is not multiplied, it is divided by..

Actually, for the old DOS enviroment the clock_t is used with the clock() function, and it is divided by the macro TICKS_PER_SEC, not CLOCKS_PER_SEC or CLK_TCK.

The form of TICKS_PER_SEC was:
#define TICKS_PER_SEC 18.2f

As you well said, this value represents the number of interupts that the timer emits per second.The clock() function is exactly the function that gets this number.

If you check it out, you'll see that this macro "works" only for Win 95 and over, nor for DOS.
*

The "divisor" is 65536 (not 65636).It is not a coincidence that this number represents 2^16+1, the maximum number of values a WORD can store.

thanks buddy, and about that multiply thing that's a slip of pen see the example of biostime() a similar macro has been used fro dividing the actual BIOS time
and about the buil;t in divisor that's also a typing mistake, you can imagine when you have to prepare and type the whole thing within a day

i regret these mistakes

Last edited by dssr; 13Jul2008 at 19:02..
dssr's Avatar, Join Date: Feb 2008
Go4Expert Member
Quote:
Originally Posted by XXxxImmortalxxXX
i got a question something i just thought of when ever you download a game and they have time limits on it does the time limit go according to your time on the pc or is it a script inside the game countding down thats what im thinking but if its on ur pc then couldnt you manipulate your time so that your time limit wouldnt never expire? is it possible?

thanxx buddy, and about your query - have you ever downloaded a cracked version of a software, how do you think they do it? i dont claim that i know all the methods of such reverse engineering but of course one of them is setting the timer afresh. Setting corresponding DWORD value afresh would do the trick. But easier said than done.