2 * fat-handler - FAT12/16/32 filesystem handler
4 * Copyright © 2006 Marek Szyprowski
5 * Copyright © 2007-2015 The AROS Development Team
7 * This program is free software; you can redistribute it and/or modify it
8 * under the same terms as AROS itself.
13 #include <proto/utility.h>
16 #include "fat_protos.h"
18 #define DEBUG DEBUG_MISC
21 void ConvertFATDate(UWORD date
, UWORD time
, struct DateStamp
*ds
,
24 ULONG year
, month
, day
, hours
, mins
, secs
;
25 struct ClockData clock_data
;
27 /* Date bits: yyyy yyym mmmd dddd */
28 year
= (date
& 0xfe00) >> 9; /* Bits 15-9 */
29 month
= (date
& 0x01e0) >> 5; /* bits 8-5 */
30 day
= date
& 0x001f; /* Bits 4-0 */
32 /* Time bits: hhhh hmmm mmms ssss */
33 hours
= (time
& 0xf800) >> 11; /* Bits 15-11 */
34 mins
= (time
& 0x07e0) >> 5; /* Bits 10-5 */
35 secs
= time
& 0x001f; /* Bits 4-0 */
37 D(bug("[fat] converting fat date: year %d month %d day %d hours %d"
38 " mins %d secs %d\n", year
, month
, day
, hours
, mins
, secs
));
40 if (month
< 1 || month
> 12 || day
< 1 || day
> 31 || hours
> 23 ||
41 mins
> 59 || secs
> 29)
43 D(bug("[fat] invalid fat date: using 01-01-1978 instead\n"));
48 clock_data
.year
= 1980 + year
;
49 clock_data
.month
= month
;
50 clock_data
.mday
= day
;
51 clock_data
.hour
= hours
;
52 clock_data
.min
= mins
;
53 clock_data
.sec
= secs
<< 1;
54 secs
= Date2Amiga(&clock_data
);
57 /* Calculate days since 1978-01-01 (DOS epoch) */
58 ds
->ds_Days
= secs
/ (60 * 60 * 24);
60 /* Minutes since midnight */
61 ds
->ds_Minute
= secs
/ 60 % (24 * 60);
63 /* 1/50 sec ticks since last minute */
64 ds
->ds_Tick
= secs
% 60 * TICKS_PER_SECOND
;
66 D(bug("[fat] converted fat date: days %ld minutes %ld ticks %ld\n",
67 ds
->ds_Days
, ds
->ds_Minute
, ds
->ds_Tick
));
70 void ConvertDOSDate(struct DateStamp
*ds
, UWORD
* date
, UWORD
* time
,
73 ULONG year
, month
, day
, hours
, mins
, secs
;
74 struct ClockData clock_data
;
76 /* Convert datestamp to seconds since 1978 */
77 secs
= ds
->ds_Days
* 60 * 60 * 24 + ds
->ds_Minute
* 60
78 + ds
->ds_Tick
/ TICKS_PER_SECOND
;
80 /* Round up to next even second because of FAT's two-second granularity */
81 secs
= (secs
& ~1) + 2;
83 /* Convert seconds since 1978 to calendar/time data */
84 Amiga2Date(secs
, &clock_data
);
86 /* Get values used in FAT dates */
87 year
= clock_data
.year
- 1980;
88 month
= clock_data
.month
- 0;
89 day
= clock_data
.mday
;
90 hours
= clock_data
.hour
;
91 mins
= clock_data
.min
;
92 secs
= clock_data
.sec
>> 1;
94 /* All that remains is to bit-encode the whole lot */
96 /* Date bits: yyyy yyym mmmd dddd */
97 *date
= (((ULONG
) year
) << 9) | (((ULONG
) month
) << 5) | day
;
99 /* Time bits: hhhh hmmm mmms ssss */
100 *time
= (((ULONG
) hours
) << 11) | (((ULONG
) mins
) << 5) | secs
;