* reordered a little bit
[mascara-docs.git] / i86 / elks / elkscmd / mtools / convdate.c
blob6f97cadbf9b963b993ed6e929f9f66dde1094f76
1 /*
2 * convdate(), convtime(), convstamp()
3 */
4 #include <stdio.h>
5 /*
6 * convert MSDOS directory datestamp to ASCII.
7 */
9 char *
10 convdate(date_high, date_low)
11 unsigned date_high, date_low;
14 * hi byte | low byte
15 * |7|6|5|4|3|2|1|0|7|6|5|4|3|2|1|0|
16 * | | | | | | | | | | | | | | | | |
17 * \ 7 bits /\4 bits/\ 5 bits /
18 * year +80 month day
20 static char ans[9];
21 unsigned char year, month_hi, month_low, day;
23 year = (date_high >> 1) + 80;
24 month_hi = (date_high & 0x1) << 3;
25 month_low = date_low >> 5;
26 day = date_low & 0x1f;
27 sprintf(ans, "%2d-%02d-%02d", month_hi+month_low, day, year);
28 return(ans);
32 * Convert MSDOS directory timestamp to ASCII.
35 char *
36 convtime(time_high, time_low)
37 unsigned time_high, time_low;
40 * hi byte | low byte
41 * |7|6|5|4|3|2|1|0|7|6|5|4|3|2|1|0|
42 * | | | | | | | | | | | | | | | | |
43 * \ 5 bits /\ 6 bits /\ 5 bits /
44 * hour minutes sec*2
46 static char ans[7];
47 char am_pm;
48 unsigned char hour, min_hi, min_low;
50 hour = time_high >> 3;
51 am_pm = (hour >= 12) ? 'p' : 'a';
52 if (hour > 12)
53 hour = hour -12;
54 if (hour == 0)
55 hour = 12;
56 min_hi = (time_high & 0x7) << 3;
57 min_low = time_low >> 5;
58 sprintf(ans, "%2d:%02d%c", hour, min_hi+min_low, am_pm);
59 return(ans);
63 * Convert a MSDOS time & date stamp to the Unix time() format
66 long
67 convstamp(time_field, date_field)
68 unsigned char *time_field, *date_field;
70 extern long timezone;
71 int year, mon, mday, hour, min, sec, old_leaps;
72 long answer, sec_year, sec_mon, sec_mday, sec_hour, sec_min, sec_leap;
73 static int month[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304,
74 334};
75 /* disect the parts */
76 year = (date_field[1] >> 1) + 1980;
77 mon = (((date_field[1] & 0x1) << 3) + (date_field[0] >> 5));
78 mday = date_field[0] & 0x1f;
79 hour = time_field[1] >> 3;
80 min = (((time_field[1] & 0x7) << 3) + (time_field[0] >> 5));
81 sec = (time_field[0] & 0x1f) * 2;
82 /* how many previous leap years */
83 old_leaps = (year -1972) / 4;
84 sec_leap = old_leaps * 24 * 60 * 60;
85 /* back off 1 day if before 29 Feb */
86 if (!(year % 4) && mon < 3)
87 sec_leap -= 24 * 60 * 60;
88 sec_year = (year - 1970) * 365 * 24 * 60 * 60;
89 sec_mon = month[mon -1] * 24 * 60 * 60;
90 sec_mday = mday * 24 * 60 * 60;
91 sec_hour = hour * 60 * 60;
92 sec_min = min * 60;
94 answer = sec_leap + sec_year + sec_mon + sec_mday + sec_hour + sec_min + sec/* + timezone*/;
95 return(answer);