home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / learn / settime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-02  |  2.6 KB  |  77 lines

  1. /* SETTIME.C illustates getting and setting the DOS time and date using:
  2.  *      _dos_gettime    _dos_settime    _dos_getdate    _dos_setdate
  3.  *
  4.  * Formatted input to a string is illustrated using:
  5.  *      sscanf
  6.  */
  7.  
  8. #include <dos.h>
  9. #include <stdio.h>
  10. #include <conio.h>
  11. #include <string.h>
  12.  
  13. struct dosdate_t ddate;
  14. struct dostime_t dtime;
  15.  
  16. main()
  17. {
  18.     unsigned tmpday, tmpmonth, tmpyear;
  19.     unsigned tmphour, tmpminute, tmpsecond;
  20.     static char *days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  21.     char tmpbuf[20];
  22.  
  23.     /* Get current date. */
  24.     _dos_getdate( &ddate );
  25.     printf( "Date: %u/%02u/%02u %s\n",
  26.             ddate.month, ddate.day, ddate.year - 1900,
  27.             days[ddate.dayofweek] );
  28.     printf( "Enter new date: " );
  29.  
  30.     /* Get a date string and if it's not 0 (for CR), assign it to date. */
  31.     gets( tmpbuf );
  32.     if( strlen( tmpbuf ) )
  33.     {
  34.         /* NOTE: You must read the month and day into a temporary unsigned
  35.          * variable. If you try to read directly into the unsigned char
  36.          * values ddate.day or ddate.month, sscanf (or scanf) will read a
  37.          * word, thus overriding adjacent bytes.
  38.          */
  39.         sscanf( tmpbuf, "%u/%u/%u", &tmpmonth, &tmpday, &tmpyear );
  40.         if( (tmpyear - 80) <= 20 )
  41.             ddate.year = tmpyear + 1900;
  42.         else if( (tmpyear >= 1980) && (tmpyear <= 2099 ) )
  43.             ddate.year = tmpyear;
  44.         if( (tmpmonth + 1) <= 13 )
  45.             ddate.month = (unsigned char)tmpmonth;
  46.         if( (tmpday + 1) <= 32 )
  47.             ddate.day = (unsigned char)tmpday;
  48.         _dos_setdate( &ddate );
  49.         _dos_getdate( &ddate );
  50.         printf( "New date: %u/%02u/%02u %s\n",
  51.                 ddate.month, ddate.day, ddate.year - 1900,
  52.                 days[ddate.dayofweek] );
  53.     }
  54.  
  55.     /* Get current time. */
  56.     _dos_gettime( &dtime );
  57.     printf( "Time: %u:%02u:%02u\n", dtime.hour, dtime.minute, dtime.second );
  58.     printf( "Enter new time: " );
  59.  
  60.     /* Get a time string and if it's not 0 (for CR), assign it to time. */
  61.     gets( tmpbuf );
  62.     if( strlen( tmpbuf ) )
  63.     {
  64.         sscanf( tmpbuf, "%u:%u:%u", &tmphour, &tmpminute, &tmpsecond );
  65.         if( tmphour < 24 )
  66.             dtime.hour = (unsigned char)tmphour;
  67.         if( tmpminute < 60 )
  68.             dtime.minute = (unsigned char)tmpminute;
  69.         if( tmpsecond < 60 )
  70.             dtime.second = (unsigned char)tmpsecond;
  71.         _dos_settime( &dtime );
  72.         _dos_gettime( &dtime );
  73.         printf( "New time: %u:%02u:%02u\n",
  74.                 dtime.hour, dtime.minute, dtime.second );
  75.     }
  76. }
  77.