]> andersk Git - moira.git/blame - gen/moddiff.pc
Fixes to build with `gcc' instead of `gcc -traditional' on suns. (Needed
[moira.git] / gen / moddiff.pc
CommitLineData
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>
15EXEC SQL INCLUDE sqlca;
16
17static char * MONTHNAMES[] = {"january", "february", "march", "april", "may",
18 "june", "july", "august", "september",
19 "october", "november", "december"};
20
21static 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
29int ModDiff (flag, tbl, ModTime)
30int *flag;
31char *tbl;
32time_t ModTime;
33{
89f6df42 34 time_t filetimeno;
f852c398 35 EXEC SQL BEGIN DECLARE SECTION;
89f6df42 36 char filetime[48], *tbl_name;
f852c398 37 EXEC SQL END DECLARE SECTION;
38
39 *flag = 0;
40 tbl_name = tbl;
9c04b191 41 EXEC SQL SELECT TO_CHAR(modtime, 'DD-mon-YYYY HH24:MI:SS') INTO :filetime FROM tblstats
89f6df42 42 WHERE table_name = :tbl_name;
d78c096f 43 if (sqlca.sqlcode != 0) {
2e1d4cdb 44 fprintf(stderr, "Moddiff Query failed: %d\n", sqlca.sqlcode);
d78c096f 45 return(MR_DATE);
46 }
89f6df42 47#ifdef DEBUG
48 printf("about to call Date2Sec, flag = 0x%x\n", flag);
49#endif
f852c398 50 if (Date2Sec (filetime, &filetimeno)) {
51 fprintf(stderr,"Unable to parse mod. date for file %s\n", tbl);
52 return(MR_DATE);
53 }
89f6df42 54#ifdef DEBUG
55 printf("got time, flag = 0x%x\n", flag);
56#endif
57 *flag = (int) (filetimeno - ModTime);
f852c398 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
68Date2Sec(DS, UTime)
69char *DS;
89f6df42 70time_t *UTime;
f852c398 71{
9309b3d8 72 static int defzone = -1;
f852c398 73 int Day, Month, Year, Hour, Minute, Sec, Err, TotalDays, Iter;
89f6df42 74 struct timeval tv;
75 struct timezone tz;
f852c398 76
9309b3d8 77 if (defzone == -1) {
16de9b7a 78 if (gettimeofday(&tv, &tz))
9309b3d8 79 defzone = 0;
80 else
16de9b7a 81 defzone = tz.tz_minuteswest;
9309b3d8 82 }
83
d78c096f 84#ifdef DEBUG
89f6df42 85 printf("Date2Sec(\"%s\", %d)\n", DS, *UTime);
d78c096f 86#endif
f852c398 87 Err = ParseDateString (DS, &Day, &Month, &Year, &Hour, &Minute,&Sec);
d78c096f 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
f852c398 92 if (Err) return (Err);
d78c096f 93
f852c398 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
89f6df42 104 *UTime = -((((24 * TotalDays) + 24 - Hour) * 60 - Minute) * 60 -
9309b3d8 105 Sec) + defzone;
f852c398 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);
89f6df42 120 printf("Calculating seconds...(UTime = 0x%x)\n", *UTime);
f852c398 121#endif
89f6df42 122 *UTime = (((24 * TotalDays) + Hour) * 60 + Minute) * 60 +
9309b3d8 123 Sec + defzone;
89f6df42 124#ifdef DEBUG
125 printf("Calculated value of %d\n", *UTime);
126#endif
f852c398 127 return (0);
128 }
129}
130
131
132/*****************************************/
133/** Return the # of days in the Year **/
134/*****************************************/
135
98a7b0ee 136int NumdaysY(Year)
f852c398 137int Year;
138{
139 return (365 + Leapyear(Year));
140}
141
142
143/*****************************************/
144/** Return the # of days in the Month **/
145/*****************************************/
146
98a7b0ee 147int NumdaysM(Month, Year)
f852c398 148int 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
98a7b0ee 161int Leapyear (Year)
f852c398 162int 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
98a7b0ee 176int ParseDateString (DS, Day, Month, Year, Hour, Minute, Sec)
f852c398 177char *DS;
178int *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
89f6df42 184 M = (char *)malloc(strlen(DS) + 2);
185 D = (char *)malloc(strlen(DS) + 2);
d78c096f 186 if (!(M && D)) return (1);
f852c398 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
98a7b0ee 249int MonthNo (M)
f852c398 250char *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.106046 seconds and 5 git commands to generate.