C Language and Libraries Help (clang.hlp) (Table of Contents; Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
SETTIME.C
                                             Up Contents Index Back
─────Run-Time Library───────────────────────────────────────────────────────
 
/* SETTIME.C illustrates getting and setting the DOS time and date using:
 *      _dos_gettime    _dos_settime    _dos_getdate    _dos_setdate
 *                                                                (DOS-only)
 * Formatted input to a string is illustrated using:
 *      sscanf
 *
 * See TABLE.C (fscanf) and SCANF.C for other examples of formatted input.
 */
 
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
 
struct _dosdate_t ddate;
struct _dostime_t dtime;
 
void main()
{
    unsigned tmpday, tmpmonth, tmpyear;
    unsigned tmphour, tmpminute, tmpsecond;
    char *days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    char tmpbuf[20];
 
    /* Get current date. */
    _dos_getdate( &ddate );
    printf( "Date: %u/%02u/%02u %s\n",
            ddate.month, ddate.day, ddate.year - 1900,
            days[ddate.dayofweek] );
    printf( "Enter new date: " );
 
    /* Get a date string and if it's not 0 (for CR), assign it to date. */
    gets( tmpbuf );
    if( strlen( tmpbuf ) )
    {
        /* NOTE: You must read the month and day into a temporary unsigned
         * variable. If you try to read directly into the unsigned char
         * values ddate.day or ddate.month, sscanf (or scanf) will read a
         * word, thus overriding adjacent bytes.
         */
        sscanf( tmpbuf, "%u/%u/%u", &tmpmonth, &tmpday, &tmpyear );
        if( (tmpyear - 80) <= 20 )
            ddate.year = tmpyear + 1900;
        else if( (tmpyear >= 1980) && (tmpyear <= 2099 ) )
            ddate.year = tmpyear;
        if( (tmpmonth + 1) <= 13 )
            ddate.month = (unsigned char)tmpmonth;
        if( (tmpday + 1) <= 32 )
            ddate.day = (unsigned char)tmpday;
        _dos_setdate( &ddate );
        _dos_getdate( &ddate );
        printf( "New date: %u/%02u/%02u %s\n",
                ddate.month, ddate.day, ddate.year - 1900,
                days[ddate.dayofweek] );
    }
 
    /* Get current time. */
    _dos_gettime( &dtime );
    printf( "Time: %u:%02u:%02u\n", dtime.hour, dtime.minute, dtime.second );
    printf( "Enter new time: " );
 
    /* Get a time string and if it's not 0 (for CR), assign it to time. */
    gets( tmpbuf );
    if( strlen( tmpbuf ) )
    {
        sscanf( tmpbuf, "%u:%u:%u", &tmphour, &tmpminute, &tmpsecond );
        if( tmphour < 24 )
            dtime.hour = (unsigned char)tmphour;
        if( tmpminute < 60 )
            dtime.minute = (unsigned char)tmpminute;
        if( tmpsecond < 60 )
            dtime.second = (unsigned char)tmpsecond;
        _dos_settime( &dtime );
        _dos_gettime( &dtime );
        printf( "New time: %u:%02u:%02u\n",
                dtime.hour, dtime.minute, dtime.second );
    }
}
                                    -♦-