]> andersk Git - moira.git/blob - gen/moddiff.dc
#include file and prototype fixes
[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     time_t filetimeno;
35     EXEC SQL BEGIN DECLARE SECTION;
36     char filetime[48], *tbl_name;
37     EXEC SQL END DECLARE SECTION;
38
39     *flag = 0;
40     tbl_name = tbl;
41     EXEC SQL SELECT TO_CHAR(modtime, 'DD-mon-YYYY HH24:MI:SS') INTO :filetime FROM tblstats 
42       WHERE table_name = :tbl_name;
43     if (sqlca.sqlcode != 0) {
44         fprintf(stderr, "Moddiff Query failed: %d\n", sqlca.sqlcode);
45         return(MR_DATE);
46     }
47 #ifdef DEBUG
48     printf("about to call Date2Sec, flag = 0x%x\n", flag);
49 #endif
50     if (Date2Sec (filetime, &filetimeno)) {
51         fprintf(stderr,"Unable to parse mod. date for file %s\n", tbl);
52         return(MR_DATE);
53     }
54 #ifdef DEBUG
55     printf("got time, flag = 0x%x\n", flag);
56 #endif
57     *flag = (int) (filetimeno - ModTime);
58     return(0);
59 }
60
61
62 /***************************************************/
63 /**  convert Unix-time (# of seconds since 1970)  **/
64 /**  from a date-time string                      **/
65 /**  Return 1 if failure, 0 if success            **/
66 /***************************************************/
67
68 Date2Sec(DS, UTime)
69 char *DS;
70 time_t *UTime;
71 {
72     static int defzone = -1;
73     int Day, Month, Year, Hour, Minute, Sec, Err, TotalDays, Iter;
74     struct timeval tv;
75     struct timezone tz;
76
77     if (defzone == -1) {
78         if (gettimeofday(&tv, &tz))
79           defzone = 0;
80         else
81           defzone = tz.tz_minuteswest;
82     }
83
84 #ifdef DEBUG
85     printf("Date2Sec(\"%s\", %d)\n", DS, *UTime);
86 #endif
87     Err = ParseDateString (DS, &Day, &Month, &Year, &Hour, &Minute,&Sec);
88 #ifdef DEBUG
89     printf("date=%d/%d/%d %d:%d:%d, err=%d\n", Day, Month, Year,
90            Hour, Minute, Sec, Err);
91 #endif
92     if (Err) return (Err);
93
94     if (Year < 1970) {
95         for (TotalDays = 0, Iter = 1969; Iter > Year; Iter--) {
96             TotalDays += NumdaysY(Iter);}
97 #ifdef DEBUG
98         printf("Days in years           : %d\n", TotalDays);
99 #endif
100         TotalDays += (NumdaysY(Year) - NumdaysM(Month, Year) - Day);
101 #ifdef DEBUG
102         printf("Days after months & days: %d\n", TotalDays);
103 #endif
104         *UTime = -((((24 * TotalDays) + 24 - Hour) * 60 - Minute) * 60 -
105                           Sec) + defzone;
106         return (0);
107     } else {
108         for (TotalDays = 0, Iter = 1970; Iter < Year; Iter++) {
109             TotalDays += NumdaysY(Iter);}
110 #ifdef DEBUG
111         printf("Days in years    : %d\n", TotalDays);
112 #endif
113         TotalDays += NumdaysM(Month, Year);
114 #ifdef DEBUG
115         printf("Days after months: %d\n", TotalDays);
116 #endif
117         TotalDays += (Day - 1);
118 #ifdef DEBUG
119         printf("Days after Days  : %d\n", TotalDays);
120         printf("Calculating seconds...(UTime = 0x%x)\n", *UTime);
121 #endif
122         *UTime = (((24 * TotalDays) + Hour) * 60 + Minute) * 60 +
123                         Sec + defzone;
124 #ifdef DEBUG
125         printf("Calculated value of %d\n", *UTime);
126 #endif
127         return (0);
128     }
129 }
130
131
132 /*****************************************/
133 /**  Return the # of days in the Year   **/
134 /*****************************************/
135
136 static int NumdaysY(Year)
137 int Year;
138 {
139     return (365 + Leapyear(Year));
140 }
141
142
143 /*****************************************/
144 /**  Return the # of days in the Month  **/
145 /*****************************************/
146
147 static int NumdaysM(Month, Year)
148 int Month, Year;
149 {
150     if ((Month > 2) && (Leapyear (Year)))
151       return (MONTHDAYS[Month] + 1);
152     else
153       return (MONTHDAYS[Month]);
154 }
155
156
157 /*****************************************/
158 /**  Return 1 if a leapyear, else 0     **/
159 /*****************************************/
160
161 static int Leapyear (Year)
162 int Year;
163 {
164     if ((Year % 4) && (!(Year % 100) || (Year % 1000)))
165       return (0);
166     else
167       return (1);
168 }
169
170
171 /************************************************/
172 /**  Compute numeric breakdown of date string  **/
173 /**  Return 0 if success, 1 if failure         **/
174 /************************************************/
175
176 static int ParseDateString (DS, Day, Month, Year, Hour, Minute, Sec)
177 char *DS;
178 int *Day, *Month, *Year, *Hour, *Minute, *Sec;
179 {
180     int Gotten;
181     char *M, *D, *Temp;
182     int Y=0, H=0, Min=0, S=0, DayNum=0, MonthNum=0;
183
184     M = (char *)malloc(strlen(DS) + 2);
185     D = (char *)malloc(strlen(DS) + 2);
186     if (!(M && D)) return (1);
187     Gotten = sscanf (DS, "%[^-]-%[^-]-%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
188     if (Gotten < 3) 
189       Gotten = sscanf (DS, "%[^/]/%[^/]/%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
190     if (Gotten < 3)
191       Gotten = sscanf (DS, "%s %[^,], %d %d:%d:%d", M, D, &Y, &H, &Min, &S);
192 #ifdef DEBUG
193     printf ("Month = %s\nDay = %s\nYear = %d\n", M, D, Y);
194     printf ("Hour = %d\nMin= %d\nSec = %d\n", H, Min, S);
195 #endif
196     if ((Gotten < 3) || !(D && M && Y)) {
197         free(M);
198         free(D);
199         return (1);}               /* Couldn't scan in a date */
200     if (atoi(M)) {               /* Month not text, so M/D/Y not D/M/Y */
201         Temp = M;
202         M = D;
203         D = Temp;}
204     DayNum = atoi(D);
205     MonthNum = MonthNo(M);
206     free(M);
207     free(D);
208 #ifdef DEBUG
209     printf ("Month = %d\nDay = %d\nYear = %d\n", MonthNum, DayNum, Y);
210 #endif
211     if ((DayNum < 1) || (DayNum > 31)) 
212       return (1);                /* Bad Day */
213     if (!MonthNum) 
214       return (1);                /* Bad Month */
215     if ((Y < 1) || (Y > 10000)) 
216       return (1);                /* Bad Year */
217     if (Gotten == 4) 
218       return(1);                 /* Bad Time (Hour only) */
219     if ((Gotten > 4) && (H < 0) || (H > 24)) 
220       return(1);                 /* Bad Hour */
221     if ((Gotten > 4) && ((Min < 0) || (Min > 59))) 
222       return(1);                 /* Bad Minute */
223     if ((Gotten > 5) && ((S < 0) || (S > 59))) 
224       return(1);                 /* Bad Second */
225     *Day = DayNum; 
226     *Month = MonthNum;
227     if (Y < 100)                   /* For abreviations like 90 for 1990    */
228       Y += 1900;                   /* (Yes, it will be wrong in 2000) */
229     *Year = Y;
230     if (Gotten > 4) {
231         *Hour = H;
232         *Minute = Min;
233     }
234     else {
235         *Hour = 0;
236         *Minute = 0;}
237     if (Gotten > 5) 
238       *Sec = S;
239     else
240       *Sec = 0;
241     return(0);
242 }
243
244
245 /***********************************************************/
246 /**  Return the Month number of a Month number or string  **/
247 /***********************************************************/
248
249 static int MonthNo (M)
250 char *M;
251 {
252     int Count;
253     if (atoi(M)) {
254         if ((atoi(M) > 0) && (atoi(M) < 13))
255           return (atoi(M));
256         else
257           return (0);
258     }  
259     for (Count = 0; Count < 12; Count++) {
260         if (!strcasecmp (M, MONTHNAMES[Count]) 
261             || !strncasecmp(M, MONTHNAMES[Count], 3)) 
262           return (Count + 1);
263     }
264     return (0);
265 }
This page took 0.06691 seconds and 5 git commands to generate.