1 #ifndef LINT
2 static char mstimeid[]="@(#) mstime.i 2.2 88/01/24 12:47:58";
3 #endif /* LINT */
4
5 /*
6 (C) Copyright 1987 Rahul Dhesi -- All rights reserved
7 */
8
9 #define BASEYEAR 1970
10
11 /****************
12 Function mstime() converts time in seconds since January 1 of BASEYEAR
13 to MS-DOS format date and time.
14 */
mstime(longtime,date,time)15 mstime(longtime, date, time)
16 long longtime; /* input: seconds since Jan 1, BASEYEAR */
17 int *date, *time; /* output: MS-DOS format date and time */
18
19 {
20 static int daysinmo[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
21 #define FEBRUARY 1
22 int year, month, day, hour, min, sec;
23 long secsinhour, secsinday, secsinyear, secsinleapyear;
24
25 int leapyear; /* is this a leap year? */
26 int done; /* control variable */
27
28 secsinhour = (long) (60 * 60); /* seconds in an hour */
29 secsinday = 24 * secsinhour; /* seconds in a day */
30 secsinyear = 365 * secsinday; /* seconds in a year */
31 secsinleapyear = secsinyear + secsinday; /* seconds in a leap year */
32
33 #ifdef DEBUG
34 printf("mstime: input longtime = %ld\n", longtime);
35 #endif
36
37 /* We can't handle dates before 1970 so force longtime positive */
38 if (longtime < 0)
39 longtime = 0;
40
41 /*
42 Step through years from BASEYEAR onwards, subtracting number of
43 seconds in each, stopping just before longtime would become negative.
44 */
45 year = BASEYEAR;
46 done = 0;
47 while (!done) {
48 long yearlength;
49 leapyear = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
50 if (leapyear)
51 yearlength = secsinleapyear;
52 else
53 yearlength = secsinyear;
54
55 if (longtime >= yearlength) {
56 longtime -= yearlength;
57 year++;
58 } else
59 done++;
60 }
61
62 /* Now `year' contains year and longtime contains remaining seconds */
63 daysinmo[FEBRUARY] = leapyear ? 29 : 28;
64
65 month = 0; /* range is 0:11 */
66 while (longtime > daysinmo[month] * secsinday) {
67 longtime = longtime - daysinmo[month] * secsinday;
68 month++;
69 }
70 month++; /* range now 1:12 */
71
72 day = longtime / secsinday; /* day of month, range 0:30 */
73 longtime = longtime % secsinday;
74 day++; /* day of month, range 1:31 */
75
76 hour = longtime / secsinhour; /* hours, range 0:23 */
77 longtime = longtime % secsinhour;
78
79 min = longtime / 60L; /* minutes, range 0:59 */
80 longtime = longtime % 60L;
81
82 sec = longtime; /* seconds, range 0:59 */
83
84 #ifdef DEBUG
85 printf("mstime: date = %4d/%02d/%02d time = %02d:%02d:%02d\n",
86 year, month, day, hour, min, sec);
87 if (leapyear)
88 printf("(leap year)\n");
89 #endif
90
91 if (year < 1980)
92 year = 1980;
93 *date = day + (month << 5) + ((year - 1980) << 9);
94 *time = (sec / 2) + (min << 5) + (hour << 11);
95 }
96