Turbo C Date Conversion

Brian Campbell brianc at cognos.uucp
Tue Feb 9 03:08:11 AEST 1988


In article <454 at picuxa.UUCP> rcr at picuxa.UUCP (Richard Court ) writes:
> I am writing a program to list directory attributes.  I am using the
> "findfirst" and "findnext" routines to extract file information for me
> and then "strcpy" data out of the "ffblk" structure to fill my arrays.
> 
> What I can't figure out is how to convert the file date and time.  They
> are placed in the ffblk structure as packed integers and I'll be damned
> if I can figure out a way to get them into strings!

The following program will list the contents of the current directory,
formatting the date as yy-mm-dd, and the time as hh:mm:ss.  It should
make DOS' date and time packing obvious.

P.S.  The structs defined work under Turbo C 1.5, under other
compilers (and/or versions of Turbo C) it may be necessary to reverse
the order of the fields.

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

struct dosdate {
    unsigned day : 5;
    unsigned month : 4;
    unsigned year : 7;
};

struct dostime {
    unsigned bisecond : 5;
    unsigned minute : 6;
    unsigned hour : 5;
};

main()
{
    struct ffblk ff;

    if (findfirst("*.*", &ff, 0) != 0) {
	printf("No files found\n");
	exit(1);
    }
    do {
	printf("%14s %2d-%02d-%02d %2d:%02d:%02d\n", ff.ff_name,
	    ((struct dosdate *) &ff.ff_fdate)->year + 80,
	    ((struct dosdate *) &ff.ff_fdate)->month,
	    ((struct dosdate *) &ff.ff_fdate)->day,
	    ((struct dostime *) &ff.ff_ftime)->hour,
	    ((struct dostime *) &ff.ff_ftime)->minute,
	    ((struct dostime *) &ff.ff_ftime)->bisecond * 2);
    } while (findnext(&ff) == 0);
}
-- 
Brian Campbell        uucp: decvax!utzoo!dciem!nrcaer!cognos!brianc
Cognos Incorporated   mail: POB 9707, 3755 Riverside Drive, Ottawa, K1G 3Z4
(613) 738-1440        fido: (613) 731-2945 300/1200, sysop at 1:163/8



More information about the Comp.lang.c mailing list