1 #ifndef LINT
2 static char nixtimeid[]="@(#) nixtime.i 2.3 88/01/24 12:49:28";
3 #endif /* LINT */
4 
5 /*
6 Time handling routines for UNIX systems.  These are included by the file
7 machine.c as needed.
8 
9 The contents of this file are hereby released to the public domain.
10 
11                                     -- Rahul Dhesi  1986/12/31
12 */
13 
14 struct tm *localtime();
15 
16 /*****************
17 Function gettime() gets the date and time of the file handle supplied.
18 Date and time is in MSDOS format.
19 */
gettime(file,date,time)20 int gettime (file, date, time)
21 ZOOFILE file;
22 unsigned *date, *time;
23 {
24    struct stat buf;           /* buffer to hold file information */
25    struct tm *tm;             /* will hold year/month/day etc. */
26 	int handle;
27 	handle = fileno(file);
28    if (fstat (handle, &buf) == -1) {
29       prterror ('w', "Could not get file time\n");
30       *date = *time = 0;
31    } else {
32       tm = localtime (&buf.st_mtime); /* get info about file mod time */
33       *date = tm->tm_mday + ((tm->tm_mon + 1) << 5) +
34          ((tm->tm_year - 80) << 9);
35       *time = tm->tm_sec / 2 + (tm->tm_min << 5) +
36          (tm->tm_hour << 11);
37    }
38 
39 }
40 
41 /*****************
42 Function setutime() sets the date and time of the filename supplied.
43 Date and time is in MSDOS format.  It assumes the existence of a function
44 mstonix() that accepts MSDOS format time and returns **IX format time,
45 and a function gettz() that returns the difference (localtime - gmt)
46 in seconds, taking daylight savings time into account.
47 */
setutime(path,date,time)48 int setutime(path,date,time)
49 char *path;
50 unsigned int date, time;
51 {
52 	long mstonix();
53 	long gettz();
54 	long utimbuf[2];
55 	long t = mstonix (date, time);
56 	utimbuf[0] = utimbuf[1] = t + gettz(t);
57 	return (utime (path, utimbuf));
58 }
59 
60 /****************
61 Function mstonix() accepts an MSDOS format date and time and returns
62 a **IX format time.  No adjustment is done for timezone.
63 */
64 
mstonix(date,time)65 long mstonix (date, time)
66 unsigned int date, time;
67 {
68    int year, month, day, hour, min, sec, daycount;
69    long longtime;
70    /* no. of days to beginning of month for each month */
71    static int dsboy[12] = { 0, 31, 59, 90, 120, 151, 181, 212,
72                               243, 273, 304, 334};
73 
74    if (date == 0 && time == 0)			/* special case! */
75       return (0L);
76 
77    /* part of following code is common to zoolist.c */
78    year  =  (((unsigned int) date >> 9) & 0x7f) + 1980;
79    month =  ((unsigned int) date >> 5) & 0x0f;
80    day   =  date        & 0x1f;
81 
82    hour =  ((unsigned int) time >> 11)& 0x1f;
83    min   =  ((unsigned int) time >> 5) & 0x3f;
84    sec   =  ((unsigned int) time & 0x1f) * 2;
85 
86 /*
87 DEBUG and leap year fixes thanks to Mark Alexander
88 <uunet!amdahl!drivax!alexande>
89 */
90 #ifdef DEBUG
91    printf ("mstonix:  year=%d  month=%d  day=%d  hour=%d  min=%d  sec=%d\n",
92            year, month, day, hour, min, sec);
93 #endif
94 
95    /* Calculate days since 1970/01/01 */
96    daycount = 365 * (year - 1970) +    /* days due to whole years */
97                (year - 1969) / 4 +     /* days due to leap years */
98                dsboy[month-1] +        /* days since beginning of this year */
99                day-1;                  /* days since beginning of month */
100 
101    if (year % 4 == 0 &&
102        year % 400 != 0 && month >= 3)  /* if this is a leap year and month */
103       daycount++;                      /* is March or later, add a day */
104 
105    /* Knowing the days, we can find seconds */
106    longtime = daycount * 24L * 60L * 60L    +
107           hour * 60L * 60L   +   min * 60   +    sec;
108 	return (longtime);
109 }
110