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