home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / tctnt / dostime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  2.4 KB  |  66 lines

  1. /* DOSTIME.C: A Time Conversion Structure
  2.  
  3.         The following code demonstrates a interesting technique for
  4.         converting the DOS date/time stamp as provided by an 'ffblk'
  5.         structure to ASCIIZ. This code compiles with ANSI C or C++
  6.         compiler.  Read the comments carefully since they provide
  7.         insight into the method used.
  8. */
  9.  
  10. #include <dir.h>            // for ffblk and findfirst()
  11. #include <stdio.h>        // for sprintf()
  12.  
  13. struct ffblk xffblk;
  14.  
  15. #pragma warn -par   // we know that argc is nevre used!
  16. //*******************************************************************
  17. int main(int argc, char **argv) {
  18.     char times[9],
  19.              dates[9];
  20.  
  21.   /* Create a 'fdate' structure of bit fields and a 'd' pointer
  22.          instance of this structure that maps the bit fields over the
  23.      date member of the 'xffblk' instance of the 'ffblk' structure
  24.      provided by DOS.H. Kind'a like a post facto union. This permits
  25.          us to refer to the day, month and year bits of the DOS FCB
  26.      by name. */
  27.     typedef struct fdate {
  28.     unsigned day   :5;        // 5 bits to represent the day
  29.     unsigned month :4;        // 4 bits for the month
  30.     unsigned year  :7;        // 7 bits for the years since 1980
  31.   } fdate;
  32.  
  33.   // Make an instance that overlays 'xxfblk.ff_fdate'
  34.     fdate *d = (fdate*) &(xffblk.ff_fdate);
  35.  
  36.   /* In a C++ program this could be simplified into:
  37.      struct fdate {
  38.          unsigned day   :5;
  39.        unsigned month :4;
  40.          unsigned year  :7;
  41.      } *d = (fdate*) &(xffblk.ff_date);
  42.      by observing that structures are always types and the structures
  43.      tag (i.e., 'fdate') is available as soon as it is declared.
  44.   */
  45.  
  46.   // Ditto for the seconds, minutes and hour bits of the time member.
  47.   typedef struct ftime {
  48.         unsigned sec   :5;          // 5 bits for the seconds
  49.     unsigned min   :6;        // 6 bits for the minutes
  50.     unsigned hour  :5;        // 5 bits for the hours
  51.     } ftime;
  52.  
  53.     // make an instance that overlays 'xxfblk.ff_ftime'
  54.     ftime *t = (ftime*) &(xffblk.ff_ftime);
  55.  
  56.   /* Load 'xffblk' with info about this program (executing program is
  57.      always contained in first argv array ASCIIZ string). */
  58.     findfirst(*argv, &xffblk, 0xFF);
  59.  
  60.   // Format time and date using named bit fields & print.
  61.     sprintf(times, "%02u:%02u:%02u", t->hour, t->min, t->sec);
  62.   sprintf(dates, "%02u-%02u-%02u", d->month, d->day, d->year+80);
  63.   printf("%s created:  %s %s\n", *argv, dates, times);
  64.     return 0;
  65. } // end of main()
  66.