[comp.lang.c] Turbo C Date Conversion

rcr@picuxa.UUCP (Richard Court ) (01/21/88)

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!

Any help would be greatly appreciated!

wew@naucse.UUCP (Bill Wilson) (01/22/88)

In article <454@picuxa.UUCP>, rcr@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!

Norton's "Programmer's Guide to the IBM PC" may be a great help
to you when working on PC systems type programs.  To decode the
date try the following algorithm:
  d=dosdate and 31
  m=(dosdate shr 5) and 15
  y=(dosdate shr 9) + 1980

For the time:
  min=(dostime(shr 5) and 63
  hour=(dostime shr 11)

You'll have to transalte this psuedo code into C.

Bill Wilson

asjoshi@phoenix.Princeton.EDU (Amit S. Joshi) (01/23/88)

 I would love to know too !!

Thanks

-- 
Amit Joshi	BITNET	|	Q3696@PUCC.BITNET
		USENET	| {seismo, rutgers}\!princeton\!phoenix\!asjoshi
"There's a pleasure in being mad... which none but madmen know!" - St.Dryden

swarbric@tramp.Colorado.EDU (SWARBRICK FRANCIS JOHN) (01/26/88)

In article <454@picuxa.UUCP> rcr@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!

Get a good DOS programming book such as "Advanced MS-DOS."  I would tell
you exactly how, except I am at school and the book is at home.  It's a
very good book to have, anyway.

Frank Swarbrick (and his cat)
swarbric@tramp.UUCP               swarbric@tramp.Colorado.EDU
...!{hao|nbires}!boulder!tramp!swarbric
    "No one can hear when you're Screaming in Digital!"

brianc@cognos.uucp (Brian Campbell) (02/08/88)

In article <454@picuxa.UUCP> rcr@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@1:163/8