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