1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Date & Time Management System using C

Discussion in 'C' started by dssr, Jul 5, 2008.

  1. dssr

    dssr New Member


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

    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.

    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>

    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 :

    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 :

    struct dosdate_t {
    	unsigned char hour;
    	unsigned char minute;
    	unsigned char second;
    	unsigned char hsecond;
    struct dostime_t{
    	unsigned char day;
    	unsigned char month;
    	unsigned int year;
    	unsigned char dayofweek;
    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

    char *asctime(const struct tm *pointer)
    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
    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));
    	return 0;
    biostime() - Reads or set the BIOS timer (interrupt 0x1A)

    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.
    int main(void)
    	long bios_time;
    	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....");
    	return 0;
    clock() - Determines processor time

    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.

    void main(void)
    	long int bios_start,bios_end;
    	float elap_bios,elap_clock;
    	clock_t clock_start,clock_end;
    	printf("\n_bios = %ld\t\tclcok = %ld",bios_start,clock_start);
    	printf("\n_bios = %ld\t\tclock = %ld",bios_end,clock_end);
    	elap_clock=(clock_end-clock_start)/(float) CLK_TCK;
    	printf("\nelap_bios = %f sec\nelap_clock = %f sec",elap_bios,elap_clock);
    _bios_timeofday() - Get/set the system timer clock ticks

    unsigned _bios_timeofday(unsigned service, long *ticks)
    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

    char *ctime(const time_t *timer);
    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.
    int main(void)
    	time_t t;
    	printf("Todays date and time is : %s",ctime(&t));
    	return 0;
    time() - get system time in time_t format

    time_t time(time_t *timer);
    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.
    See previous example


    C Encyclopedia, Compiler Help and various websites (credits to google)
    Last edited: Jul 13, 2008
  2. shabbir

    shabbir Administrator Staff Member

    Really nice information.
  3. dssr

    dssr New 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
  4. XXxxImmortalxxXX

    XXxxImmortalxxXX New Member

    Very VERy good information keep up the good work
  5. XXxxImmortalxxXX

    XXxxImmortalxxXX New Member

    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?
  6. GMail

    GMail New Member

  7. madlex

    madlex New Member

    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.
  8. DeepSeas

    DeepSeas New Member

    very interesting

    nice first post
  9. dssr

    dssr New Member

    thanks buddy, and about that multiply thing that's a slip of pen :D 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: Jul 13, 2008
  10. dssr

    dssr New Member

    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.
  11. XXxxImmortalxxXX

    XXxxImmortalxxXX New Member

    well i always thought 2 ways a serial key generator or a patch to put with in the program its self
  12. faizulhaque

    faizulhaque New Member

    Good Work
  13. Dean-123

    Dean-123 New Member

    i often find time is so rare and my spare time less!
    how to handle mess of tasks effectively?
    any suggestions?
  14. dssr

    dssr New Member

    are you sure you talking about any technical thing?
  15. shabbir

    shabbir Administrator Staff Member

    Does not look like
  16. dssr

    dssr New Member

    lol i know i was being (errrr..... bad thing for a forum) sracastic

Share This Page