3 * (c) Copyright 1990 by the Massachusetts Institute of Technology.
4 * For copying and distribution information, please see the file
8 #include <mit-copyright.h>
11 #include <sys/types.h>
15 #include <moira_site.h>
16 EXEC SQL INCLUDE sqlca;
18 static char *MONTHNAMES[] = {"january", "february", "march", "april", "may",
19 "june", "july", "august", "september",
20 "october", "november", "december"};
22 static int MONTHDAYS[] = {0,0,31,59,90,120,151,181,212,243,273,304,334};
25 /****************************************************************/
26 /** Return the difference between the modtime & table modtime **/
27 /** in flag variable. Return 0 if success, else errno. **/
28 /****************************************************************/
30 int ModDiff(int *flag, char *tbl, time_t ModTime)
33 EXEC SQL BEGIN DECLARE SECTION;
34 char filetime[48], *tbl_name;
35 EXEC SQL END DECLARE SECTION;
39 EXEC SQL SELECT TO_CHAR(modtime, 'DD-mon-YYYY HH24:MI:SS')
40 INTO :filetime FROM tblstats
41 WHERE table_name = :tbl_name;
44 fprintf(stderr, "Moddiff Query failed: %d\n", sqlca.sqlcode);
47 if (Date2Sec (filetime, &filetimeno))
49 fprintf(stderr, "Unable to parse mod. date for file %s\n", tbl);
52 *flag = (int) (filetimeno - ModTime);
57 /***************************************************/
58 /** convert Unix-time (# of seconds since 1970) **/
59 /** from a date-time string **/
60 /** Return 1 if failure, 0 if success **/
61 /***************************************************/
63 int Date2Sec(char *DS, time_t *UTime)
65 static int defzone = -1;
66 int Day, Month, Year, Hour, Minute, Sec, Err, TotalDays, Iter;
72 if (gettimeofday(&tv, &tz))
75 defzone = tz.tz_minuteswest;
78 Err = ParseDateString (DS, &Day, &Month, &Year, &Hour, &Minute, &Sec);
84 for (TotalDays = 0, Iter = 1969; Iter > Year; Iter--)
85 TotalDays += NumdaysY(Iter);
86 TotalDays += (NumdaysY(Year) - NumdaysM(Month, Year) - Day);
87 *UTime = -((((24 * TotalDays) + 24 - Hour) * 60 - Minute) * 60 -
93 for (TotalDays = 0, Iter = 1970; Iter < Year; Iter++)
94 TotalDays += NumdaysY(Iter);
95 TotalDays += NumdaysM(Month, Year);
96 TotalDays += (Day - 1);
97 *UTime = (((24 * TotalDays) + Hour) * 60 + Minute) * 60 +
104 /*****************************************/
105 /** Return the # of days in the Year **/
106 /*****************************************/
108 int NumdaysY(int Year)
110 return 365 + Leapyear(Year);
114 /*****************************************/
115 /** Return the # of days in the Month **/
116 /*****************************************/
118 int NumdaysM(int Month, int Year)
120 if ((Month > 2) && (Leapyear (Year)))
121 return MONTHDAYS[Month] + 1;
123 return MONTHDAYS[Month];
127 /*****************************************/
128 /** Return 1 if a leapyear, else 0 **/
129 /*****************************************/
131 int Leapyear(int Year)
133 if ((Year % 4) && (!(Year % 100) || (Year % 1000)))
140 /************************************************/
141 /** Compute numeric breakdown of date string **/
142 /** Return 0 if success, 1 if failure **/
143 /************************************************/
145 int ParseDateString(char *DS, int *Day, int *Month, int *Year, int *Hour,
146 int *Minute, int *Sec)
150 int Y = 0, H = 0, Min = 0, S = 0, DayNum = 0, MonthNum = 0;
152 M = malloc(strlen(DS) + 2);
153 D = malloc(strlen(DS) + 2);
156 Gotten = sscanf (DS, "%[^-]-%[^-]-%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
158 Gotten = sscanf (DS, "%[^/]/%[^/]/%d %d:%d:%d", D, M, &Y, &H, &Min, &S);
160 Gotten = sscanf (DS, "%s %[^,], %d %d:%d:%d", M, D, &Y, &H, &Min, &S);
161 if ((Gotten < 3) || !(D && M && Y))
165 return 1; /* Couldn't scan in a date */
168 { /* Month not text, so M/D/Y not D/M/Y */
174 MonthNum = MonthNo(M);
177 if ((DayNum < 1) || (DayNum > 31))
178 return 1; /* Bad Day */
180 return 1; /* Bad Month */
181 if ((Y < 1) || (Y > 10000))
182 return 1; /* Bad Year */
184 return 1; /* Bad Time (Hour only) */
185 if ((Gotten > 4) && (H < 0) || (H > 24))
186 return 1; /* Bad Hour */
187 if ((Gotten > 4) && ((Min < 0) || (Min > 59)))
188 return 1; /* Bad Minute */
189 if ((Gotten > 5) && ((S < 0) || (S > 59)))
190 return 1; /* Bad Second */
193 if (Y < 100) /* For abreviations like 90 for 1990 */
194 Y += 1900; /* (Yes, it will be wrong in 2000) */
214 /***********************************************************/
215 /** Return the Month number of a Month number or string **/
216 /***********************************************************/
223 if ((atoi(M) > 0) && (atoi(M) < 13))
228 for (Count = 0; Count < 12; Count++)
230 if (!strcasecmp (M, MONTHNAMES[Count])
231 || !strncasecmp(M, MONTHNAMES[Count], 3))