Example Using _dos_findfirst() to Get the Time and Date

Last reviewed: July 17, 1997
Article 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:

  • The C Run-time (CRT), included with:

        - Microsoft C for MS-DOS, versions 5.0, 5.1, 6.0, 6.0a, and 6.0ax
        - Microsoft C for OS/2, versions 5.1, 6.0, and 6.0a
        - Microsoft C/C++ for MS-DOS, version 7.0
        - Microsoft Visual C++ for Windows, version 1.0
    

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


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.