Example Using _dos_findfirst() to Get the Time and Date

ID: Q39916

5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a | 1.00

MS-DOS                      | OS/2            | WINDOWS
kbprg kbcode

The information in this article applies to:

SUMMARY

The code below illustrates how to extract the time and the date out of the C run-time function _dos_findfirst(). The program prints out the time and the date of the creation of the file "test".

This information also applies to QuickC.

MORE INFORMATION

The time at which the file was last written to is returned as a binary value in a word formatted as follows:

   Bits     Meaning
   ----     -------

   0-4      Number of seconds DIVIDED BY TWO
               (to find actual number of seconds, multiply by two)
   5-10     Minutes
   11-15    Hours, based on a 24-hour clock

The date at which the file was last written to is returned as a binary value in a word formatted as follows:

   Bits     Meaning
   ----     -------

   0-4      Day of the month
   5-8      Month (1 = January and so on)
   9-15     This number plus 1980 give current year

Sample Code

/* Compile options needed: none
*/ 

#include <dos.h>
#include <stdio.h>
#include <time.h>

struct low_unit {
  unsigned biseconds: 5;  /* in units of TWO seconds */ 
  unsigned minutes: 6;
  unsigned hours:   5;
} *ptime;

struct hi_unit {

  unsigned day:   5;
  unsigned month: 4;
  unsigned year:  7;
} *pdate;

struct find_t c_file;

void main(void)
{
   _dos_findfirst ("test", _A_NORMAL, &c_file);

   system("cls");

   ptime = (struct low_unit *) &c_file.wr_time;
   pdate = (struct hi_unit *) &c_file.wr_date;

   printf ("Created at %u:%u:%u",\ 
            ptime->hours, ptime->minutes, ptime->biseconds * 2);
                /* NOTE: seconds are divided by two when stored,
                         so we have to multiply by two to get
                         the proper value.... */ 

   printf (" on %u-%u-%u.",\ 
            pdate->month, pdate->day, pdate->year + 80);
}

The program might produce the following output (depending on when the file "test" was created):

   Created at 10:32:28 on 12-19-89

Note that the seconds field of the time will always be even.

For more information on function _dos_findfirst(), see the Microsoft C Run-Time Library Reference."

Additional reference words: kbinf 5.00 5.10 6.00 6.00a 6.00ax 7.00 1.00 KBCategory: kbprg kbcode KBSubcategory: CRTIss Keywords : kb16bitonly

Last Reviewed: July 18, 1997