]> andersk Git - moira.git/blob - gen/moddiff.dc
make it work on Ultrix
[moira.git] / gen / moddiff.dc
1 /* $Header$
2  *
3  *  (c) Copyright 1990 by the Massachusetts Institute of Technology.
4  *  For copying and distribution information, please see the file
5  *  <mit-copyright.h>.
6  */
7
8 #include <mit-copyright.h>
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <sys/time.h>
13 #include <moira.h>
14 #include <moira_site.h>
15 EXEC SQL INCLUDE sqlca;
16
17 static char * MONTHNAMES[] = {"january", "february", "march", "april", "may", 
18                               "june", "july", "august", "september",
19                               "october", "november", "december"};
20
21 static int MONTHDAYS[] = {0,0,31,59,90,120,151,181,212,243,273,304,334};
22
23
24 /****************************************************************/
25 /**  Return the difference between the modtime & table modtime **/
26 /**  in flag variable.  Return 0 if success, else errno.       **/
27 /****************************************************************/
28
29 int ModDiff (flag, tbl, ModTime)
30 int *flag;
31 char *tbl;
32 time_t ModTime;
33 {
34     int filetimeno;
35     EXEC SQL BEGIN DECLARE SECTION;
36     char filetime[29], *tbl_name;
37     EXEC SQL END DECLARE SECTION;
38
39     *flag = 0;
40     tbl_name = tbl;
41     EXEC SQL SELECT MODTIME INTO :filetime FROM tblstats WHERE tbl = :tbl_name;
42     if (Date2Sec (filetime, &filetimeno)) {
43         fprintf(stderr,"Unable to parse mod. date for file %s\n", tbl);
44         return(MR_DATE);
45     }
46     *flag = (filetimeno - ModTime);
47     return(0);
48 }
49
50
51 /***************************************************/
52 /**  convert Unix-time (# of seconds since 1970)  **/
53 /**  from a date-time string                      **/
54 /**  Return 1 if failure, 0 if success            **/
55 /***************************************************/
56
57 Date2Sec(DS, UTime)
58 char *DS;
59 struct timeval *UTime;
60 {
61     int Day, Month, Year, Hour, Minute, Sec, Err, TotalDays, Iter;
62
63     Err = ParseDateString (DS, &Day, &Month, &Year, &Hour, &Minute,&Sec);
64     if (Err) return (Err);
65     if (Year < 1970) {
66         for (TotalDays = 0, Iter = 1969; Iter > Year; Iter--) {
67             TotalDays += NumdaysY(Iter);}
68 #ifdef DEBUG
69         printf("Days in years           : %d\n", TotalDays);
70 #endif
71         TotalDays += (NumdaysY(Year) - NumdaysM(Month, Year) - Day);
72 #ifdef DEBUG
73         printf("Days after months & days: %d\n", TotalDays);
74 #endif
75         UTime->tv_sec = -((((24 * TotalDays) + 24 - Hour) * 60 - Minute) * 60 -
76                           Sec);
77         UTime->tv_usec = 0;
78         return (0);
79     } else {
80         for (TotalDays = 0, Iter = 1970; Iter < Year; Iter++) {
81             TotalDays += NumdaysY(Iter);}
82 #ifdef DEBUG
83         printf("Days in years    : %d\n", TotalDays);
84 #endif
85         TotalDays += NumdaysM(Month, Year);
86 #ifdef DEBUG
87         printf("Days after months: %d\n", TotalDays);
88 #endif
89         TotalDays += (Day - 1);
90 #ifdef DEBUG
91         printf("Days after Days  : %d\n", TotalDays);
92 #endif
93         UTime->tv_sec = (((24 * TotalDays) + Hour) * 60 + Minute) * 60 + Sec;
94         UTime->tv_usec = 0;
95         return (0);
96     }
97 }
98
99
100 /*****************************************/
101 /**  Return the # of days in the Year   **/
102 /*****************************************/
103
104 static int NumdaysY(Year)
105 int Year;
106 {
107     return (365 + Leapyear(Year));
108 }
109
110
111 /*****************************************/
112 /**  Return the # of days in the Month  **/
113 /*****************************************/
114
115 static int NumdaysM(Month, Year)
116 int Month, Year;
117 {
118     if ((Month > 2) && (Leapyear (Year)))
119       return (MONTHDAYS[Month] + 1);
120     else
121       return (MONTHDAYS[Month]);
122 }
123
124
125 /*****************************************/
126 /**  Return 1 if a leapyear, else 0     **/
127 /*****************************************/
128
129 static int Leapyear (Year)
130 int Year;
131 {
132     if ((Year % 4) && (!(Year % 100) || (Year % 1000)))
133       return (0);
134     else
135       return (1);
136 }
137
138
139 /************************************************/
140 /**  Compute numeric breakdown of date string  **/
141 /**  Return 0 if success, 1 if failure         **/
142 /************************************************/
143
144 static int ParseDateString (DS, Day, Month, Year, Hour, Minute, Sec)
145 char *DS;
146 int *Day, *Month, *Year, *Hour, *Minute, *Sec;
147 {
148     int Gotten;
149     char *M, *D, *Temp;
150     int Y=0, H=0, Min=0, S=0, DayNum=0, MonthNum=0;
151
152     M = (char *)malloc(strlen(DS));
153     D = (char *)malloc(strlen(DS));
154     if (!(M && D)) return (0);
155     Gotten = sscanf (DS, "%[^-]-%[^-]-%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
156     if (Gotten < 3) 
157       Gotten = sscanf (DS, "%[^/]/%[^/]/%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
158     if (Gotten < 3)
159       Gotten = sscanf (DS, "%s %[^,], %d %d:%d:%d", M, D, &Y, &H, &Min, &S);
160 #ifdef DEBUG
161     printf ("Month = %s\nDay = %s\nYear = %d\n", M, D, Y);
162     printf ("Hour = %d\nMin= %d\nSec = %d\n", H, Min, S);
163 #endif
164     if ((Gotten < 3) || !(D && M && Y)) {
165         free(M);
166         free(D);
167         return (1);}               /* Couldn't scan in a date */
168     if (atoi(M)) {               /* Month not text, so M/D/Y not D/M/Y */
169         Temp = M;
170         M = D;
171         D = Temp;}
172     DayNum = atoi(D);
173     MonthNum = MonthNo(M);
174     free(M);
175     free(D);
176 #ifdef DEBUG
177     printf ("Month = %d\nDay = %d\nYear = %d\n", MonthNum, DayNum, Y);
178 #endif
179     if ((DayNum < 1) || (DayNum > 31)) 
180       return (1);                /* Bad Day */
181     if (!MonthNum) 
182       return (1);                /* Bad Month */
183     if ((Y < 1) || (Y > 10000)) 
184       return (1);                /* Bad Year */
185     if (Gotten == 4) 
186       return(1);                 /* Bad Time (Hour only) */
187     if ((Gotten > 4) && (H < 0) || (H > 24)) 
188       return(1);                 /* Bad Hour */
189     if ((Gotten > 4) && ((Min < 0) || (Min > 59))) 
190       return(1);                 /* Bad Minute */
191     if ((Gotten > 5) && ((S < 0) || (S > 59))) 
192       return(1);                 /* Bad Second */
193     *Day = DayNum; 
194     *Month = MonthNum;
195     if (Y < 100)                   /* For abreviations like 90 for 1990    */
196       Y += 1900;                   /* (Yes, it will be wrong in 2000) */
197     *Year = Y;
198     if (Gotten > 4) {
199         *Hour = H;
200         *Minute = Min;
201     }
202     else {
203         *Hour = 0;
204         *Minute = 0;}
205     if (Gotten > 5) 
206       *Sec = S;
207     else
208       *Sec = 0;
209     return(0);
210 }
211
212
213 /***********************************************************/
214 /**  Return the Month number of a Month number or string  **/
215 /***********************************************************/
216
217 static int MonthNo (M)
218 char *M;
219 {
220     int Count;
221     if (atoi(M)) {
222         if ((atoi(M) > 0) && (atoi(M) < 13))
223           return (atoi(M));
224         else
225           return (0);
226     }  
227     for (Count = 0; Count < 12; Count++) {
228         if (!strcasecmp (M, MONTHNAMES[Count]) 
229             || !strncasecmp(M, MONTHNAMES[Count], 3)) 
230           return (Count + 1);
231     }
232     return (0);
233 }
This page took 0.061579 seconds and 5 git commands to generate.